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 * Calls to fetch an X event from the event queue are blocking. Due reading
10 * status text from standard input, a select()-driven main loop has been
11 * implemented which selects for reads on the X connection and STDIN_FILENO to
12 * handle all data smoothly. The event handlers of dwm are organized in an
13 * array which is accessed whenever a new event has been fetched. This allows
14 * event dispatching in O(1) time.
16 * Each child of the root window is called a client, except windows which have
17 * set the override_redirect flag. Clients are organized in a global
18 * doubly-linked client list, the focus history is remembered through a global
19 * stack list. Each client contains an array of Bools of the same size as the
20 * global tags array to indicate the tags of a client.
22 * Keys and tagging rules are organized as arrays and defined in config.h.
24 * To understand everything else, start reading main().
33 #include <sys/select.h>
34 #include <sys/types.h>
37 #include <X11/cursorfont.h>
38 #include <X11/keysym.h>
39 #include <X11/Xatom.h>
41 #include <X11/Xproto.h>
42 #include <X11/Xutil.h>
44 #include <X11/extensions/Xinerama.h>
48 #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
49 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
50 #define LENGTH(x) (sizeof x / sizeof x[0])
52 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
56 enum { BarTop
, BarBot
, BarOff
}; /* bar position */
57 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
58 enum { ColBorder
, ColFG
, ColBG
, ColLast
}; /* color */
59 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
60 enum { WMProtocols
, WMDelete
, WMName
, WMState
, WMLast
};/* default atoms */
63 typedef struct Client Client
;
67 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
68 int minax
, maxax
, minay
, maxay
;
70 unsigned int border
, oldborder
;
71 Bool isbanned
, isfixed
, isfloating
, isurgent
;
82 unsigned long norm
[ColLast
];
83 unsigned long sel
[ColLast
];
93 } DC
; /* draw context */
98 void (*func
)(const char *arg
);
104 void (*arrange
)(void);
123 int sx
, sy
, sw
, sh
, wax
, way
, wah
, waw
;
131 /* function declarations */
132 void applyrules(Client
*c
);
134 void attach(Client
*c
);
135 void attachstack(Client
*c
);
137 void buttonpress(XEvent
*e
);
138 void checkotherwm(void);
140 void compileregs(void);
141 void configure(Client
*c
);
142 void configurenotify(XEvent
*e
);
143 void configurerequest(XEvent
*e
);
144 void destroynotify(XEvent
*e
);
145 void detach(Client
*c
);
146 void detachstack(Client
*c
);
148 void drawsquare(Monitor
*, Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
149 void drawtext(Monitor
*, const char *text
, unsigned long col
[ColLast
], Bool invert
);
150 void *emallocz(unsigned int size
);
151 void enternotify(XEvent
*e
);
152 void eprint(const char *errstr
, ...);
153 void expose(XEvent
*e
);
154 void floating(void); /* default floating layout */
155 void focus(Client
*c
);
156 void focusin(XEvent
*e
);
157 void focusnext(const char *arg
);
158 void focusprev(const char *arg
);
159 Client
*getclient(Window w
);
160 unsigned long getcolor(const char *colstr
, int screen
);
161 long getstate(Window w
);
162 Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
163 void grabbuttons(Client
*c
, Bool focused
);
165 unsigned int idxoftag(const char *tag
);
166 void initfont(Monitor
*, const char *fontstr
);
167 Bool
isoccupied(Monitor
*m
, unsigned int t
);
168 Bool
isprotodel(Client
*c
);
169 Bool
isurgent(int monitor
, unsigned int t
);
170 Bool
isvisible(Client
*c
, int monitor
);
171 void keypress(XEvent
*e
);
172 void killclient(const char *arg
);
173 void manage(Window w
, XWindowAttributes
*wa
);
174 void mappingnotify(XEvent
*e
);
175 void maprequest(XEvent
*e
);
176 void movemouse(Client
*c
);
177 Client
*nexttiled(Client
*c
, int monitor
);
178 void propertynotify(XEvent
*e
);
179 void quit(const char *arg
);
180 void reapply(const char *arg
);
181 void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
);
182 void resizemouse(Client
*c
);
186 void setclientstate(Client
*c
, long state
);
187 void setlayout(const char *arg
);
188 void setmwfact(const char *arg
);
190 void spawn(const char *arg
);
191 void tag(const char *arg
);
192 unsigned int textnw(Monitor
*, const char *text
, unsigned int len
);
193 unsigned int textw(Monitor
*, const char *text
);
195 void togglebar(const char *arg
);
196 void togglefloating(const char *arg
);
197 void toggletag(const char *arg
);
198 void toggleview(const char *arg
);
199 void unban(Client
*c
);
200 void unmanage(Client
*c
);
201 void unmapnotify(XEvent
*e
);
202 void updatebarpos(Monitor
*m
);
203 void updatesizehints(Client
*c
);
204 void updatetitle(Client
*c
);
205 void updatewmhints(Client
*c
);
206 void view(const char *arg
);
207 void viewprevtag(const char *arg
); /* views previous selected tags */
208 int xerror(Display
*dpy
, XErrorEvent
*ee
);
209 int xerrordummy(Display
*dsply
, XErrorEvent
*ee
);
210 int xerrorstart(Display
*dsply
, XErrorEvent
*ee
);
211 void zoom(const char *arg
);
213 void movetomonitor(const char *arg
);
214 void selectmonitor(const char *arg
);
219 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
220 unsigned int bh
, bpos
;
221 unsigned int blw
= 0;
222 unsigned int numlockmask
= 0;
223 void (*handler
[LASTEvent
]) (XEvent
*) = {
224 [ButtonPress
] = buttonpress
,
225 [ConfigureRequest
] = configurerequest
,
226 [ConfigureNotify
] = configurenotify
,
227 [DestroyNotify
] = destroynotify
,
228 [EnterNotify
] = enternotify
,
231 [KeyPress
] = keypress
,
232 [MappingNotify
] = mappingnotify
,
233 [MapRequest
] = maprequest
,
234 [PropertyNotify
] = propertynotify
,
235 [UnmapNotify
] = unmapnotify
237 Atom wmatom
[WMLast
], netatom
[NetLast
];
238 Bool isxinerama
= False
;
239 Bool domwfact
= True
;
241 Bool otherwm
, readin
;
243 Client
*clients
= NULL
;
245 Client
*stack
= NULL
;
246 Cursor cursor
[CurLast
];
253 /* configuration, allows nested code to access above variables */
256 //Bool prevtags[LENGTH(tags)];
258 /* function implementations */
260 applyrules(Client
*c
) {
261 static char buf
[512];
264 Bool matched_tag
= False
;
265 Bool matched_monitor
= False
;
266 XClassHint ch
= { 0 };
269 XGetClassHint(dpy
, c
->win
, &ch
);
270 snprintf(buf
, sizeof buf
, "%s:%s:%s",
271 ch
.res_class
? ch
.res_class
: "",
272 ch
.res_name
? ch
.res_name
: "", c
->name
);
273 for(i
= 0; i
< LENGTH(rules
); i
++)
274 if(regs
[i
].propregex
&& !regexec(regs
[i
].propregex
, buf
, 1, &tmp
, 0)) {
275 if (rules
[i
].monitor
>= 0 && rules
[i
].monitor
< mcount
) {
276 matched_monitor
= True
;
277 c
->monitor
= rules
[i
].monitor
;
280 c
->isfloating
= rules
[i
].isfloating
;
281 for(j
= 0; regs
[i
].tagregex
&& j
< LENGTH(tags
); j
++) {
282 if(!regexec(regs
[i
].tagregex
, tags
[j
], 1, &tmp
, 0)) {
293 memcpy(c
->tags
, monitors
[monitorat()].seltags
, sizeof initags
);
294 if (!matched_monitor
)
295 c
->monitor
= monitorat();
302 for(c
= clients
; c
; c
= c
->next
)
303 if(isvisible(c
, c
->monitor
))
308 monitors
[selmonitor
].layout
->arrange();
322 attachstack(Client
*c
) {
331 XMoveWindow(dpy
, c
->win
, c
->x
+ 3 * monitors
[c
->monitor
].sw
, c
->y
);
336 buttonpress(XEvent
*e
) {
339 XButtonPressedEvent
*ev
= &e
->xbutton
;
341 Monitor
*m
= &monitors
[monitorat()];
343 if(ev
->window
== m
->barwin
) {
345 for(i
= 0; i
< LENGTH(tags
); i
++) {
346 x
+= textw(m
, tags
[i
]);
348 if(ev
->button
== Button1
) {
349 if(ev
->state
& MODKEY
)
354 else if(ev
->button
== Button3
) {
355 if(ev
->state
& MODKEY
)
363 if((ev
->x
< x
+ blw
) && ev
->button
== Button1
)
366 else if((c
= getclient(ev
->window
))) {
368 if(CLEANMASK(ev
->state
) != MODKEY
)
370 if(ev
->button
== Button1
) {
374 else if(ev
->button
== Button2
) {
375 if((floating
!= m
->layout
->arrange
) && c
->isfloating
)
376 togglefloating(NULL
);
380 else if(ev
->button
== Button3
&& !c
->isfixed
) {
390 XSetErrorHandler(xerrorstart
);
392 /* this causes an error if some other window manager is running */
393 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
396 eprint("dwm: another window manager is already running\n");
398 XSetErrorHandler(NULL
);
399 xerrorxlib
= XSetErrorHandler(xerror
);
411 for(i
= 0; i
< mcount
; i
++) {
412 Monitor
*m
= &monitors
[i
];
414 XFreeFontSet(dpy
, m
->dc
.font
.set
);
416 XFreeFont(dpy
, m
->dc
.font
.xfont
);
417 XUngrabKey(dpy
, AnyKey
, AnyModifier
, m
->root
);
418 XFreePixmap(dpy
, m
->dc
.drawable
);
419 XFreeGC(dpy
, m
->dc
.gc
);
420 XDestroyWindow(dpy
, m
->barwin
);
421 XFreeCursor(dpy
, cursor
[CurNormal
]);
422 XFreeCursor(dpy
, cursor
[CurResize
]);
423 XFreeCursor(dpy
, cursor
[CurMove
]);
424 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
436 regs
= emallocz(LENGTH(rules
) * sizeof(Regs
));
437 for(i
= 0; i
< LENGTH(rules
); i
++) {
439 reg
= emallocz(sizeof(regex_t
));
440 if(regcomp(reg
, rules
[i
].prop
, REG_EXTENDED
))
443 regs
[i
].propregex
= reg
;
446 reg
= emallocz(sizeof(regex_t
));
447 if(regcomp(reg
, rules
[i
].tags
, REG_EXTENDED
))
450 regs
[i
].tagregex
= reg
;
456 configure(Client
*c
) {
459 ce
.type
= ConfigureNotify
;
467 ce
.border_width
= c
->border
;
469 ce
.override_redirect
= False
;
470 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
474 configurenotify(XEvent
*e
) {
475 XConfigureEvent
*ev
= &e
->xconfigure
;
476 Monitor
*m
= &monitors
[selmonitor
];
478 if(ev
->window
== m
->root
&& (ev
->width
!= m
->sw
|| ev
->height
!= m
->sh
)) {
481 XFreePixmap(dpy
, dc
.drawable
);
482 dc
.drawable
= XCreatePixmap(dpy
, m
->root
, m
->sw
, bh
, DefaultDepth(dpy
, m
->screen
));
483 XResizeWindow(dpy
, m
->barwin
, m
->sw
, bh
);
490 configurerequest(XEvent
*e
) {
492 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
495 if((c
= getclient(ev
->window
))) {
496 Monitor
*m
= &monitors
[c
->monitor
];
497 if(ev
->value_mask
& CWBorderWidth
)
498 c
->border
= ev
->border_width
;
499 if(c
->isfixed
|| c
->isfloating
|| (floating
== m
->layout
->arrange
)) {
500 if(ev
->value_mask
& CWX
)
502 if(ev
->value_mask
& CWY
)
504 if(ev
->value_mask
& CWWidth
)
506 if(ev
->value_mask
& CWHeight
)
508 if((c
->x
- m
->sx
+ c
->w
) > m
->sw
&& c
->isfloating
)
509 c
->x
= m
->sx
+ (m
->sw
/ 2 - c
->w
/ 2); /* center in x direction */
510 if((c
->y
- m
->sy
+ c
->h
) > m
->sh
&& c
->isfloating
)
511 c
->y
= m
->sy
+ (m
->sh
/ 2 - c
->h
/ 2); /* center in y direction */
512 if((ev
->value_mask
& (CWX
| CWY
))
513 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
515 if(isvisible(c
, monitorat()))
516 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
524 wc
.width
= ev
->width
;
525 wc
.height
= ev
->height
;
526 wc
.border_width
= ev
->border_width
;
527 wc
.sibling
= ev
->above
;
528 wc
.stack_mode
= ev
->detail
;
529 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
535 destroynotify(XEvent
*e
) {
537 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
539 if((c
= getclient(ev
->window
)))
546 c
->prev
->next
= c
->next
;
548 c
->next
->prev
= c
->prev
;
551 c
->next
= c
->prev
= NULL
;
555 detachstack(Client
*c
) {
558 for(tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
566 for(i
= 0; i
< mcount
; i
++) {
567 Monitor
*m
= &monitors
[i
];
569 for(j
= 0; j
< LENGTH(tags
); j
++) {
570 m
->dc
.w
= textw(m
, tags
[j
]);
572 drawtext(m
, tags
[j
], m
->dc
.sel
, isurgent(i
, j
));
573 drawsquare(m
, sel
&& sel
->tags
[j
] && sel
->monitor
== selmonitor
,
574 isoccupied(m
, j
), isurgent(i
, j
), m
->dc
.sel
);
577 drawtext(m
, tags
[j
], m
->dc
.norm
, isurgent(i
, j
));
578 drawsquare(m
, sel
&& sel
->tags
[j
] && sel
->monitor
== selmonitor
,
579 isoccupied(m
, j
), isurgent(i
, j
), m
->dc
.norm
);
584 drawtext(m
, m
->layout
->symbol
, m
->dc
.norm
, False
);
585 x
= m
->dc
.x
+ m
->dc
.w
;
586 m
->dc
.w
= textw(m
, stext
);
587 m
->dc
.x
= m
->sw
- m
->dc
.w
;
592 drawtext(m
, stext
, m
->dc
.norm
, False
);
593 if((m
->dc
.w
= m
->dc
.x
- x
) > bh
) {
595 if(sel
&& sel
->monitor
== selmonitor
) {
596 drawtext(m
, sel
->name
, m
->dc
.sel
, False
);
597 drawsquare(m
, False
, sel
->isfloating
, False
, m
->dc
.sel
);
600 drawtext(m
, NULL
, m
->dc
.norm
, False
);
602 XCopyArea(dpy
, m
->dc
.drawable
, m
->barwin
, m
->dc
.gc
, 0, 0, m
->sw
, bh
, 0, 0);
608 drawsquare(Monitor
*m
, Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
611 XRectangle r
= { m
->dc
.x
, m
->dc
.y
, m
->dc
.w
, m
->dc
.h
};
613 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
614 XChangeGC(dpy
, m
->dc
.gc
, GCForeground
, &gcv
);
615 x
= (m
->dc
.font
.ascent
+ m
->dc
.font
.descent
+ 2) / 4;
619 r
.width
= r
.height
= x
+ 1;
620 XFillRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
623 r
.width
= r
.height
= x
;
624 XDrawRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
629 drawtext(Monitor
*m
, const char *text
, unsigned long col
[ColLast
], Bool invert
) {
631 static char buf
[256];
632 unsigned int len
, olen
;
633 XRectangle r
= { m
->dc
.x
, m
->dc
.y
, m
->dc
.w
, m
->dc
.h
};
635 XSetForeground(dpy
, m
->dc
.gc
, col
[invert
? ColFG
: ColBG
]);
636 XFillRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
640 olen
= len
= strlen(text
);
641 if(len
>= sizeof buf
)
642 len
= sizeof buf
- 1;
643 memcpy(buf
, text
, len
);
645 h
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
646 y
= m
->dc
.y
+ (m
->dc
.h
/ 2) - (h
/ 2) + m
->dc
.font
.ascent
;
647 x
= m
->dc
.x
+ (h
/ 2);
648 /* shorten text if necessary */
649 while(len
&& (w
= textnw(m
, buf
, len
)) > m
->dc
.w
- h
)
660 return; /* too long */
661 XSetForeground(dpy
, m
->dc
.gc
, col
[invert
? ColBG
: ColFG
]);
663 XmbDrawString(dpy
, m
->dc
.drawable
, m
->dc
.font
.set
, m
->dc
.gc
, x
, y
, buf
, len
);
665 XDrawString(dpy
, m
->dc
.drawable
, m
->dc
.gc
, x
, y
, buf
, len
);
669 emallocz(unsigned int size
) {
670 void *res
= calloc(1, size
);
673 eprint("fatal: could not malloc() %u bytes\n", size
);
678 enternotify(XEvent
*e
) {
680 XCrossingEvent
*ev
= &e
->xcrossing
;
682 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
);
684 if((c
= getclient(ev
->window
)))
687 selmonitor
= monitorat();
688 fprintf(stderr
, "updating selmonitor %d\n", selmonitor
);
694 eprint(const char *errstr
, ...) {
697 va_start(ap
, errstr
);
698 vfprintf(stderr
, errstr
, ap
);
705 XExposeEvent
*ev
= &e
->xexpose
;
708 if(ev
->window
== monitors
[selmonitor
].barwin
)
714 floating(void) { /* default floating layout */
717 domwfact
= dozoom
= False
;
718 for(c
= clients
; c
; c
= c
->next
)
719 if(isvisible(c
, selmonitor
))
720 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
728 selmonitor
= c
->monitor
;
729 m
= &monitors
[selmonitor
];
730 if(!c
|| (c
&& !isvisible(c
, selmonitor
)))
731 for(c
= stack
; c
&& !isvisible(c
, c
->monitor
); c
= c
->snext
);
732 if(sel
&& sel
!= c
) {
733 grabbuttons(sel
, False
);
734 XSetWindowBorder(dpy
, sel
->win
, monitors
[sel
->monitor
].dc
.norm
[ColBorder
]);
739 grabbuttons(c
, True
);
744 XSetWindowBorder(dpy
, c
->win
, m
->dc
.sel
[ColBorder
]);
745 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
746 selmonitor
= c
->monitor
;
749 XSetInputFocus(dpy
, m
->root
, RevertToPointerRoot
, CurrentTime
);
754 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
755 XFocusChangeEvent
*ev
= &e
->xfocus
;
757 if(sel
&& ev
->window
!= sel
->win
)
758 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
762 focusnext(const char *arg
) {
767 for(c
= sel
->next
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
769 for(c
= clients
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
777 focusprev(const char *arg
) {
782 for(c
= sel
->prev
; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
784 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
785 for(; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
794 getclient(Window w
) {
797 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
802 getcolor(const char *colstr
, int screen
) {
803 Colormap cmap
= DefaultColormap(dpy
, screen
);
806 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
807 eprint("error, cannot allocate color '%s'\n", colstr
);
815 unsigned char *p
= NULL
;
816 unsigned long n
, extra
;
819 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
820 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
821 if(status
!= Success
)
830 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
835 if(!text
|| size
== 0)
838 XGetTextProperty(dpy
, w
, &name
, atom
);
841 if(name
.encoding
== XA_STRING
)
842 strncpy(text
, (char *)name
.value
, size
- 1);
844 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
846 strncpy(text
, *list
, size
- 1);
847 XFreeStringList(list
);
850 text
[size
- 1] = '\0';
856 grabbuttons(Client
*c
, Bool focused
) {
857 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
860 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
861 GrabModeAsync
, GrabModeSync
, None
, None
);
862 XGrabButton(dpy
, Button1
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
863 GrabModeAsync
, GrabModeSync
, None
, None
);
864 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
865 GrabModeAsync
, GrabModeSync
, None
, None
);
866 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
867 GrabModeAsync
, GrabModeSync
, None
, None
);
869 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
870 GrabModeAsync
, GrabModeSync
, None
, None
);
871 XGrabButton(dpy
, Button2
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
872 GrabModeAsync
, GrabModeSync
, None
, None
);
873 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
874 GrabModeAsync
, GrabModeSync
, None
, None
);
875 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
876 GrabModeAsync
, GrabModeSync
, None
, None
);
878 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
879 GrabModeAsync
, GrabModeSync
, None
, None
);
880 XGrabButton(dpy
, Button3
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
881 GrabModeAsync
, GrabModeSync
, None
, None
);
882 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
883 GrabModeAsync
, GrabModeSync
, None
, None
);
884 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
885 GrabModeAsync
, GrabModeSync
, None
, None
);
888 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
889 GrabModeAsync
, GrabModeSync
, None
, None
);
896 XModifierKeymap
*modmap
;
898 /* init modifier map */
899 modmap
= XGetModifierMapping(dpy
);
900 for(i
= 0; i
< 8; i
++)
901 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
902 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
903 numlockmask
= (1 << i
);
905 XFreeModifiermap(modmap
);
907 for(i
= 0; i
< mcount
; i
++) {
908 Monitor
*m
= &monitors
[i
];
909 XUngrabKey(dpy
, AnyKey
, AnyModifier
, m
->root
);
910 for(j
= 0; j
< LENGTH(keys
); j
++) {
911 code
= XKeysymToKeycode(dpy
, keys
[j
].keysym
);
912 XGrabKey(dpy
, code
, keys
[j
].mod
, m
->root
, True
,
913 GrabModeAsync
, GrabModeAsync
);
914 XGrabKey(dpy
, code
, keys
[j
].mod
| LockMask
, m
->root
, True
,
915 GrabModeAsync
, GrabModeAsync
);
916 XGrabKey(dpy
, code
, keys
[j
].mod
| numlockmask
, m
->root
, True
,
917 GrabModeAsync
, GrabModeAsync
);
918 XGrabKey(dpy
, code
, keys
[j
].mod
| numlockmask
| LockMask
, m
->root
, True
,
919 GrabModeAsync
, GrabModeAsync
);
925 idxoftag(const char *tag
) {
928 for(i
= 0; (i
< LENGTH(tags
)) && (tags
[i
] != tag
); i
++);
929 return (i
< LENGTH(tags
)) ? i
: 0;
933 initfont(Monitor
*m
, const char *fontstr
) {
934 char *def
, **missing
;
939 XFreeFontSet(dpy
, m
->dc
.font
.set
);
940 m
->dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
943 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
944 XFreeStringList(missing
);
947 XFontSetExtents
*font_extents
;
948 XFontStruct
**xfonts
;
950 m
->dc
.font
.ascent
= m
->dc
.font
.descent
= 0;
951 font_extents
= XExtentsOfFontSet(m
->dc
.font
.set
);
952 n
= XFontsOfFontSet(m
->dc
.font
.set
, &xfonts
, &font_names
);
953 for(i
= 0, m
->dc
.font
.ascent
= 0, m
->dc
.font
.descent
= 0; i
< n
; i
++) {
954 if(m
->dc
.font
.ascent
< (*xfonts
)->ascent
)
955 m
->dc
.font
.ascent
= (*xfonts
)->ascent
;
956 if(m
->dc
.font
.descent
< (*xfonts
)->descent
)
957 m
->dc
.font
.descent
= (*xfonts
)->descent
;
963 XFreeFont(dpy
, m
->dc
.font
.xfont
);
964 m
->dc
.font
.xfont
= NULL
;
965 if(!(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
966 && !(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
967 eprint("error, cannot load font: '%s'\n", fontstr
);
968 m
->dc
.font
.ascent
= m
->dc
.font
.xfont
->ascent
;
969 m
->dc
.font
.descent
= m
->dc
.font
.xfont
->descent
;
971 m
->dc
.font
.height
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
975 isoccupied(Monitor
*m
, unsigned int t
) {
978 for(c
= clients
; c
; c
= c
->next
)
979 if(c
->tags
[t
] && c
->monitor
== selmonitor
)
985 isprotodel(Client
*c
) {
990 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
991 for(i
= 0; !ret
&& i
< n
; i
++)
992 if(protocols
[i
] == wmatom
[WMDelete
])
1000 isurgent(int monitor
, unsigned int t
) {
1003 for(c
= clients
; c
; c
= c
->next
)
1004 if(c
->monitor
== monitor
&& c
->isurgent
&& c
->tags
[t
])
1010 isvisible(Client
*c
, int monitor
) {
1013 for(i
= 0; i
< LENGTH(tags
); i
++)
1014 if(c
->tags
[i
] && monitors
[c
->monitor
].seltags
[i
] && c
->monitor
== monitor
)
1020 keypress(XEvent
*e
) {
1026 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1027 for(i
= 0; i
< LENGTH(keys
); i
++)
1028 if(keysym
== keys
[i
].keysym
1029 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
1032 keys
[i
].func(keys
[i
].arg
);
1037 killclient(const char *arg
) {
1042 if(isprotodel(sel
)) {
1043 ev
.type
= ClientMessage
;
1044 ev
.xclient
.window
= sel
->win
;
1045 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1046 ev
.xclient
.format
= 32;
1047 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1048 ev
.xclient
.data
.l
[1] = CurrentTime
;
1049 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
1052 XKillClient(dpy
, sel
->win
);
1056 manage(Window w
, XWindowAttributes
*wa
) {
1057 Client
*c
, *t
= NULL
;
1063 c
= emallocz(sizeof(Client
));
1064 c
->tags
= emallocz(sizeof initags
);
1069 m
= &monitors
[c
->monitor
];
1071 c
->x
= wa
->x
+ m
->sx
;
1072 c
->y
= wa
->y
+ m
->sy
;
1075 c
->oldborder
= wa
->border_width
;
1077 if(c
->w
== m
->sw
&& c
->h
== m
->sh
) {
1080 c
->border
= wa
->border_width
;
1083 if(c
->x
+ c
->w
+ 2 * c
->border
> m
->wax
+ m
->waw
)
1084 c
->x
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1085 if(c
->y
+ c
->h
+ 2 * c
->border
> m
->way
+ m
->wah
)
1086 c
->y
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1091 c
->border
= BORDERPX
;
1093 wc
.border_width
= c
->border
;
1094 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1095 XSetWindowBorder(dpy
, w
, m
->dc
.norm
[ColBorder
]);
1096 configure(c
); /* propagates border_width, if size doesn't change */
1098 XSelectInput(dpy
, w
, EnterWindowMask
| FocusChangeMask
| PropertyChangeMask
| StructureNotifyMask
);
1099 grabbuttons(c
, False
);
1101 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
1102 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
1104 memcpy(c
->tags
, t
->tags
, sizeof initags
);
1106 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
1109 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1111 XMapWindow(dpy
, c
->win
);
1112 setclientstate(c
, NormalState
);
1117 mappingnotify(XEvent
*e
) {
1118 XMappingEvent
*ev
= &e
->xmapping
;
1120 XRefreshKeyboardMapping(ev
);
1121 if(ev
->request
== MappingKeyboard
)
1126 maprequest(XEvent
*e
) {
1127 static XWindowAttributes wa
;
1128 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1130 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1132 if(wa
.override_redirect
)
1134 if(!getclient(ev
->window
))
1135 manage(ev
->window
, &wa
);
1144 XQueryPointer(dpy
, monitors
[selmonitor
].root
, &win
, &win
, &x
, &y
, &i
, &i
, &mask
);
1145 for(i
= 0; i
< mcount
; i
++) {
1146 if((x
>= monitors
[i
].sx
&& x
< monitors
[i
].sx
+ monitors
[i
].sw
)
1147 && (y
>= monitors
[i
].sy
&& y
< monitors
[i
].sy
+ monitors
[i
].sh
)) {
1155 movemouse(Client
*c
) {
1156 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1163 if(XGrabPointer(dpy
, monitors
[selmonitor
].root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1164 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1166 XQueryPointer(dpy
, monitors
[selmonitor
].root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1168 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1171 XUngrabPointer(dpy
, CurrentTime
);
1173 case ConfigureRequest
:
1176 handler
[ev
.type
](&ev
);
1180 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1181 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1182 Monitor
*m
= &monitors
[monitorat()];
1183 if(abs(m
->wax
- nx
) < SNAP
)
1185 else if(abs((m
->wax
+ m
->waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
1186 nx
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1187 if(abs(m
->way
- ny
) < SNAP
)
1189 else if(abs((m
->way
+ m
->wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
1190 ny
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1191 if((monitors
[selmonitor
].layout
->arrange
!= floating
) && (abs(nx
- c
->x
) > SNAP
|| abs(ny
- c
->y
) > SNAP
))
1192 togglefloating(NULL
);
1193 if((monitors
[selmonitor
].layout
->arrange
== floating
) || c
->isfloating
)
1194 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1195 memcpy(c
->tags
, monitors
[monitorat()].seltags
, sizeof initags
);
1202 nexttiled(Client
*c
, int monitor
) {
1203 for(; c
&& (c
->isfloating
|| !isvisible(c
, monitor
)); c
= c
->next
);
1208 propertynotify(XEvent
*e
) {
1211 XPropertyEvent
*ev
= &e
->xproperty
;
1213 if(ev
->state
== PropertyDelete
)
1214 return; /* ignore */
1215 if((c
= getclient(ev
->window
))) {
1218 case XA_WM_TRANSIENT_FOR
:
1219 XGetTransientForHint(dpy
, c
->win
, &trans
);
1220 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1223 case XA_WM_NORMAL_HINTS
:
1231 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1240 quit(const char *arg
) {
1241 readin
= running
= False
;
1245 reapply(const char *arg
) {
1246 static Bool zerotags
[LENGTH(tags
)] = { 0 };
1249 for(c
= clients
; c
; c
= c
->next
) {
1250 memcpy(c
->tags
, zerotags
, sizeof zerotags
);
1257 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1259 //Monitor scr = monitors[monitorat()];
1260 // c->monitor = monitorat();
1263 /* set minimum possible */
1269 /* temporarily remove base dimensions */
1273 /* adjust for aspect limits */
1274 if (c
->minay
> 0 && c
->maxay
> 0 && c
->minax
> 0 && c
->maxax
> 0) {
1275 if (w
* c
->maxay
> h
* c
->maxax
)
1276 w
= h
* c
->maxax
/ c
->maxay
;
1277 else if (w
* c
->minay
< h
* c
->minax
)
1278 h
= w
* c
->minay
/ c
->minax
;
1281 /* adjust for increment value */
1287 /* restore base dimensions */
1291 if(c
->minw
> 0 && w
< c
->minw
)
1293 if(c
->minh
> 0 && h
< c
->minh
)
1295 if(c
->maxw
> 0 && w
> c
->maxw
)
1297 if(c
->maxh
> 0 && h
> c
->maxh
)
1300 if(w
<= 0 || h
<= 0)
1302 /* TODO: offscreen appearance fixes */
1305 x = scr.sw - w - 2 * c->border;
1307 y = scr.sh - h - 2 * c->border;
1308 if(x + w + 2 * c->border < scr.sx)
1310 if(y + h + 2 * c->border < scr.sy)
1313 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1316 c
->w
= wc
.width
= w
;
1317 c
->h
= wc
.height
= h
;
1318 wc
.border_width
= c
->border
;
1319 XConfigureWindow(dpy
, c
->win
, CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
, &wc
);
1326 resizemouse(Client
*c
) {
1333 if(XGrabPointer(dpy
, monitors
[selmonitor
].root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1334 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1336 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1338 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1341 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1342 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1343 XUngrabPointer(dpy
, CurrentTime
);
1344 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1346 case ConfigureRequest
:
1349 handler
[ev
.type
](&ev
);
1353 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
1355 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
1357 if((monitors
[selmonitor
].layout
->arrange
!= floating
) && (abs(nw
- c
->w
) > SNAP
|| abs(nh
- c
->h
) > SNAP
))
1358 togglefloating(NULL
);
1359 if((monitors
[selmonitor
].layout
->arrange
== floating
) || c
->isfloating
)
1360 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1376 if(sel
->isfloating
|| (monitors
[selmonitor
].layout
->arrange
== floating
))
1377 XRaiseWindow(dpy
, sel
->win
);
1378 if(monitors
[selmonitor
].layout
->arrange
!= floating
) {
1379 wc
.stack_mode
= Below
;
1380 wc
.sibling
= monitors
[selmonitor
].barwin
;
1381 if(!sel
->isfloating
) {
1382 XConfigureWindow(dpy
, sel
->win
, CWSibling
| CWStackMode
, &wc
);
1383 wc
.sibling
= sel
->win
;
1385 for(i
= 0; i
< mcount
; i
++) {
1386 for(c
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
)) {
1389 XConfigureWindow(dpy
, c
->win
, CWSibling
| CWStackMode
, &wc
);
1390 wc
.sibling
= c
->win
;
1395 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1401 char buf
[sizeof stext
];
1404 unsigned int len
, offset
;
1407 /* main event loop, also reads status text from stdin */
1409 xfd
= ConnectionNumber(dpy
);
1412 len
= sizeof stext
- 1;
1413 buf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1417 FD_SET(STDIN_FILENO
, &rd
);
1419 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1422 eprint("select failed\n");
1424 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1425 switch((r
= read(STDIN_FILENO
, buf
+ offset
, len
- offset
))) {
1427 strncpy(stext
, strerror(errno
), len
);
1431 strncpy(stext
, "EOF", 4);
1435 for(p
= buf
+ offset
; r
> 0; p
++, r
--, offset
++)
1436 if(*p
== '\n' || *p
== '\0') {
1438 strncpy(stext
, buf
, len
);
1439 p
+= r
- 1; /* p is buf + offset + r - 1 */
1440 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1443 memmove(buf
, p
- r
+ 1, r
);
1450 while(XPending(dpy
)) {
1451 XNextEvent(dpy
, &ev
);
1452 if(handler
[ev
.type
])
1453 (handler
[ev
.type
])(&ev
); /* call handler */
1460 unsigned int i
, j
, num
;
1461 Window
*wins
, d1
, d2
;
1462 XWindowAttributes wa
;
1464 for(i
= 0; i
< mcount
; i
++) {
1465 Monitor
*m
= &monitors
[i
];
1467 if(XQueryTree(dpy
, m
->root
, &d1
, &d2
, &wins
, &num
)) {
1468 for(j
= 0; j
< num
; j
++) {
1469 if(!XGetWindowAttributes(dpy
, wins
[j
], &wa
)
1470 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[j
], &d1
))
1472 if(wa
.map_state
== IsViewable
|| getstate(wins
[j
]) == IconicState
)
1473 manage(wins
[j
], &wa
);
1475 for(j
= 0; j
< num
; j
++) { /* now the transients */
1476 if(!XGetWindowAttributes(dpy
, wins
[j
], &wa
))
1478 if(XGetTransientForHint(dpy
, wins
[j
], &d1
)
1479 && (wa
.map_state
== IsViewable
|| getstate(wins
[j
]) == IconicState
))
1480 manage(wins
[j
], &wa
);
1489 setclientstate(Client
*c
, long state
) {
1490 long data
[] = {state
, None
};
1492 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1493 PropModeReplace
, (unsigned char *)data
, 2);
1497 setlayout(const char *arg
) {
1499 Monitor
*m
= &monitors
[monitorat()];
1503 if(m
->layout
== &layouts
[LENGTH(layouts
)])
1504 m
->layout
= &layouts
[0];
1507 for(i
= 0; i
< LENGTH(layouts
); i
++)
1508 if(!strcmp(arg
, layouts
[i
].symbol
))
1510 if(i
== LENGTH(layouts
))
1512 m
->layout
= &layouts
[i
];
1521 setmwfact(const char *arg
) {
1524 Monitor
*m
= &monitors
[monitorat()];
1528 /* arg handling, manipulate mwfact */
1531 else if(sscanf(arg
, "%lf", &delta
) == 1) {
1532 if(arg
[0] == '+' || arg
[0] == '-')
1538 else if(m
->mwfact
> 0.9)
1546 unsigned int i
, j
, k
;
1548 XSetWindowAttributes wa
;
1549 XineramaScreenInfo
*info
= NULL
;
1552 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1553 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1554 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1555 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1556 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1557 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1560 wa
.cursor
= cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1561 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1562 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1564 // init screens/monitors first
1566 if((isxinerama
= XineramaIsActive(dpy
)))
1567 info
= XineramaQueryScreens(dpy
, &mcount
);
1568 monitors
= emallocz(mcount
* sizeof(Monitor
));
1570 for(i
= 0; i
< mcount
; i
++) {
1574 m
->screen
= isxinerama
? 0 : i
;
1575 m
->root
= RootWindow(dpy
, m
->screen
);
1577 if (mcount
!= 1 && isxinerama
) {
1578 m
->sx
= info
[i
].x_org
;
1579 m
->sy
= info
[i
].y_org
;
1580 m
->sw
= info
[i
].width
;
1581 m
->sh
= info
[i
].height
;
1582 fprintf(stderr
, "monitor[%d]: %d,%d,%d,%d\n", i
, m
->sx
, m
->sy
, m
->sw
, m
->sh
);
1587 m
->sw
= DisplayWidth(dpy
, m
->screen
);
1588 m
->sh
= DisplayHeight(dpy
, m
->screen
);
1591 m
->seltags
= emallocz(sizeof initags
);
1592 m
->prevtags
= emallocz(sizeof initags
);
1594 memcpy(m
->seltags
, initags
, sizeof initags
);
1595 memcpy(m
->prevtags
, initags
, sizeof initags
);
1597 /* init appearance */
1598 m
->dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
, m
->screen
);
1599 m
->dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
, m
->screen
);
1600 m
->dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
, m
->screen
);
1601 m
->dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
, m
->screen
);
1602 m
->dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
, m
->screen
);
1603 m
->dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
, m
->screen
);
1605 m
->dc
.h
= bh
= m
->dc
.font
.height
+ 2;
1609 m
->layout
= &layouts
[0];
1610 for(blw
= k
= 0; k
< LENGTH(layouts
); k
++) {
1611 j
= textw(m
, layouts
[k
].symbol
);
1616 // TODO: bpos per screen?
1618 wa
.override_redirect
= 1;
1619 wa
.background_pixmap
= ParentRelative
;
1620 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
1623 m
->barwin
= XCreateWindow(dpy
, m
->root
, m
->sx
, m
->sy
, m
->sw
, bh
, 0,
1624 DefaultDepth(dpy
, m
->screen
), CopyFromParent
, DefaultVisual(dpy
, m
->screen
),
1625 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
1626 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1628 XMapRaised(dpy
, m
->barwin
);
1629 strcpy(stext
, "dwm-"VERSION
);
1630 m
->dc
.drawable
= XCreatePixmap(dpy
, m
->root
, m
->sw
, bh
, DefaultDepth(dpy
, m
->screen
));
1631 m
->dc
.gc
= XCreateGC(dpy
, m
->root
, 0, 0);
1632 XSetLineAttributes(dpy
, m
->dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1634 XSetFont(dpy
, m
->dc
.gc
, m
->dc
.font
.xfont
->fid
);
1636 /* EWMH support per monitor */
1637 XChangeProperty(dpy
, m
->root
, netatom
[NetSupported
], XA_ATOM
, 32,
1638 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1640 /* select for events */
1641 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
1642 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
1643 XChangeWindowAttributes(dpy
, m
->root
, CWEventMask
| CWCursor
, &wa
);
1644 XSelectInput(dpy
, m
->root
, wa
.event_mask
);
1655 selmonitor
= monitorat();
1656 fprintf(stderr
, "selmonitor == %d\n", selmonitor
);
1660 spawn(const char *arg
) {
1661 static char *shell
= NULL
;
1663 if(!shell
&& !(shell
= getenv("SHELL")))
1667 /* The double-fork construct avoids zombie processes and keeps the code
1668 * clean from stupid signal handlers. */
1672 close(ConnectionNumber(dpy
));
1674 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1675 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1684 tag(const char *arg
) {
1689 for(i
= 0; i
< LENGTH(tags
); i
++)
1690 sel
->tags
[i
] = (NULL
== arg
);
1691 sel
->tags
[idxoftag(arg
)] = True
;
1696 textnw(Monitor
*m
, const char *text
, unsigned int len
) {
1699 if(m
->dc
.font
.set
) {
1700 XmbTextExtents(m
->dc
.font
.set
, text
, len
, NULL
, &r
);
1703 return XTextWidth(m
->dc
.font
.xfont
, text
, len
);
1707 textw(Monitor
*m
, const char *text
) {
1708 return textnw(m
, text
, strlen(text
)) + m
->dc
.font
.height
;
1713 unsigned int i
, j
, n
, nx
, ny
, nw
, nh
, mw
, th
;
1716 domwfact
= dozoom
= True
;
1718 nx
= ny
= nw
= 0; /* gcc stupidity requires this */
1720 for (i
= 0; i
< mcount
; i
++) {
1721 Monitor
*m
= &monitors
[i
];
1723 for(n
= 0, c
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
))
1727 mw
= (n
== 1) ? m
->waw
: m
->mwfact
* m
->waw
;
1728 th
= (n
> 1) ? m
->wah
/ (n
- 1) : 0;
1729 if(n
> 1 && th
< bh
)
1732 for(j
= 0, c
= mc
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
)) {
1733 if(j
== 0) { /* master */
1736 nw
= mw
- 2 * c
->border
;
1737 nh
= m
->wah
- 2 * c
->border
;
1739 else { /* tile window */
1742 nx
+= mc
->w
+ 2 * mc
->border
;
1743 nw
= m
->waw
- mw
- 2 * c
->border
;
1745 if(j
+ 1 == n
) /* remainder */
1746 nh
= (m
->way
+ m
->wah
) - ny
- 2 * c
->border
;
1748 nh
= th
- 2 * c
->border
;
1750 fprintf(stderr
, "tile(%d, %d, %d, %d)\n", nx
, ny
, nw
, nh
);
1751 resize(c
, nx
, ny
, nw
, nh
, RESIZEHINTS
);
1752 if((RESIZEHINTS
) && ((c
->h
< bh
) || (c
->h
> nh
) || (c
->w
< bh
) || (c
->w
> nw
)))
1753 /* client doesn't accept size constraints */
1754 resize(c
, nx
, ny
, nw
, nh
, False
);
1755 if(n
> 1 && th
!= m
->wah
)
1756 ny
= c
->y
+ c
->h
+ 2 * c
->border
;
1761 fprintf(stderr
, "done\n");
1764 togglebar(const char *arg
) {
1766 bpos
= (BARPOS
== BarOff
) ? BarTop
: BARPOS
;
1769 updatebarpos(&monitors
[monitorat()]);
1774 togglefloating(const char *arg
) {
1777 sel
->isfloating
= !sel
->isfloating
;
1779 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1784 toggletag(const char *arg
) {
1790 sel
->tags
[i
] = !sel
->tags
[i
];
1791 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1792 if(j
== LENGTH(tags
))
1793 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1798 toggleview(const char *arg
) {
1801 Monitor
*m
= &monitors
[monitorat()];
1804 m
->seltags
[i
] = !m
->seltags
[i
];
1805 for(j
= 0; j
< LENGTH(tags
) && !m
->seltags
[j
]; j
++);
1806 if(j
== LENGTH(tags
))
1807 m
->seltags
[i
] = True
; /* at least one tag must be viewed */
1815 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1816 c
->isbanned
= False
;
1820 unmanage(Client
*c
) {
1823 wc
.border_width
= c
->oldborder
;
1824 /* The server grab construct avoids race conditions. */
1826 XSetErrorHandler(xerrordummy
);
1827 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1832 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1833 setclientstate(c
, WithdrawnState
);
1837 XSetErrorHandler(xerror
);
1843 unmapnotify(XEvent
*e
) {
1845 XUnmapEvent
*ev
= &e
->xunmap
;
1847 if((c
= getclient(ev
->window
)))
1852 updatebarpos(Monitor
*m
) {
1863 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
);
1867 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
+ m
->wah
);
1870 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
- bh
);
1874 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1878 updatesizehints(Client
*c
) {
1882 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1884 c
->flags
= size
.flags
;
1885 if(c
->flags
& PBaseSize
) {
1886 c
->basew
= size
.base_width
;
1887 c
->baseh
= size
.base_height
;
1889 else if(c
->flags
& PMinSize
) {
1890 c
->basew
= size
.min_width
;
1891 c
->baseh
= size
.min_height
;
1894 c
->basew
= c
->baseh
= 0;
1895 if(c
->flags
& PResizeInc
) {
1896 c
->incw
= size
.width_inc
;
1897 c
->inch
= size
.height_inc
;
1900 c
->incw
= c
->inch
= 0;
1901 if(c
->flags
& PMaxSize
) {
1902 c
->maxw
= size
.max_width
;
1903 c
->maxh
= size
.max_height
;
1906 c
->maxw
= c
->maxh
= 0;
1907 if(c
->flags
& PMinSize
) {
1908 c
->minw
= size
.min_width
;
1909 c
->minh
= size
.min_height
;
1911 else if(c
->flags
& PBaseSize
) {
1912 c
->minw
= size
.base_width
;
1913 c
->minh
= size
.base_height
;
1916 c
->minw
= c
->minh
= 0;
1917 if(c
->flags
& PAspect
) {
1918 c
->minax
= size
.min_aspect
.x
;
1919 c
->maxax
= size
.max_aspect
.x
;
1920 c
->minay
= size
.min_aspect
.y
;
1921 c
->maxay
= size
.max_aspect
.y
;
1924 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1925 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1926 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1930 updatetitle(Client
*c
) {
1931 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1932 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1936 updatewmhints(Client
*c
) {
1939 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1940 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1945 /* There's no way to check accesses to destroyed windows, thus those cases are
1946 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1947 * default error handler, which may call exit. */
1949 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1950 if(ee
->error_code
== BadWindow
1951 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1952 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1953 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1954 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1955 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1956 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1957 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1959 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1960 ee
->request_code
, ee
->error_code
);
1961 return xerrorxlib(dpy
, ee
); /* may call exit */
1965 xerrordummy(Display
*dsply
, XErrorEvent
*ee
) {
1969 /* Startup Error handler to check if another window manager
1970 * is already running. */
1972 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
1978 view(const char *arg
) {
1980 Bool tmp
[LENGTH(tags
)];
1981 Monitor
*m
= &monitors
[monitorat()];
1983 for(i
= 0; i
< LENGTH(tags
); i
++)
1984 tmp
[i
] = (NULL
== arg
);
1985 tmp
[idxoftag(arg
)] = True
;
1986 if(memcmp(m
->seltags
, tmp
, sizeof initags
) != 0) {
1987 memcpy(m
->prevtags
, m
->seltags
, sizeof initags
);
1988 memcpy(m
->seltags
, tmp
, sizeof initags
);
1994 viewprevtag(const char *arg
) {
1995 static Bool tmp
[LENGTH(tags
)];
1997 Monitor
*m
= &monitors
[monitorat()];
1999 memcpy(tmp
, m
->seltags
, sizeof initags
);
2000 memcpy(m
->seltags
, m
->prevtags
, sizeof initags
);
2001 memcpy(m
->prevtags
, tmp
, sizeof initags
);
2006 zoom(const char *arg
) {
2009 if(!sel
|| !dozoom
|| sel
->isfloating
)
2011 if((c
= sel
) == nexttiled(clients
, c
->monitor
))
2012 if(!(c
= nexttiled(c
->next
, c
->monitor
)))
2021 movetomonitor(const char *arg
) {
2023 sel
->monitor
= arg
? atoi(arg
) : (sel
->monitor
+1) % mcount
;
2025 memcpy(sel
->tags
, monitors
[sel
->monitor
].seltags
, sizeof initags
);
2026 resize(sel
, monitors
[sel
->monitor
].wax
, monitors
[sel
->monitor
].way
, sel
->w
, sel
->h
, True
);
2032 selectmonitor(const char *arg
) {
2033 Monitor
*m
= &monitors
[arg
? atoi(arg
) : (monitorat()+1) % mcount
];
2035 XWarpPointer(dpy
, None
, m
->root
, 0, 0, 0, 0, m
->wax
+m
->waw
/2, m
->way
+m
->wah
/2);
2041 main(int argc
, char *argv
[]) {
2042 if(argc
== 2 && !strcmp("-v", argv
[1]))
2043 eprint("dwm-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
2044 "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
2046 eprint("usage: dwm [-v]\n");
2048 setlocale(LC_CTYPE
, "");
2049 if(!(dpy
= XOpenDisplay(0)))
2050 eprint("dwm: cannot open display\n");