Xinqi Bao's Git
79a3a33b66c559a54c657c7dc982abe87161e2d9
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
, 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
, isoccupied(m
, j
), m
->dc
.sel
);
576 drawtext(m
, tags
[j
], m
->dc
.norm
, isurgent(i
, j
));
577 drawsquare(m
, sel
&& sel
->tags
[j
] && sel
->monitor
== selmonitor
, isoccupied(m
, j
), m
->dc
.norm
);
582 drawtext(m
, m
->layout
->symbol
, m
->dc
.norm
, False
);
583 x
= m
->dc
.x
+ m
->dc
.w
;
584 m
->dc
.w
= textw(m
, stext
);
585 m
->dc
.x
= m
->sw
- m
->dc
.w
;
590 drawtext(m
, stext
, m
->dc
.norm
, False
);
591 if((m
->dc
.w
= m
->dc
.x
- x
) > bh
) {
593 if(sel
&& sel
->monitor
== selmonitor
) {
594 drawtext(m
, sel
->name
, m
->dc
.sel
, False
);
595 drawsquare(m
, False
, sel
->isfloating
, m
->dc
.sel
);
598 drawtext(m
, NULL
, m
->dc
.norm
, False
);
600 XCopyArea(dpy
, m
->dc
.drawable
, m
->barwin
, m
->dc
.gc
, 0, 0, m
->sw
, bh
, 0, 0);
606 drawsquare(Monitor
*m
, Bool filled
, Bool empty
, unsigned long col
[ColLast
]) {
609 XRectangle r
= { m
->dc
.x
, m
->dc
.y
, m
->dc
.w
, m
->dc
.h
};
611 gcv
.foreground
= col
[ColFG
];
612 XChangeGC(dpy
, m
->dc
.gc
, GCForeground
, &gcv
);
613 x
= (m
->dc
.font
.ascent
+ m
->dc
.font
.descent
+ 2) / 4;
617 r
.width
= r
.height
= x
+ 1;
618 XFillRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
621 r
.width
= r
.height
= x
;
622 XDrawRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
627 drawtext(Monitor
*m
, const char *text
, unsigned long col
[ColLast
], Bool invert
) {
629 static char buf
[256];
630 unsigned int len
, olen
;
631 XRectangle r
= { m
->dc
.x
, m
->dc
.y
, m
->dc
.w
, m
->dc
.h
};
633 XSetForeground(dpy
, m
->dc
.gc
, col
[invert
? ColFG
: ColBG
]);
634 XFillRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
638 olen
= len
= strlen(text
);
639 if(len
>= sizeof buf
)
640 len
= sizeof buf
- 1;
641 memcpy(buf
, text
, len
);
643 h
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
644 y
= m
->dc
.y
+ (m
->dc
.h
/ 2) - (h
/ 2) + m
->dc
.font
.ascent
;
645 x
= m
->dc
.x
+ (h
/ 2);
646 /* shorten text if necessary */
647 while(len
&& (w
= textnw(m
, buf
, len
)) > m
->dc
.w
- h
)
658 return; /* too long */
659 XSetForeground(dpy
, m
->dc
.gc
, col
[invert
? ColBG
: ColFG
]);
661 XmbDrawString(dpy
, m
->dc
.drawable
, m
->dc
.font
.set
, m
->dc
.gc
, x
, y
, buf
, len
);
663 XDrawString(dpy
, m
->dc
.drawable
, m
->dc
.gc
, x
, y
, buf
, len
);
667 emallocz(unsigned int size
) {
668 void *res
= calloc(1, size
);
671 eprint("fatal: could not malloc() %u bytes\n", size
);
676 enternotify(XEvent
*e
) {
678 XCrossingEvent
*ev
= &e
->xcrossing
;
680 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
);
682 if((c
= getclient(ev
->window
)))
685 selmonitor
= monitorat();
686 fprintf(stderr
, "updating selmonitor %d\n", selmonitor
);
692 eprint(const char *errstr
, ...) {
695 va_start(ap
, errstr
);
696 vfprintf(stderr
, errstr
, ap
);
703 XExposeEvent
*ev
= &e
->xexpose
;
706 if(ev
->window
== monitors
[selmonitor
].barwin
)
712 floating(void) { /* default floating layout */
715 domwfact
= dozoom
= False
;
716 for(c
= clients
; c
; c
= c
->next
)
717 if(isvisible(c
, selmonitor
))
718 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
726 selmonitor
= c
->monitor
;
727 m
= &monitors
[selmonitor
];
728 if(!c
|| (c
&& !isvisible(c
, selmonitor
)))
729 for(c
= stack
; c
&& !isvisible(c
, c
->monitor
); c
= c
->snext
);
730 if(sel
&& sel
!= c
) {
731 grabbuttons(sel
, False
);
732 XSetWindowBorder(dpy
, sel
->win
, monitors
[sel
->monitor
].dc
.norm
[ColBorder
]);
737 grabbuttons(c
, True
);
742 XSetWindowBorder(dpy
, c
->win
, m
->dc
.sel
[ColBorder
]);
743 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
744 selmonitor
= c
->monitor
;
747 XSetInputFocus(dpy
, m
->root
, RevertToPointerRoot
, CurrentTime
);
752 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
753 XFocusChangeEvent
*ev
= &e
->xfocus
;
755 if(sel
&& ev
->window
!= sel
->win
)
756 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
760 focusnext(const char *arg
) {
765 for(c
= sel
->next
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
767 for(c
= clients
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
775 focusprev(const char *arg
) {
780 for(c
= sel
->prev
; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
782 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
783 for(; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
792 getclient(Window w
) {
795 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
800 getcolor(const char *colstr
, int screen
) {
801 Colormap cmap
= DefaultColormap(dpy
, screen
);
804 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
805 eprint("error, cannot allocate color '%s'\n", colstr
);
813 unsigned char *p
= NULL
;
814 unsigned long n
, extra
;
817 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
818 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
819 if(status
!= Success
)
828 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
833 if(!text
|| size
== 0)
836 XGetTextProperty(dpy
, w
, &name
, atom
);
839 if(name
.encoding
== XA_STRING
)
840 strncpy(text
, (char *)name
.value
, size
- 1);
842 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
844 strncpy(text
, *list
, size
- 1);
845 XFreeStringList(list
);
848 text
[size
- 1] = '\0';
854 grabbuttons(Client
*c
, Bool focused
) {
855 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
858 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
859 GrabModeAsync
, GrabModeSync
, None
, None
);
860 XGrabButton(dpy
, Button1
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
861 GrabModeAsync
, GrabModeSync
, None
, None
);
862 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
863 GrabModeAsync
, GrabModeSync
, None
, None
);
864 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
865 GrabModeAsync
, GrabModeSync
, None
, None
);
867 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
868 GrabModeAsync
, GrabModeSync
, None
, None
);
869 XGrabButton(dpy
, Button2
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
870 GrabModeAsync
, GrabModeSync
, None
, None
);
871 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
872 GrabModeAsync
, GrabModeSync
, None
, None
);
873 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
874 GrabModeAsync
, GrabModeSync
, None
, None
);
876 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
877 GrabModeAsync
, GrabModeSync
, None
, None
);
878 XGrabButton(dpy
, Button3
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
879 GrabModeAsync
, GrabModeSync
, None
, None
);
880 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
881 GrabModeAsync
, GrabModeSync
, None
, None
);
882 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
883 GrabModeAsync
, GrabModeSync
, None
, None
);
886 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
887 GrabModeAsync
, GrabModeSync
, None
, None
);
894 XModifierKeymap
*modmap
;
896 /* init modifier map */
897 modmap
= XGetModifierMapping(dpy
);
898 for(i
= 0; i
< 8; i
++)
899 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
900 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
901 numlockmask
= (1 << i
);
903 XFreeModifiermap(modmap
);
905 for(i
= 0; i
< mcount
; i
++) {
906 Monitor
*m
= &monitors
[i
];
907 XUngrabKey(dpy
, AnyKey
, AnyModifier
, m
->root
);
908 for(j
= 0; j
< LENGTH(keys
); j
++) {
909 code
= XKeysymToKeycode(dpy
, keys
[j
].keysym
);
910 XGrabKey(dpy
, code
, keys
[j
].mod
, m
->root
, True
,
911 GrabModeAsync
, GrabModeAsync
);
912 XGrabKey(dpy
, code
, keys
[j
].mod
| LockMask
, m
->root
, True
,
913 GrabModeAsync
, GrabModeAsync
);
914 XGrabKey(dpy
, code
, keys
[j
].mod
| numlockmask
, m
->root
, True
,
915 GrabModeAsync
, GrabModeAsync
);
916 XGrabKey(dpy
, code
, keys
[j
].mod
| numlockmask
| LockMask
, m
->root
, True
,
917 GrabModeAsync
, GrabModeAsync
);
923 idxoftag(const char *tag
) {
926 for(i
= 0; (i
< LENGTH(tags
)) && (tags
[i
] != tag
); i
++);
927 return (i
< LENGTH(tags
)) ? i
: 0;
931 initfont(Monitor
*m
, const char *fontstr
) {
932 char *def
, **missing
;
937 XFreeFontSet(dpy
, m
->dc
.font
.set
);
938 m
->dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
941 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
942 XFreeStringList(missing
);
945 XFontSetExtents
*font_extents
;
946 XFontStruct
**xfonts
;
948 m
->dc
.font
.ascent
= m
->dc
.font
.descent
= 0;
949 font_extents
= XExtentsOfFontSet(m
->dc
.font
.set
);
950 n
= XFontsOfFontSet(m
->dc
.font
.set
, &xfonts
, &font_names
);
951 for(i
= 0, m
->dc
.font
.ascent
= 0, m
->dc
.font
.descent
= 0; i
< n
; i
++) {
952 if(m
->dc
.font
.ascent
< (*xfonts
)->ascent
)
953 m
->dc
.font
.ascent
= (*xfonts
)->ascent
;
954 if(m
->dc
.font
.descent
< (*xfonts
)->descent
)
955 m
->dc
.font
.descent
= (*xfonts
)->descent
;
961 XFreeFont(dpy
, m
->dc
.font
.xfont
);
962 m
->dc
.font
.xfont
= NULL
;
963 if(!(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
964 && !(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
965 eprint("error, cannot load font: '%s'\n", fontstr
);
966 m
->dc
.font
.ascent
= m
->dc
.font
.xfont
->ascent
;
967 m
->dc
.font
.descent
= m
->dc
.font
.xfont
->descent
;
969 m
->dc
.font
.height
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
973 isoccupied(Monitor
*m
, unsigned int t
) {
976 for(c
= clients
; c
; c
= c
->next
)
977 if(c
->tags
[t
] && c
->monitor
== selmonitor
)
983 isprotodel(Client
*c
) {
988 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
989 for(i
= 0; !ret
&& i
< n
; i
++)
990 if(protocols
[i
] == wmatom
[WMDelete
])
998 isurgent(int monitor
, unsigned int t
) {
1001 for(c
= clients
; c
; c
= c
->next
)
1002 if(c
->monitor
== monitor
&& c
->isurgent
&& c
->tags
[t
])
1008 isvisible(Client
*c
, int monitor
) {
1011 for(i
= 0; i
< LENGTH(tags
); i
++)
1012 if(c
->tags
[i
] && monitors
[c
->monitor
].seltags
[i
] && c
->monitor
== monitor
)
1018 keypress(XEvent
*e
) {
1024 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1025 for(i
= 0; i
< LENGTH(keys
); i
++)
1026 if(keysym
== keys
[i
].keysym
1027 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
1030 keys
[i
].func(keys
[i
].arg
);
1035 killclient(const char *arg
) {
1040 if(isprotodel(sel
)) {
1041 ev
.type
= ClientMessage
;
1042 ev
.xclient
.window
= sel
->win
;
1043 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1044 ev
.xclient
.format
= 32;
1045 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1046 ev
.xclient
.data
.l
[1] = CurrentTime
;
1047 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
1050 XKillClient(dpy
, sel
->win
);
1054 manage(Window w
, XWindowAttributes
*wa
) {
1055 Client
*c
, *t
= NULL
;
1061 c
= emallocz(sizeof(Client
));
1062 c
->tags
= emallocz(sizeof initags
);
1067 m
= &monitors
[c
->monitor
];
1069 c
->x
= wa
->x
+ m
->sx
;
1070 c
->y
= wa
->y
+ m
->sy
;
1073 c
->oldborder
= wa
->border_width
;
1075 if(c
->w
== m
->sw
&& c
->h
== m
->sh
) {
1078 c
->border
= wa
->border_width
;
1081 if(c
->x
+ c
->w
+ 2 * c
->border
> m
->wax
+ m
->waw
)
1082 c
->x
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1083 if(c
->y
+ c
->h
+ 2 * c
->border
> m
->way
+ m
->wah
)
1084 c
->y
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1089 c
->border
= BORDERPX
;
1091 wc
.border_width
= c
->border
;
1092 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1093 XSetWindowBorder(dpy
, w
, m
->dc
.norm
[ColBorder
]);
1094 configure(c
); /* propagates border_width, if size doesn't change */
1096 XSelectInput(dpy
, w
, EnterWindowMask
| FocusChangeMask
| PropertyChangeMask
| StructureNotifyMask
);
1097 grabbuttons(c
, False
);
1099 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
1100 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
1102 memcpy(c
->tags
, t
->tags
, sizeof initags
);
1104 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
1107 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1109 XMapWindow(dpy
, c
->win
);
1110 setclientstate(c
, NormalState
);
1115 mappingnotify(XEvent
*e
) {
1116 XMappingEvent
*ev
= &e
->xmapping
;
1118 XRefreshKeyboardMapping(ev
);
1119 if(ev
->request
== MappingKeyboard
)
1124 maprequest(XEvent
*e
) {
1125 static XWindowAttributes wa
;
1126 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1128 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1130 if(wa
.override_redirect
)
1132 if(!getclient(ev
->window
))
1133 manage(ev
->window
, &wa
);
1142 XQueryPointer(dpy
, monitors
[selmonitor
].root
, &win
, &win
, &x
, &y
, &i
, &i
, &mask
);
1143 for(i
= 0; i
< mcount
; i
++) {
1144 fprintf(stderr
, "checking monitor[%d]: %d %d %d %d\n", i
, monitors
[i
].sx
, monitors
[i
].sy
, monitors
[i
].sw
, monitors
[i
].sh
);
1145 if((x
>= monitors
[i
].sx
&& x
< monitors
[i
].sx
+ monitors
[i
].sw
)
1146 && (y
>= monitors
[i
].sy
&& y
< monitors
[i
].sy
+ monitors
[i
].sh
)) {
1147 fprintf(stderr
, "%d,%d -> %d\n", x
, y
, i
);
1151 fprintf(stderr
, "?,? -> 0\n");
1156 movemouse(Client
*c
) {
1157 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1164 if(XGrabPointer(dpy
, monitors
[selmonitor
].root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1165 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1167 XQueryPointer(dpy
, monitors
[selmonitor
].root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1169 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1172 XUngrabPointer(dpy
, CurrentTime
);
1174 case ConfigureRequest
:
1177 handler
[ev
.type
](&ev
);
1181 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1182 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1183 Monitor
*m
= &monitors
[monitorat()];
1184 if(abs(m
->wax
- nx
) < SNAP
)
1186 else if(abs((m
->wax
+ m
->waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
1187 nx
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1188 if(abs(m
->way
- ny
) < SNAP
)
1190 else if(abs((m
->way
+ m
->wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
1191 ny
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1192 if((monitors
[selmonitor
].layout
->arrange
!= floating
) && (abs(nx
- c
->x
) > SNAP
|| abs(ny
- c
->y
) > SNAP
))
1193 togglefloating(NULL
);
1194 if((monitors
[selmonitor
].layout
->arrange
== floating
) || c
->isfloating
)
1195 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1196 memcpy(c
->tags
, monitors
[monitorat()].seltags
, sizeof initags
);
1203 nexttiled(Client
*c
, int monitor
) {
1204 for(; c
&& (c
->isfloating
|| !isvisible(c
, monitor
)); c
= c
->next
);
1209 propertynotify(XEvent
*e
) {
1212 XPropertyEvent
*ev
= &e
->xproperty
;
1214 if(ev
->state
== PropertyDelete
)
1215 return; /* ignore */
1216 if((c
= getclient(ev
->window
))) {
1219 case XA_WM_TRANSIENT_FOR
:
1220 XGetTransientForHint(dpy
, c
->win
, &trans
);
1221 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1224 case XA_WM_NORMAL_HINTS
:
1232 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1241 quit(const char *arg
) {
1242 readin
= running
= False
;
1246 reapply(const char *arg
) {
1247 static Bool zerotags
[LENGTH(tags
)] = { 0 };
1250 for(c
= clients
; c
; c
= c
->next
) {
1251 memcpy(c
->tags
, zerotags
, sizeof zerotags
);
1258 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1260 //Monitor scr = monitors[monitorat()];
1261 // c->monitor = monitorat();
1264 /* set minimum possible */
1270 /* temporarily remove base dimensions */
1274 /* adjust for aspect limits */
1275 if (c
->minay
> 0 && c
->maxay
> 0 && c
->minax
> 0 && c
->maxax
> 0) {
1276 if (w
* c
->maxay
> h
* c
->maxax
)
1277 w
= h
* c
->maxax
/ c
->maxay
;
1278 else if (w
* c
->minay
< h
* c
->minax
)
1279 h
= w
* c
->minay
/ c
->minax
;
1282 /* adjust for increment value */
1288 /* restore base dimensions */
1292 if(c
->minw
> 0 && w
< c
->minw
)
1294 if(c
->minh
> 0 && h
< c
->minh
)
1296 if(c
->maxw
> 0 && w
> c
->maxw
)
1298 if(c
->maxh
> 0 && h
> c
->maxh
)
1301 if(w
<= 0 || h
<= 0)
1303 /* TODO: offscreen appearance fixes */
1306 x = scr.sw - w - 2 * c->border;
1308 y = scr.sh - h - 2 * c->border;
1309 if(x + w + 2 * c->border < scr.sx)
1311 if(y + h + 2 * c->border < scr.sy)
1314 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1317 c
->w
= wc
.width
= w
;
1318 c
->h
= wc
.height
= h
;
1319 wc
.border_width
= c
->border
;
1320 XConfigureWindow(dpy
, c
->win
, CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
, &wc
);
1327 resizemouse(Client
*c
) {
1334 if(XGrabPointer(dpy
, monitors
[selmonitor
].root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1335 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1337 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1339 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1342 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1343 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1344 XUngrabPointer(dpy
, CurrentTime
);
1345 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1347 case ConfigureRequest
:
1350 handler
[ev
.type
](&ev
);
1354 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
1356 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
1358 if((monitors
[selmonitor
].layout
->arrange
!= floating
) && (abs(nw
- c
->w
) > SNAP
|| abs(nh
- c
->h
) > SNAP
))
1359 togglefloating(NULL
);
1360 if((monitors
[selmonitor
].layout
->arrange
== floating
) || c
->isfloating
)
1361 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1377 if(sel
->isfloating
|| (monitors
[selmonitor
].layout
->arrange
== floating
))
1378 XRaiseWindow(dpy
, sel
->win
);
1379 if(monitors
[selmonitor
].layout
->arrange
!= floating
) {
1380 wc
.stack_mode
= Below
;
1381 wc
.sibling
= monitors
[selmonitor
].barwin
;
1382 if(!sel
->isfloating
) {
1383 XConfigureWindow(dpy
, sel
->win
, CWSibling
| CWStackMode
, &wc
);
1384 wc
.sibling
= sel
->win
;
1386 for(i
= 0; i
< mcount
; i
++) {
1387 for(c
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
)) {
1390 XConfigureWindow(dpy
, c
->win
, CWSibling
| CWStackMode
, &wc
);
1391 wc
.sibling
= c
->win
;
1396 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1402 char buf
[sizeof stext
];
1405 unsigned int len
, offset
;
1408 /* main event loop, also reads status text from stdin */
1410 xfd
= ConnectionNumber(dpy
);
1413 len
= sizeof stext
- 1;
1414 buf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1418 FD_SET(STDIN_FILENO
, &rd
);
1420 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1423 eprint("select failed\n");
1425 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1426 switch((r
= read(STDIN_FILENO
, buf
+ offset
, len
- offset
))) {
1428 strncpy(stext
, strerror(errno
), len
);
1432 strncpy(stext
, "EOF", 4);
1436 for(p
= buf
+ offset
; r
> 0; p
++, r
--, offset
++)
1437 if(*p
== '\n' || *p
== '\0') {
1439 strncpy(stext
, buf
, len
);
1440 p
+= r
- 1; /* p is buf + offset + r - 1 */
1441 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1444 memmove(buf
, p
- r
+ 1, r
);
1451 while(XPending(dpy
)) {
1452 XNextEvent(dpy
, &ev
);
1453 if(handler
[ev
.type
])
1454 (handler
[ev
.type
])(&ev
); /* call handler */
1461 unsigned int i
, j
, num
;
1462 Window
*wins
, d1
, d2
;
1463 XWindowAttributes wa
;
1465 for(i
= 0; i
< mcount
; i
++) {
1466 Monitor
*m
= &monitors
[i
];
1468 if(XQueryTree(dpy
, m
->root
, &d1
, &d2
, &wins
, &num
)) {
1469 for(j
= 0; j
< num
; j
++) {
1470 if(!XGetWindowAttributes(dpy
, wins
[j
], &wa
)
1471 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[j
], &d1
))
1473 if(wa
.map_state
== IsViewable
|| getstate(wins
[j
]) == IconicState
)
1474 manage(wins
[j
], &wa
);
1476 for(j
= 0; j
< num
; j
++) { /* now the transients */
1477 if(!XGetWindowAttributes(dpy
, wins
[j
], &wa
))
1479 if(XGetTransientForHint(dpy
, wins
[j
], &d1
)
1480 && (wa
.map_state
== IsViewable
|| getstate(wins
[j
]) == IconicState
))
1481 manage(wins
[j
], &wa
);
1490 setclientstate(Client
*c
, long state
) {
1491 long data
[] = {state
, None
};
1493 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1494 PropModeReplace
, (unsigned char *)data
, 2);
1498 setlayout(const char *arg
) {
1500 Monitor
*m
= &monitors
[monitorat()];
1504 if(m
->layout
== &layouts
[LENGTH(layouts
)])
1505 m
->layout
= &layouts
[0];
1508 for(i
= 0; i
< LENGTH(layouts
); i
++)
1509 if(!strcmp(arg
, layouts
[i
].symbol
))
1511 if(i
== LENGTH(layouts
))
1513 m
->layout
= &layouts
[i
];
1522 setmwfact(const char *arg
) {
1525 Monitor
*m
= &monitors
[monitorat()];
1529 /* arg handling, manipulate mwfact */
1532 else if(sscanf(arg
, "%lf", &delta
) == 1) {
1533 if(arg
[0] == '+' || arg
[0] == '-')
1539 else if(m
->mwfact
> 0.9)
1547 unsigned int i
, j
, k
;
1549 XSetWindowAttributes wa
;
1550 XineramaScreenInfo
*info
= NULL
;
1553 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1554 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1555 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1556 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1557 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1558 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1561 wa
.cursor
= cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1562 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1563 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1565 // init screens/monitors first
1567 if((isxinerama
= XineramaIsActive(dpy
)))
1568 info
= XineramaQueryScreens(dpy
, &mcount
);
1569 monitors
= emallocz(mcount
* sizeof(Monitor
));
1571 for(i
= 0; i
< mcount
; i
++) {
1575 m
->screen
= isxinerama
? 0 : i
;
1576 m
->root
= RootWindow(dpy
, m
->screen
);
1578 if (mcount
!= 1 && isxinerama
) {
1579 m
->sx
= info
[i
].x_org
;
1580 m
->sy
= info
[i
].y_org
;
1581 m
->sw
= info
[i
].width
;
1582 m
->sh
= info
[i
].height
;
1583 fprintf(stderr
, "monitor[%d]: %d,%d,%d,%d\n", i
, m
->sx
, m
->sy
, m
->sw
, m
->sh
);
1588 m
->sw
= DisplayWidth(dpy
, m
->screen
);
1589 m
->sh
= DisplayHeight(dpy
, m
->screen
);
1592 m
->seltags
= emallocz(sizeof initags
);
1593 m
->prevtags
= emallocz(sizeof initags
);
1595 memcpy(m
->seltags
, initags
, sizeof initags
);
1596 memcpy(m
->prevtags
, initags
, sizeof initags
);
1598 /* init appearance */
1599 m
->dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
, m
->screen
);
1600 m
->dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
, m
->screen
);
1601 m
->dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
, m
->screen
);
1602 m
->dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
, m
->screen
);
1603 m
->dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
, m
->screen
);
1604 m
->dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
, m
->screen
);
1606 m
->dc
.h
= bh
= m
->dc
.font
.height
+ 2;
1610 m
->layout
= &layouts
[0];
1611 for(blw
= k
= 0; k
< LENGTH(layouts
); k
++) {
1612 j
= textw(m
, layouts
[k
].symbol
);
1617 // TODO: bpos per screen?
1619 wa
.override_redirect
= 1;
1620 wa
.background_pixmap
= ParentRelative
;
1621 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
1624 m
->barwin
= XCreateWindow(dpy
, m
->root
, m
->sx
, m
->sy
, m
->sw
, bh
, 0,
1625 DefaultDepth(dpy
, m
->screen
), CopyFromParent
, DefaultVisual(dpy
, m
->screen
),
1626 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
1627 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1629 XMapRaised(dpy
, m
->barwin
);
1630 strcpy(stext
, "dwm-"VERSION
);
1631 m
->dc
.drawable
= XCreatePixmap(dpy
, m
->root
, m
->sw
, bh
, DefaultDepth(dpy
, m
->screen
));
1632 m
->dc
.gc
= XCreateGC(dpy
, m
->root
, 0, 0);
1633 XSetLineAttributes(dpy
, m
->dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1635 XSetFont(dpy
, m
->dc
.gc
, m
->dc
.font
.xfont
->fid
);
1637 /* EWMH support per monitor */
1638 XChangeProperty(dpy
, m
->root
, netatom
[NetSupported
], XA_ATOM
, 32,
1639 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1641 /* select for events */
1642 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
1643 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
1644 XChangeWindowAttributes(dpy
, m
->root
, CWEventMask
| CWCursor
, &wa
);
1645 XSelectInput(dpy
, m
->root
, wa
.event_mask
);
1656 selmonitor
= monitorat();
1657 fprintf(stderr
, "selmonitor == %d\n", selmonitor
);
1661 spawn(const char *arg
) {
1662 static char *shell
= NULL
;
1664 if(!shell
&& !(shell
= getenv("SHELL")))
1668 /* The double-fork construct avoids zombie processes and keeps the code
1669 * clean from stupid signal handlers. */
1673 close(ConnectionNumber(dpy
));
1675 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1676 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1685 tag(const char *arg
) {
1690 for(i
= 0; i
< LENGTH(tags
); i
++)
1691 sel
->tags
[i
] = (NULL
== arg
);
1692 sel
->tags
[idxoftag(arg
)] = True
;
1697 textnw(Monitor
*m
, const char *text
, unsigned int len
) {
1700 if(m
->dc
.font
.set
) {
1701 XmbTextExtents(m
->dc
.font
.set
, text
, len
, NULL
, &r
);
1704 return XTextWidth(m
->dc
.font
.xfont
, text
, len
);
1708 textw(Monitor
*m
, const char *text
) {
1709 return textnw(m
, text
, strlen(text
)) + m
->dc
.font
.height
;
1714 unsigned int i
, j
, n
, nx
, ny
, nw
, nh
, mw
, th
;
1717 domwfact
= dozoom
= True
;
1719 nx
= ny
= nw
= 0; /* gcc stupidity requires this */
1721 for (i
= 0; i
< mcount
; i
++) {
1722 Monitor
*m
= &monitors
[i
];
1724 for(n
= 0, c
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
))
1728 mw
= (n
== 1) ? m
->waw
: m
->mwfact
* m
->waw
;
1729 th
= (n
> 1) ? m
->wah
/ (n
- 1) : 0;
1730 if(n
> 1 && th
< bh
)
1733 for(j
= 0, c
= mc
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
)) {
1734 if(j
== 0) { /* master */
1737 nw
= mw
- 2 * c
->border
;
1738 nh
= m
->wah
- 2 * c
->border
;
1740 else { /* tile window */
1743 nx
+= mc
->w
+ 2 * mc
->border
;
1744 nw
= m
->waw
- mw
- 2 * c
->border
;
1746 if(j
+ 1 == n
) /* remainder */
1747 nh
= (m
->way
+ m
->wah
) - ny
- 2 * c
->border
;
1749 nh
= th
- 2 * c
->border
;
1751 fprintf(stderr
, "tile(%d, %d, %d, %d)\n", nx
, ny
, nw
, nh
);
1752 resize(c
, nx
, ny
, nw
, nh
, RESIZEHINTS
);
1753 if((RESIZEHINTS
) && ((c
->h
< bh
) || (c
->h
> nh
) || (c
->w
< bh
) || (c
->w
> nw
)))
1754 /* client doesn't accept size constraints */
1755 resize(c
, nx
, ny
, nw
, nh
, False
);
1756 if(n
> 1 && th
!= m
->wah
)
1757 ny
= c
->y
+ c
->h
+ 2 * c
->border
;
1762 fprintf(stderr
, "done\n");
1765 togglebar(const char *arg
) {
1767 bpos
= (BARPOS
== BarOff
) ? BarTop
: BARPOS
;
1770 updatebarpos(&monitors
[monitorat()]);
1775 togglefloating(const char *arg
) {
1778 sel
->isfloating
= !sel
->isfloating
;
1780 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1785 toggletag(const char *arg
) {
1791 sel
->tags
[i
] = !sel
->tags
[i
];
1792 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1793 if(j
== LENGTH(tags
))
1794 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1799 toggleview(const char *arg
) {
1802 Monitor
*m
= &monitors
[monitorat()];
1805 m
->seltags
[i
] = !m
->seltags
[i
];
1806 for(j
= 0; j
< LENGTH(tags
) && !m
->seltags
[j
]; j
++);
1807 if(j
== LENGTH(tags
))
1808 m
->seltags
[i
] = True
; /* at least one tag must be viewed */
1816 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1817 c
->isbanned
= False
;
1821 unmanage(Client
*c
) {
1824 wc
.border_width
= c
->oldborder
;
1825 /* The server grab construct avoids race conditions. */
1827 XSetErrorHandler(xerrordummy
);
1828 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1833 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1834 setclientstate(c
, WithdrawnState
);
1838 XSetErrorHandler(xerror
);
1844 unmapnotify(XEvent
*e
) {
1846 XUnmapEvent
*ev
= &e
->xunmap
;
1848 if((c
= getclient(ev
->window
)))
1853 updatebarpos(Monitor
*m
) {
1864 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
);
1868 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
+ m
->wah
);
1871 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
- bh
);
1875 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1879 updatesizehints(Client
*c
) {
1883 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1885 c
->flags
= size
.flags
;
1886 if(c
->flags
& PBaseSize
) {
1887 c
->basew
= size
.base_width
;
1888 c
->baseh
= size
.base_height
;
1890 else if(c
->flags
& PMinSize
) {
1891 c
->basew
= size
.min_width
;
1892 c
->baseh
= size
.min_height
;
1895 c
->basew
= c
->baseh
= 0;
1896 if(c
->flags
& PResizeInc
) {
1897 c
->incw
= size
.width_inc
;
1898 c
->inch
= size
.height_inc
;
1901 c
->incw
= c
->inch
= 0;
1902 if(c
->flags
& PMaxSize
) {
1903 c
->maxw
= size
.max_width
;
1904 c
->maxh
= size
.max_height
;
1907 c
->maxw
= c
->maxh
= 0;
1908 if(c
->flags
& PMinSize
) {
1909 c
->minw
= size
.min_width
;
1910 c
->minh
= size
.min_height
;
1912 else if(c
->flags
& PBaseSize
) {
1913 c
->minw
= size
.base_width
;
1914 c
->minh
= size
.base_height
;
1917 c
->minw
= c
->minh
= 0;
1918 if(c
->flags
& PAspect
) {
1919 c
->minax
= size
.min_aspect
.x
;
1920 c
->maxax
= size
.max_aspect
.x
;
1921 c
->minay
= size
.min_aspect
.y
;
1922 c
->maxay
= size
.max_aspect
.y
;
1925 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1926 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1927 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1931 updatetitle(Client
*c
) {
1932 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1933 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1937 updatewmhints(Client
*c
) {
1940 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1941 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1946 /* There's no way to check accesses to destroyed windows, thus those cases are
1947 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1948 * default error handler, which may call exit. */
1950 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1951 if(ee
->error_code
== BadWindow
1952 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1953 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1954 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1955 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1956 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1957 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1958 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1960 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1961 ee
->request_code
, ee
->error_code
);
1962 return xerrorxlib(dpy
, ee
); /* may call exit */
1966 xerrordummy(Display
*dsply
, XErrorEvent
*ee
) {
1970 /* Startup Error handler to check if another window manager
1971 * is already running. */
1973 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
1979 view(const char *arg
) {
1981 Bool tmp
[LENGTH(tags
)];
1982 Monitor
*m
= &monitors
[monitorat()];
1984 for(i
= 0; i
< LENGTH(tags
); i
++)
1985 tmp
[i
] = (NULL
== arg
);
1986 tmp
[idxoftag(arg
)] = True
;
1987 if(memcmp(m
->seltags
, tmp
, sizeof initags
) != 0) {
1988 memcpy(m
->prevtags
, m
->seltags
, sizeof initags
);
1989 memcpy(m
->seltags
, tmp
, sizeof initags
);
1995 viewprevtag(const char *arg
) {
1996 static Bool tmp
[LENGTH(tags
)];
1998 Monitor
*m
= &monitors
[monitorat()];
2000 memcpy(tmp
, m
->seltags
, sizeof initags
);
2001 memcpy(m
->seltags
, m
->prevtags
, sizeof initags
);
2002 memcpy(m
->prevtags
, tmp
, sizeof initags
);
2007 zoom(const char *arg
) {
2010 if(!sel
|| !dozoom
|| sel
->isfloating
)
2012 if((c
= sel
) == nexttiled(clients
, c
->monitor
))
2013 if(!(c
= nexttiled(c
->next
, c
->monitor
)))
2022 movetomonitor(const char *arg
) {
2024 sel
->monitor
= arg
? atoi(arg
) : (sel
->monitor
+1) % mcount
;
2026 memcpy(sel
->tags
, monitors
[sel
->monitor
].seltags
, sizeof initags
);
2027 resize(sel
, monitors
[sel
->monitor
].wax
, monitors
[sel
->monitor
].way
, sel
->w
, sel
->h
, True
);
2033 selectmonitor(const char *arg
) {
2034 Monitor
*m
= &monitors
[arg
? atoi(arg
) : (monitorat()+1) % mcount
];
2036 XWarpPointer(dpy
, None
, m
->root
, 0, 0, 0, 0, m
->wax
+m
->waw
/2, m
->way
+m
->wah
/2);
2042 main(int argc
, char *argv
[]) {
2043 if(argc
== 2 && !strcmp("-v", argv
[1]))
2044 eprint("dwm-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
2045 "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
2047 eprint("usage: dwm [-v]\n");
2049 setlocale(LC_CTYPE
, "");
2050 if(!(dpy
= XOpenDisplay(0)))
2051 eprint("dwm: cannot open display\n");