Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details.
3 * dynamic window manager is designed like any other X client as well. It is
4 * driven through handling X events. In contrast to other X clients, a window
5 * manager selects for SubstructureRedirectMask on the root window, to receive
6 * events about window (dis-)appearance. Only one X connection at a time is
7 * allowed to select for this event mask.
9 * The event handlers of dwm are organized in an array which is accessed
10 * whenever a new event has been fetched. This allows event dispatching
13 * Each child of the root window is called a client, except windows which have
14 * set the override_redirect flag. Clients are organized in a global
15 * linked client list, the focus history is remembered through a global
16 * stack list. 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(M, C) ((M) == (&mon[C->mon]) && (C->tags & tagset[M->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 ((int)((1LL << 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 Client Client
;
85 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
89 Bool isfixed
, isfloating
, isurgent
;
97 unsigned long norm
[ColLast
];
98 unsigned long sel
[ColLast
];
108 } DC
; /* draw context */
113 void (*func
)(const Arg
*);
119 int by
, btx
; /* bar geometry */
120 int wx
, wy
, ww
, wh
; /* window area */
121 unsigned int seltags
;
130 void (*arrange
)(Monitor
*);
135 const char *instance
;
141 /* function declarations */
142 static void applyrules(Client
*c
);
143 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
);
144 static void arrange(void);
145 static void attach(Client
*c
);
146 static void attachstack(Client
*c
);
147 static void buttonpress(XEvent
*e
);
148 static void checkotherwm(void);
149 static void cleanup(void);
150 static void clearurgent(Client
*c
);
151 static void configure(Client
*c
);
152 static void configurenotify(XEvent
*e
);
153 static void configurerequest(XEvent
*e
);
154 static void destroynotify(XEvent
*e
);
155 static void detach(Client
*c
);
156 static void detachstack(Client
*c
);
157 static void die(const char *errstr
, ...);
158 static void drawbar(Monitor
*m
);
159 static void drawbars();
160 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
161 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
162 static void enternotify(XEvent
*e
);
163 static void expose(XEvent
*e
);
164 static void focus(Client
*c
);
165 static void focusin(XEvent
*e
);
166 static void focusstack(const Arg
*arg
);
167 static Client
*getclient(Window w
);
168 static unsigned long getcolor(const char *colstr
);
169 static long getstate(Window w
);
170 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
171 static void grabbuttons(Client
*c
, Bool focused
);
172 static void grabkeys(void);
173 static void initfont(const char *fontstr
);
174 static Bool
isprotodel(Client
*c
);
175 static void keypress(XEvent
*e
);
176 static void killclient(const Arg
*arg
);
177 static void manage(Window w
, XWindowAttributes
*wa
);
178 static void mappingnotify(XEvent
*e
);
179 static void maprequest(XEvent
*e
);
180 static void monocle(Monitor
*m
);
181 static void movemouse(const Arg
*arg
);
182 static Client
*nexttiled(Monitor
*m
, Client
*c
);
183 static void propertynotify(XEvent
*e
);
184 static void quit(const Arg
*arg
);
185 static void resize(Client
*c
, int x
, int y
, int w
, int h
);
186 static void resizemouse(const Arg
*arg
);
187 static void restack(Monitor
*m
);
188 static void run(void);
189 static void scan(void);
190 static void setclientstate(Client
*c
, long state
);
191 static void setlayout(const Arg
*arg
);
192 static void setmfact(const Arg
*arg
);
193 static void setup(void);
194 static void showhide(Client
*c
);
195 static void sigchld(int signal
);
196 static void spawn(const Arg
*arg
);
197 static void tag(const Arg
*arg
);
198 static int textnw(const char *text
, unsigned int len
);
199 static void tile(Monitor
*);
200 static void togglebar(const Arg
*arg
);
201 static void togglefloating(const Arg
*arg
);
202 static void toggletag(const Arg
*arg
);
203 static void toggleview(const Arg
*arg
);
204 static void unmanage(Client
*c
);
205 static void unmapnotify(XEvent
*e
);
206 static void updategeom(void);
207 static void updatenumlockmask(void);
208 static void updatesizehints(Client
*c
);
209 static void updatestatus(void);
210 static void updatetitle(Client
*c
);
211 static void updatewmhints(Client
*c
);
212 static void view(const Arg
*arg
);
213 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
214 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
215 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
216 static void zoom(const Arg
*arg
);
218 static void focusmon(const Arg
*arg
);
219 static void tagmon(const Arg
*arg
);
220 #endif /* XINERAMA */
223 static char stext
[256];
225 static int sx
, sy
, sw
, sh
; /* X display screen geometry x, y, width, height */
226 static int bh
, blw
= 0; /* bar geometry */
227 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
228 static unsigned int numlockmask
= 0;
229 static void (*handler
[LASTEvent
]) (XEvent
*) = {
230 [ButtonPress
] = buttonpress
,
231 [ConfigureRequest
] = configurerequest
,
232 [ConfigureNotify
] = configurenotify
,
233 [DestroyNotify
] = destroynotify
,
234 [EnterNotify
] = enternotify
,
237 [KeyPress
] = keypress
,
238 [MappingNotify
] = mappingnotify
,
239 [MapRequest
] = maprequest
,
240 [PropertyNotify
] = propertynotify
,
241 [UnmapNotify
] = unmapnotify
243 static Atom wmatom
[WMLast
], netatom
[NetLast
];
245 static Bool running
= True
;
246 static Client
*clients
= NULL
;
247 static Client
*sel
= NULL
;
248 static Client
*stack
= NULL
;
249 static Cursor cursor
[CurLast
];
252 static Layout
*lt
[] = { NULL
, NULL
};
253 static Monitor
*mon
= NULL
, *selmon
= NULL
;
254 static unsigned int nmons
;
256 /* configuration, allows nested code to access above variables */
259 /* compile-time check if all tags fit into an unsigned int bit array. */
260 struct NumTags
{ char limitexceeded
[sizeof(unsigned int) * 8 < LENGTH(tags
) ? -1 : 1]; };
262 /* function implementations */
264 applyrules(Client
*c
) {
267 XClassHint ch
= { 0 };
270 c
->isfloating
= c
->tags
= 0;
271 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
272 for(i
= 0; i
< LENGTH(rules
); i
++) {
274 if((!r
->title
|| strstr(c
->name
, r
->title
))
275 && (!r
->class || (ch
.res_class
&& strstr(ch
.res_class
, r
->class)))
276 && (!r
->instance
|| (ch
.res_name
&& strstr(ch
.res_name
, r
->instance
)))) {
277 c
->isfloating
= r
->isfloating
;
286 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: tagset
[mon
[c
->mon
].seltags
];
290 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
) {
293 /* set minimum possible */
301 if(*x
+ *w
+ 2 * c
->bw
< sx
)
303 if(*y
+ *h
+ 2 * c
->bw
< sy
)
310 if(resizehints
|| c
->isfloating
) {
311 /* see last two sentences in ICCCM 4.1.2.3 */
312 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
314 if(!baseismin
) { /* temporarily remove base dimensions */
319 /* adjust for aspect limits */
320 if(c
->mina
> 0 && c
->maxa
> 0) {
321 if(c
->maxa
< (float)*w
/ *h
)
323 else if(c
->mina
< (float)*h
/ *w
)
327 if(baseismin
) { /* increment calculation requires this */
332 /* adjust for increment value */
338 /* restore base dimensions */
342 *w
= MAX(*w
, c
->minw
);
343 *h
= MAX(*h
, c
->minh
);
346 *w
= MIN(*w
, c
->maxw
);
349 *h
= MIN(*h
, c
->maxh
);
351 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
359 for(i
= 0; i
< nmons
; i
++) {
360 if(lt
[mon
[i
].sellt
]->arrange
)
361 lt
[mon
[i
].sellt
]->arrange(&mon
[i
]);
373 attachstack(Client
*c
) {
379 buttonpress(XEvent
*e
) {
380 unsigned int i
, x
, click
;
383 XButtonPressedEvent
*ev
= &e
->xbutton
;
386 if(ev
->window
== selmon
->barwin
) {
391 while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
392 if(i
< LENGTH(tags
)) {
396 else if(ev
->x
< x
+ blw
)
398 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
399 click
= ClkStatusText
;
403 else if((c
= getclient(ev
->window
))) {
405 click
= ClkClientWin
;
408 for(i
= 0; i
< LENGTH(buttons
); i
++)
409 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
410 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
411 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
417 xerrorxlib
= XSetErrorHandler(xerrorstart
);
419 /* this causes an error if some other window manager is running */
420 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
423 die("dwm: another window manager is already running\n");
424 XSetErrorHandler(xerror
);
432 Layout foo
= { "", NULL
};
435 lt
[selmon
->sellt
] = &foo
;
439 XFreeFontSet(dpy
, dc
.font
.set
);
441 XFreeFont(dpy
, dc
.font
.xfont
);
442 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
443 XFreePixmap(dpy
, dc
.drawable
);
445 XFreeCursor(dpy
, cursor
[CurNormal
]);
446 XFreeCursor(dpy
, cursor
[CurResize
]);
447 XFreeCursor(dpy
, cursor
[CurMove
]);
448 for(i
= 0; i
< nmons
; i
++)
449 XDestroyWindow(dpy
, mon
[i
].barwin
);
452 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
456 clearurgent(Client
*c
) {
460 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
462 wmh
->flags
&= ~XUrgencyHint
;
463 XSetWMHints(dpy
, c
->win
, wmh
);
468 configure(Client
*c
) {
471 ce
.type
= ConfigureNotify
;
479 ce
.border_width
= c
->bw
;
481 ce
.override_redirect
= False
;
482 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
486 configurenotify(XEvent
*e
) {
488 XConfigureEvent
*ev
= &e
->xconfigure
;
490 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
495 XFreePixmap(dpy
, dc
.drawable
);
496 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
497 for(i
= 0; i
< nmons
; i
++)
498 XMoveResizeWindow(dpy
, mon
[i
].barwin
, mon
[i
].wx
, mon
[i
].by
, mon
[i
].ww
, bh
);
504 configurerequest(XEvent
*e
) {
506 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
509 if((c
= getclient(ev
->window
))) {
510 if(ev
->value_mask
& CWBorderWidth
)
511 c
->bw
= ev
->border_width
;
512 else if(c
->isfloating
|| !lt
[selmon
->sellt
]->arrange
) {
513 if(ev
->value_mask
& CWX
)
515 if(ev
->value_mask
& CWY
)
517 if(ev
->value_mask
& CWWidth
)
519 if(ev
->value_mask
& CWHeight
)
521 if((c
->x
- sx
+ c
->w
) > sw
&& c
->isfloating
)
522 c
->x
= sx
+ (sw
/ 2 - c
->w
/ 2); /* center in x direction */
523 if((c
->y
- sy
+ c
->h
) > sh
&& c
->isfloating
)
524 c
->y
= sy
+ (sh
/ 2 - c
->h
/ 2); /* center in y direction */
525 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
527 if(ISVISIBLE((&mon
[c
->mon
]), c
))
528 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
536 wc
.width
= ev
->width
;
537 wc
.height
= ev
->height
;
538 wc
.border_width
= ev
->border_width
;
539 wc
.sibling
= ev
->above
;
540 wc
.stack_mode
= ev
->detail
;
541 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
547 destroynotify(XEvent
*e
) {
549 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
551 if((c
= getclient(ev
->window
)))
559 for(tc
= &clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
564 detachstack(Client
*c
) {
567 for(tc
= &stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
572 die(const char *errstr
, ...) {
575 va_start(ap
, errstr
);
576 vfprintf(stderr
, errstr
, ap
);
582 drawbar(Monitor
*m
) {
584 unsigned int i
, occ
= 0, urg
= 0;
588 for(c
= clients
; c
; c
= c
->next
) {
589 if(m
== &mon
[c
->mon
]) {
599 dc
.w
= TEXTW(m
->symbol
);
600 drawtext(m
->symbol
, selmon
== m
? dc
.sel
: dc
.norm
, False
);
603 #endif /* XINERAMA */
605 for(i
= 0; i
< LENGTH(tags
); i
++) {
606 dc
.w
= TEXTW(tags
[i
]);
607 col
= tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
608 drawtext(tags
[i
], col
, urg
& 1 << i
);
609 drawsquare(m
== selmon
&& sel
&& sel
->tags
& 1 << i
,
610 occ
& 1 << i
, urg
& 1 << i
, col
);
615 drawtext(lt
[m
->sellt
]->symbol
, dc
.norm
, False
);
627 drawtext(stext
, dc
.norm
, False
);
628 if((dc
.w
= dc
.x
- x
) > bh
) {
631 drawtext(sel
->name
, dc
.sel
, False
);
632 drawsquare(sel
->isfixed
, sel
->isfloating
, False
, dc
.sel
);
635 drawtext(NULL
, dc
.norm
, False
);
641 drawtext(NULL
, dc
.norm
, False
);
643 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
651 for(i
= 0; i
< nmons
; i
++)
656 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
659 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
661 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
662 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
663 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
667 r
.width
= r
.height
= x
+ 1;
668 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
671 r
.width
= r
.height
= x
;
672 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
677 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
679 int i
, x
, y
, h
, len
, olen
;
680 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
682 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
683 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
687 h
= dc
.font
.ascent
+ dc
.font
.descent
;
688 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
690 /* shorten text if necessary */
691 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
694 memcpy(buf
, text
, len
);
696 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
697 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
699 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
701 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
705 enternotify(XEvent
*e
) {
707 XCrossingEvent
*ev
= &e
->xcrossing
;
709 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
711 if((c
= getclient(ev
->window
)))
720 XExposeEvent
*ev
= &e
->xexpose
;
723 for(i
= 0; i
< nmons
; i
++)
724 if(ev
->window
== mon
[i
].barwin
) {
732 if(!c
|| !ISVISIBLE((&mon
[c
->mon
]), c
))
733 for(c
= stack
; c
&& !ISVISIBLE(selmon
, c
); c
= c
->snext
);
734 if(sel
&& sel
!= c
) {
735 grabbuttons(sel
, False
);
736 XSetWindowBorder(dpy
, sel
->win
, dc
.norm
[ColBorder
]);
743 grabbuttons(c
, True
);
744 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
745 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
748 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
751 selmon
= &mon
[c
->mon
];
756 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
757 XFocusChangeEvent
*ev
= &e
->xfocus
;
759 if(sel
&& ev
->window
!= sel
->win
)
760 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
765 focusmon(const Arg
*arg
) {
768 selmon
= &mon
[arg
->ui
];
772 #endif /* XINERAMA */
775 focusstack(const Arg
*arg
) {
776 Client
*c
= NULL
, *i
;
781 for(c
= sel
->next
; c
&& !ISVISIBLE(selmon
, c
); c
= c
->next
);
783 for(c
= clients
; c
&& !ISVISIBLE(selmon
, c
); c
= c
->next
);
786 for(i
= clients
; i
!= sel
; i
= i
->next
)
787 if(ISVISIBLE(selmon
, i
))
790 for(; i
; i
= i
->next
)
791 if(ISVISIBLE(selmon
, i
))
801 getclient(Window w
) {
804 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
809 getcolor(const char *colstr
) {
810 Colormap cmap
= DefaultColormap(dpy
, screen
);
813 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
814 die("error, cannot allocate color '%s'\n", colstr
);
822 unsigned char *p
= NULL
;
823 unsigned long n
, extra
;
826 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
827 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
828 if(status
!= Success
)
837 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
842 if(!text
|| size
== 0)
845 XGetTextProperty(dpy
, w
, &name
, atom
);
848 if(name
.encoding
== XA_STRING
)
849 strncpy(text
, (char *)name
.value
, size
- 1);
851 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
853 strncpy(text
, *list
, size
- 1);
854 XFreeStringList(list
);
857 text
[size
- 1] = '\0';
863 grabbuttons(Client
*c
, Bool focused
) {
867 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
868 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
870 for(i
= 0; i
< LENGTH(buttons
); i
++)
871 if(buttons
[i
].click
== ClkClientWin
)
872 for(j
= 0; j
< LENGTH(modifiers
); j
++)
873 XGrabButton(dpy
, buttons
[i
].button
,
874 buttons
[i
].mask
| modifiers
[j
],
875 c
->win
, False
, BUTTONMASK
,
876 GrabModeAsync
, GrabModeSync
, None
, None
);
878 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
879 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
888 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
891 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
892 for(i
= 0; i
< LENGTH(keys
); i
++) {
893 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
894 for(j
= 0; j
< LENGTH(modifiers
); j
++)
895 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
896 True
, GrabModeAsync
, GrabModeAsync
);
902 initfont(const char *fontstr
) {
903 char *def
, **missing
;
907 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
910 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
911 XFreeStringList(missing
);
914 XFontSetExtents
*font_extents
;
915 XFontStruct
**xfonts
;
917 dc
.font
.ascent
= dc
.font
.descent
= 0;
918 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
919 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
920 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
921 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
922 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
927 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
928 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
929 die("error, cannot load font: '%s'\n", fontstr
);
930 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
931 dc
.font
.descent
= dc
.font
.xfont
->descent
;
933 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
937 isprotodel(Client
*c
) {
942 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
943 for(i
= 0; !ret
&& i
< n
; i
++)
944 if(protocols
[i
] == wmatom
[WMDelete
])
952 keypress(XEvent
*e
) {
958 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
959 for(i
= 0; i
< LENGTH(keys
); i
++)
960 if(keysym
== keys
[i
].keysym
961 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
963 keys
[i
].func(&(keys
[i
].arg
));
967 killclient(const Arg
*arg
) {
972 if(isprotodel(sel
)) {
973 ev
.type
= ClientMessage
;
974 ev
.xclient
.window
= sel
->win
;
975 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
976 ev
.xclient
.format
= 32;
977 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
978 ev
.xclient
.data
.l
[1] = CurrentTime
;
979 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
982 XKillClient(dpy
, sel
->win
);
986 manage(Window w
, XWindowAttributes
*wa
) {
988 Client
*c
, *t
= NULL
;
992 if(!(c
= malloc(sizeof(Client
))))
993 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
996 for(c
->mon
= 0; selmon
!= &mon
[c
->mon
]; c
->mon
++);
1003 c
->oldbw
= wa
->border_width
;
1004 if(c
->w
== sw
&& c
->h
== sh
) {
1010 if(c
->x
+ WIDTH(c
) > sx
+ sw
)
1011 c
->x
= sx
+ sw
- WIDTH(c
);
1012 if(c
->y
+ HEIGHT(c
) > sy
+ sh
)
1013 c
->y
= sy
+ sh
- HEIGHT(c
);
1014 c
->x
= MAX(c
->x
, sx
);
1015 /* only fix client y-offset, if the client center might cover the bar */
1016 c
->y
= MAX(c
->y
, ((selmon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= selmon
->wx
)
1017 && (c
->x
+ (c
->w
/ 2) < selmon
->wx
+ selmon
->ww
)) ? bh
: sy
);
1021 wc
.border_width
= c
->bw
;
1022 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1023 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1024 configure(c
); /* propagates border_width, if size doesn't change */
1026 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1027 grabbuttons(c
, False
);
1029 if(XGetTransientForHint(dpy
, w
, &trans
))
1030 t
= getclient(trans
);
1036 c
->isfloating
= trans
!= None
|| c
->isfixed
;
1038 XRaiseWindow(dpy
, c
->win
);
1041 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1042 XMapWindow(dpy
, c
->win
);
1043 setclientstate(c
, NormalState
);
1048 mappingnotify(XEvent
*e
) {
1049 XMappingEvent
*ev
= &e
->xmapping
;
1051 XRefreshKeyboardMapping(ev
);
1052 if(ev
->request
== MappingKeyboard
)
1057 maprequest(XEvent
*e
) {
1058 static XWindowAttributes wa
;
1059 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1061 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1063 if(wa
.override_redirect
)
1065 if(!getclient(ev
->window
))
1066 manage(ev
->window
, &wa
);
1070 monocle(Monitor
*m
) {
1073 for(c
= nexttiled(m
, clients
); c
; c
= nexttiled(m
, c
->next
))
1074 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
);
1078 movemouse(const Arg
*arg
) {
1079 int x
, y
, ocx
, ocy
, di
, nx
, ny
;
1090 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1091 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1093 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
);
1095 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1097 case ConfigureRequest
:
1100 handler
[ev
.type
](&ev
);
1103 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1104 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1105 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1106 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1107 if(abs(selmon
->wx
- nx
) < snap
)
1109 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1110 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1111 if(abs(selmon
->wy
- ny
) < snap
)
1113 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1114 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1115 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1116 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1117 togglefloating(NULL
);
1119 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1120 resize(c
, nx
, ny
, c
->w
, c
->h
);
1124 while(ev
.type
!= ButtonRelease
);
1125 XUngrabPointer(dpy
, CurrentTime
);
1129 nexttiled(Monitor
*m
, Client
*c
) {
1131 for(; c
&& (c
->isfloating
|| !ISVISIBLE(m
, c
)); c
= c
->next
);
1136 propertynotify(XEvent
*e
) {
1139 XPropertyEvent
*ev
= &e
->xproperty
;
1141 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1143 else if(ev
->state
== PropertyDelete
)
1144 return; /* ignore */
1145 else if((c
= getclient(ev
->window
))) {
1148 case XA_WM_TRANSIENT_FOR
:
1149 XGetTransientForHint(dpy
, c
->win
, &trans
);
1150 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1153 case XA_WM_NORMAL_HINTS
:
1161 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1170 quit(const Arg
*arg
) {
1175 resize(Client
*c
, int x
, int y
, int w
, int h
) {
1178 if(applysizehints(c
, &x
, &y
, &w
, &h
)) {
1181 c
->w
= wc
.width
= w
;
1182 c
->h
= wc
.height
= h
;
1183 wc
.border_width
= c
->bw
;
1184 XConfigureWindow(dpy
, c
->win
,
1185 CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1192 resizemouse(const Arg
*arg
) {
1203 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1204 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1206 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1208 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1210 case ConfigureRequest
:
1213 handler
[ev
.type
](&ev
);
1216 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1217 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1219 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1220 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
) {
1221 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1222 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1223 togglefloating(NULL
);
1225 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1226 resize(c
, c
->x
, c
->y
, nw
, nh
);
1230 while(ev
.type
!= ButtonRelease
);
1231 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1232 XUngrabPointer(dpy
, CurrentTime
);
1233 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1237 restack(Monitor
*m
) {
1245 if(m
== selmon
&& (sel
->isfloating
|| !lt
[m
->sellt
]->arrange
))
1246 XRaiseWindow(dpy
, sel
->win
);
1247 if(lt
[m
->sellt
]->arrange
) {
1248 wc
.stack_mode
= Below
;
1249 wc
.sibling
= m
->barwin
;
1250 for(c
= stack
; c
; c
= c
->snext
)
1251 if(!c
->isfloating
&& ISVISIBLE(m
, c
)) {
1252 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1253 wc
.sibling
= c
->win
;
1257 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1264 /* main event loop */
1266 while(running
&& !XNextEvent(dpy
, &ev
)) {
1267 if(handler
[ev
.type
])
1268 (handler
[ev
.type
])(&ev
); /* call handler */
1274 unsigned int i
, num
;
1275 Window d1
, d2
, *wins
= NULL
;
1276 XWindowAttributes wa
;
1278 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1279 for(i
= 0; i
< num
; i
++) {
1280 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1281 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1283 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1284 manage(wins
[i
], &wa
);
1286 for(i
= 0; i
< num
; i
++) { /* now the transients */
1287 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1289 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1290 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1291 manage(wins
[i
], &wa
);
1299 setclientstate(Client
*c
, long state
) {
1300 long data
[] = {state
, None
};
1302 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1303 PropModeReplace
, (unsigned char *)data
, 2);
1307 setlayout(const Arg
*arg
) {
1308 if(!arg
|| !arg
->v
|| arg
->v
!= lt
[selmon
->sellt
])
1311 lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1318 /* arg > 1.0 will set mfact absolutly */
1320 setmfact(const Arg
*arg
) {
1323 if(!arg
|| !lt
[selmon
->sellt
]->arrange
)
1325 f
= arg
->f
< 1.0 ? arg
->f
+ mfact
: arg
->f
- 1.0;
1326 if(f
< 0.1 || f
> 0.9)
1336 XSetWindowAttributes wa
;
1339 screen
= DefaultScreen(dpy
);
1340 root
= RootWindow(dpy
, screen
);
1344 sw
= DisplayWidth(dpy
, screen
);
1345 sh
= DisplayHeight(dpy
, screen
);
1346 bh
= dc
.h
= dc
.font
.height
+ 2;
1347 lt
[0] = &layouts
[0];
1348 lt
[1] = &layouts
[1 % LENGTH(layouts
)];
1352 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1353 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1354 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1355 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1356 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1359 wa
.cursor
= cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1360 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1361 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1363 /* init appearance */
1364 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1365 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1366 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1367 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1368 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1369 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1370 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1371 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1372 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1374 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1377 wa
.override_redirect
= True
;
1378 wa
.background_pixmap
= ParentRelative
;
1379 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1380 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1381 w
= TEXTW(layouts
[i
].symbol
);
1385 for(i
= 0; i
< nmons
; i
++) {
1386 mon
[i
].barwin
= XCreateWindow(dpy
, root
, mon
[i
].wx
, mon
[i
].by
, mon
[i
].ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1388 CopyFromParent
, DefaultVisual(dpy
, screen
),
1389 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1390 XDefineCursor(dpy
, mon
[i
].barwin
, cursor
[CurNormal
]);
1391 XMapRaised(dpy
, mon
[i
].barwin
);
1395 /* EWMH support per view */
1396 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1397 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1399 /* select for events */
1400 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1401 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1402 |PropertyChangeMask
;
1403 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1404 XSelectInput(dpy
, root
, wa
.event_mask
);
1410 showhide(Client
*c
) {
1413 if(ISVISIBLE((&mon
[c
->mon
]), c
)) { /* show clients top down */
1414 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1415 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1416 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1419 else { /* hide clients bottom up */
1421 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1427 sigchld(int signal
) {
1428 while(0 < waitpid(-1, NULL
, WNOHANG
));
1432 spawn(const Arg
*arg
) {
1433 signal(SIGCHLD
, sigchld
);
1436 close(ConnectionNumber(dpy
));
1438 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1439 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1446 tag(const Arg
*arg
) {
1447 if(sel
&& arg
->ui
& TAGMASK
) {
1448 sel
->tags
= arg
->ui
& TAGMASK
;
1455 tagmon(const Arg
*arg
) {
1456 if(!sel
|| arg
->ui
>= nmons
)
1461 #endif /* XINERAMA */
1464 textnw(const char *text
, unsigned int len
) {
1468 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1471 return XTextWidth(dc
.font
.xfont
, text
, len
);
1480 for(n
= 0, c
= nexttiled(m
, clients
); c
; c
= nexttiled(m
, c
->next
), n
++);
1485 c
= nexttiled(m
, clients
);
1487 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
);
1493 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1495 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1500 for(i
= 0, c
= nexttiled(m
, c
->next
); c
; c
= nexttiled(m
, c
->next
), i
++) {
1501 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1502 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
));
1504 y
= c
->y
+ HEIGHT(c
);
1509 togglebar(const Arg
*arg
) {
1510 selmon
->showbar
= !selmon
->showbar
;
1512 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1517 togglefloating(const Arg
*arg
) {
1520 sel
->isfloating
= !sel
->isfloating
|| sel
->isfixed
;
1522 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
);
1527 toggletag(const Arg
*arg
) {
1533 mask
= sel
->tags
^ (arg
->ui
& TAGMASK
);
1541 toggleview(const Arg
*arg
) {
1542 unsigned int mask
= tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1545 tagset
[selmon
->seltags
] = mask
;
1551 unmanage(Client
*c
) {
1554 wc
.border_width
= c
->oldbw
;
1555 /* The server grab construct avoids race conditions. */
1557 XSetErrorHandler(xerrordummy
);
1558 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1563 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1564 setclientstate(c
, WithdrawnState
);
1567 XSetErrorHandler(xerror
);
1573 unmapnotify(XEvent
*e
) {
1575 XUnmapEvent
*ev
= &e
->xunmap
;
1577 if((c
= getclient(ev
->window
)))
1585 unsigned int dui
, i
= 0;
1589 XineramaScreenInfo
*info
= NULL
;
1591 /* window area geometry */
1592 if(XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
1593 nmons
= (unsigned int)n
;
1594 for(c
= clients
; c
; c
= c
->next
)
1597 if(!(mon
= (Monitor
*)realloc(mon
, sizeof(Monitor
) * nmons
)))
1598 die("fatal: could not realloc() %u bytes\n", sizeof(Monitor
) * nmons
);
1599 pquery
= XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
);
1600 for(i
= 0; i
< nmons
; i
++) {
1601 /* TODO: consider re-using XineramaScreenInfo */
1602 mon
[i
].symbol
[0] = '[';
1603 mon
[i
].symbol
[1] = '0' + info
[i
].screen_number
;
1604 mon
[i
].symbol
[2] = ']';
1605 mon
[i
].symbol
[3] = 0;
1606 mon
[i
].showbar
= showbar
;
1607 mon
[i
].topbar
= topbar
;
1608 mon
[i
].wx
= info
[i
].x_org
;
1609 mon
[i
].wy
= mon
[i
].showbar
&& mon
[i
].topbar
? info
[i
].y_org
+ bh
: info
[i
].y_org
;
1610 mon
[i
].ww
= info
[i
].width
;
1611 mon
[i
].wh
= mon
[i
].showbar
? info
[i
].height
- bh
: info
[i
].height
;
1615 mon
[i
].by
= mon
[i
].topbar
? info
[i
].y_org
: mon
[i
].wy
+ mon
[i
].wh
;
1618 if(pquery
&& INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
1624 #endif /* XINERAMA */
1627 if(!(mon
= (Monitor
*)realloc(mon
, sizeof(Monitor
))))
1628 die("fatal: could not realloc() %u bytes\n", sizeof(Monitor
));
1630 mon
[0].symbol
[0] = '[';
1631 mon
[0].symbol
[1] = '0';
1632 mon
[0].symbol
[2] = ']';
1633 mon
[0].symbol
[3] = 0;
1634 mon
[0].showbar
= showbar
;
1635 mon
[0].topbar
= topbar
;
1637 mon
[0].wy
= mon
[0].showbar
&& mon
[0].topbar
? sy
+ bh
: sy
;
1639 mon
[0].wh
= mon
[0].showbar
? sh
- bh
: sh
;
1643 mon
[0].by
= mon
[0].topbar
? sy
: mon
[0].wy
+ mon
[0].wh
;
1650 updatenumlockmask(void) {
1652 XModifierKeymap
*modmap
;
1655 modmap
= XGetModifierMapping(dpy
);
1656 for(i
= 0; i
< 8; i
++)
1657 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1658 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1659 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1660 numlockmask
= (1 << i
);
1661 XFreeModifiermap(modmap
);
1665 updatesizehints(Client
*c
) {
1669 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1670 /* size is uninitialized, ensure that size.flags aren't used */
1672 if(size
.flags
& PBaseSize
) {
1673 c
->basew
= size
.base_width
;
1674 c
->baseh
= size
.base_height
;
1676 else if(size
.flags
& PMinSize
) {
1677 c
->basew
= size
.min_width
;
1678 c
->baseh
= size
.min_height
;
1681 c
->basew
= c
->baseh
= 0;
1682 if(size
.flags
& PResizeInc
) {
1683 c
->incw
= size
.width_inc
;
1684 c
->inch
= size
.height_inc
;
1687 c
->incw
= c
->inch
= 0;
1688 if(size
.flags
& PMaxSize
) {
1689 c
->maxw
= size
.max_width
;
1690 c
->maxh
= size
.max_height
;
1693 c
->maxw
= c
->maxh
= 0;
1694 if(size
.flags
& PMinSize
) {
1695 c
->minw
= size
.min_width
;
1696 c
->minh
= size
.min_height
;
1698 else if(size
.flags
& PBaseSize
) {
1699 c
->minw
= size
.base_width
;
1700 c
->minh
= size
.base_height
;
1703 c
->minw
= c
->minh
= 0;
1704 if(size
.flags
& PAspect
) {
1705 c
->mina
= (float)size
.min_aspect
.y
/ (float)size
.min_aspect
.x
;
1706 c
->maxa
= (float)size
.max_aspect
.x
/ (float)size
.max_aspect
.y
;
1709 c
->maxa
= c
->mina
= 0.0;
1710 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1711 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1715 updatetitle(Client
*c
) {
1716 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1717 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1722 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1723 strcpy(stext
, "dwm-"VERSION
);
1728 updatewmhints(Client
*c
) {
1731 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1732 if(c
== sel
&& wmh
->flags
& XUrgencyHint
) {
1733 wmh
->flags
&= ~XUrgencyHint
;
1734 XSetWMHints(dpy
, c
->win
, wmh
);
1737 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1744 view(const Arg
*arg
) {
1745 if((arg
->ui
& TAGMASK
) == tagset
[selmon
->seltags
])
1747 selmon
->seltags
^= 1; /* toggle sel tagset */
1748 if(arg
->ui
& TAGMASK
)
1749 tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1753 /* There's no way to check accesses to destroyed windows, thus those cases are
1754 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1755 * default error handler, which may call exit. */
1757 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1758 if(ee
->error_code
== BadWindow
1759 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1760 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1761 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1762 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1763 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1764 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1765 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1766 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1768 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1769 ee
->request_code
, ee
->error_code
);
1770 return xerrorxlib(dpy
, ee
); /* may call exit */
1774 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1778 /* Startup Error handler to check if another window manager
1779 * is already running. */
1781 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1787 zoom(const Arg
*arg
) {
1790 if(!lt
[selmon
->sellt
]->arrange
|| lt
[selmon
->sellt
]->arrange
== monocle
|| (sel
&& sel
->isfloating
))
1792 if(c
== nexttiled(selmon
, clients
))
1793 if(!c
|| !(c
= nexttiled(selmon
, c
->next
)))
1802 main(int argc
, char *argv
[]) {
1803 if(argc
== 2 && !strcmp("-v", argv
[1]))
1804 die("dwm-"VERSION
", © 2006-2009 dwm engineers, see LICENSE for details\n");
1806 die("usage: dwm [-v]\n");
1808 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1809 fputs("warning: no locale support\n", stderr
);
1811 if(!(dpy
= XOpenDisplay(NULL
)))
1812 die("dwm: cannot open display\n");