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
, 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
);
379 else if(ev
->button
== Button3
&& !c
->isfixed
) {
389 XSetErrorHandler(xerrorstart
);
391 /* this causes an error if some other window manager is running */
392 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
395 eprint("dwm: another window manager is already running\n");
397 XSetErrorHandler(NULL
);
398 xerrorxlib
= XSetErrorHandler(xerror
);
410 for(i
= 0; i
< mcount
; i
++) {
411 Monitor
*m
= &monitors
[i
];
413 XFreeFontSet(dpy
, m
->dc
.font
.set
);
415 XFreeFont(dpy
, m
->dc
.font
.xfont
);
416 XUngrabKey(dpy
, AnyKey
, AnyModifier
, m
->root
);
417 XFreePixmap(dpy
, m
->dc
.drawable
);
418 XFreeGC(dpy
, m
->dc
.gc
);
419 XDestroyWindow(dpy
, m
->barwin
);
420 XFreeCursor(dpy
, cursor
[CurNormal
]);
421 XFreeCursor(dpy
, cursor
[CurResize
]);
422 XFreeCursor(dpy
, cursor
[CurMove
]);
423 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
435 regs
= emallocz(LENGTH(rules
) * sizeof(Regs
));
436 for(i
= 0; i
< LENGTH(rules
); i
++) {
438 reg
= emallocz(sizeof(regex_t
));
439 if(regcomp(reg
, rules
[i
].prop
, REG_EXTENDED
))
442 regs
[i
].propregex
= reg
;
445 reg
= emallocz(sizeof(regex_t
));
446 if(regcomp(reg
, rules
[i
].tags
, REG_EXTENDED
))
449 regs
[i
].tagregex
= reg
;
455 configure(Client
*c
) {
458 ce
.type
= ConfigureNotify
;
466 ce
.border_width
= c
->border
;
468 ce
.override_redirect
= False
;
469 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
473 configurenotify(XEvent
*e
) {
474 XConfigureEvent
*ev
= &e
->xconfigure
;
475 Monitor
*m
= &monitors
[selmonitor
];
477 if(ev
->window
== m
->root
&& (ev
->width
!= m
->sw
|| ev
->height
!= m
->sh
)) {
480 XFreePixmap(dpy
, dc
.drawable
);
481 dc
.drawable
= XCreatePixmap(dpy
, m
->root
, m
->sw
, bh
, DefaultDepth(dpy
, m
->screen
));
482 XResizeWindow(dpy
, m
->barwin
, m
->sw
, bh
);
489 configurerequest(XEvent
*e
) {
491 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
494 if((c
= getclient(ev
->window
))) {
495 Monitor
*m
= &monitors
[c
->monitor
];
496 if(ev
->value_mask
& CWBorderWidth
)
497 c
->border
= ev
->border_width
;
498 if(c
->isfixed
|| c
->isfloating
|| (floating
== m
->layout
->arrange
)) {
499 if(ev
->value_mask
& CWX
)
501 if(ev
->value_mask
& CWY
)
503 if(ev
->value_mask
& CWWidth
)
505 if(ev
->value_mask
& CWHeight
)
507 if((c
->x
- m
->sx
+ c
->w
) > m
->sw
&& c
->isfloating
)
508 c
->x
= m
->sx
+ (m
->sw
/ 2 - c
->w
/ 2); /* center in x direction */
509 if((c
->y
- m
->sy
+ c
->h
) > m
->sh
&& c
->isfloating
)
510 c
->y
= m
->sy
+ (m
->sh
/ 2 - c
->h
/ 2); /* center in y direction */
511 if((ev
->value_mask
& (CWX
| CWY
))
512 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
514 if(isvisible(c
, monitorat()))
515 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
523 wc
.width
= ev
->width
;
524 wc
.height
= ev
->height
;
525 wc
.border_width
= ev
->border_width
;
526 wc
.sibling
= ev
->above
;
527 wc
.stack_mode
= ev
->detail
;
528 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
534 destroynotify(XEvent
*e
) {
536 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
538 if((c
= getclient(ev
->window
)))
545 c
->prev
->next
= c
->next
;
547 c
->next
->prev
= c
->prev
;
550 c
->next
= c
->prev
= NULL
;
554 detachstack(Client
*c
) {
557 for(tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
565 for(i
= 0; i
< mcount
; i
++) {
566 Monitor
*m
= &monitors
[i
];
568 for(j
= 0; j
< LENGTH(tags
); j
++) {
569 m
->dc
.w
= textw(m
, tags
[j
]);
571 drawtext(m
, tags
[j
], m
->dc
.sel
, isurgent(i
, j
));
572 drawsquare(m
, sel
&& sel
->tags
[j
] && sel
->monitor
== selmonitor
, isoccupied(m
, j
), m
->dc
.sel
);
575 drawtext(m
, tags
[j
], m
->dc
.norm
, isurgent(i
, j
));
576 drawsquare(m
, sel
&& sel
->tags
[j
] && sel
->monitor
== selmonitor
, isoccupied(m
, j
), m
->dc
.norm
);
581 drawtext(m
, m
->layout
->symbol
, m
->dc
.norm
, False
);
582 x
= m
->dc
.x
+ m
->dc
.w
;
583 m
->dc
.w
= textw(m
, stext
);
584 m
->dc
.x
= m
->sw
- m
->dc
.w
;
589 drawtext(m
, stext
, m
->dc
.norm
, False
);
590 if((m
->dc
.w
= m
->dc
.x
- x
) > bh
) {
592 if(sel
&& sel
->monitor
== selmonitor
) {
593 drawtext(m
, sel
->name
, m
->dc
.sel
, False
);
594 drawsquare(m
, False
, sel
->isfloating
, m
->dc
.sel
);
597 drawtext(m
, NULL
, m
->dc
.norm
, False
);
599 XCopyArea(dpy
, m
->dc
.drawable
, m
->barwin
, m
->dc
.gc
, 0, 0, m
->sw
, bh
, 0, 0);
605 drawsquare(Monitor
*m
, Bool filled
, Bool empty
, unsigned long col
[ColLast
]) {
608 XRectangle r
= { m
->dc
.x
, m
->dc
.y
, m
->dc
.w
, m
->dc
.h
};
610 gcv
.foreground
= col
[ColFG
];
611 XChangeGC(dpy
, m
->dc
.gc
, GCForeground
, &gcv
);
612 x
= (m
->dc
.font
.ascent
+ m
->dc
.font
.descent
+ 2) / 4;
616 r
.width
= r
.height
= x
+ 1;
617 XFillRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
620 r
.width
= r
.height
= x
;
621 XDrawRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
626 drawtext(Monitor
*m
, const char *text
, unsigned long col
[ColLast
], Bool invert
) {
628 static char buf
[256];
629 unsigned int len
, olen
;
630 XRectangle r
= { m
->dc
.x
, m
->dc
.y
, m
->dc
.w
, m
->dc
.h
};
632 XSetForeground(dpy
, m
->dc
.gc
, col
[invert
? ColFG
: ColBG
]);
633 XFillRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
637 olen
= len
= strlen(text
);
638 if(len
>= sizeof buf
)
639 len
= sizeof buf
- 1;
640 memcpy(buf
, text
, len
);
642 h
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
643 y
= m
->dc
.y
+ (m
->dc
.h
/ 2) - (h
/ 2) + m
->dc
.font
.ascent
;
644 x
= m
->dc
.x
+ (h
/ 2);
645 /* shorten text if necessary */
646 while(len
&& (w
= textnw(m
, buf
, len
)) > m
->dc
.w
- h
)
657 return; /* too long */
658 XSetForeground(dpy
, m
->dc
.gc
, col
[invert
? ColBG
: ColFG
]);
660 XmbDrawString(dpy
, m
->dc
.drawable
, m
->dc
.font
.set
, m
->dc
.gc
, x
, y
, buf
, len
);
662 XDrawString(dpy
, m
->dc
.drawable
, m
->dc
.gc
, x
, y
, buf
, len
);
666 emallocz(unsigned int size
) {
667 void *res
= calloc(1, size
);
670 eprint("fatal: could not malloc() %u bytes\n", size
);
675 enternotify(XEvent
*e
) {
677 XCrossingEvent
*ev
= &e
->xcrossing
;
679 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
);
681 if((c
= getclient(ev
->window
)))
684 selmonitor
= monitorat();
685 fprintf(stderr
, "updating selmonitor %d\n", selmonitor
);
691 eprint(const char *errstr
, ...) {
694 va_start(ap
, errstr
);
695 vfprintf(stderr
, errstr
, ap
);
702 XExposeEvent
*ev
= &e
->xexpose
;
705 if(ev
->window
== monitors
[selmonitor
].barwin
)
711 floating(void) { /* default floating layout */
714 domwfact
= dozoom
= False
;
715 for(c
= clients
; c
; c
= c
->next
)
716 if(isvisible(c
, selmonitor
))
717 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
725 selmonitor
= c
->monitor
;
726 m
= &monitors
[selmonitor
];
727 if(!c
|| (c
&& !isvisible(c
, selmonitor
)))
728 for(c
= stack
; c
&& !isvisible(c
, c
->monitor
); c
= c
->snext
);
729 if(sel
&& sel
!= c
) {
730 grabbuttons(sel
, False
);
731 XSetWindowBorder(dpy
, sel
->win
, monitors
[sel
->monitor
].dc
.norm
[ColBorder
]);
736 grabbuttons(c
, True
);
741 XSetWindowBorder(dpy
, c
->win
, m
->dc
.sel
[ColBorder
]);
742 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
743 selmonitor
= c
->monitor
;
746 XSetInputFocus(dpy
, m
->root
, RevertToPointerRoot
, CurrentTime
);
751 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
752 XFocusChangeEvent
*ev
= &e
->xfocus
;
754 if(sel
&& ev
->window
!= sel
->win
)
755 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
759 focusnext(const char *arg
) {
764 for(c
= sel
->next
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
766 for(c
= clients
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
774 focusprev(const char *arg
) {
779 for(c
= sel
->prev
; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
781 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
782 for(; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
791 getclient(Window w
) {
794 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
799 getcolor(const char *colstr
, int screen
) {
800 Colormap cmap
= DefaultColormap(dpy
, screen
);
803 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
804 eprint("error, cannot allocate color '%s'\n", colstr
);
812 unsigned char *p
= NULL
;
813 unsigned long n
, extra
;
816 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
817 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
818 if(status
!= Success
)
827 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
832 if(!text
|| size
== 0)
835 XGetTextProperty(dpy
, w
, &name
, atom
);
838 if(name
.encoding
== XA_STRING
)
839 strncpy(text
, (char *)name
.value
, size
- 1);
841 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
843 strncpy(text
, *list
, size
- 1);
844 XFreeStringList(list
);
847 text
[size
- 1] = '\0';
853 grabbuttons(Client
*c
, Bool focused
) {
854 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
857 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
858 GrabModeAsync
, GrabModeSync
, None
, None
);
859 XGrabButton(dpy
, Button1
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
860 GrabModeAsync
, GrabModeSync
, None
, None
);
861 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
862 GrabModeAsync
, GrabModeSync
, None
, None
);
863 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
864 GrabModeAsync
, GrabModeSync
, None
, None
);
866 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
867 GrabModeAsync
, GrabModeSync
, None
, None
);
868 XGrabButton(dpy
, Button2
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
869 GrabModeAsync
, GrabModeSync
, None
, None
);
870 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
871 GrabModeAsync
, GrabModeSync
, None
, None
);
872 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
873 GrabModeAsync
, GrabModeSync
, None
, None
);
875 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
876 GrabModeAsync
, GrabModeSync
, None
, None
);
877 XGrabButton(dpy
, Button3
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
878 GrabModeAsync
, GrabModeSync
, None
, None
);
879 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
880 GrabModeAsync
, GrabModeSync
, None
, None
);
881 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
882 GrabModeAsync
, GrabModeSync
, None
, None
);
885 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
886 GrabModeAsync
, GrabModeSync
, None
, None
);
893 XModifierKeymap
*modmap
;
895 /* init modifier map */
896 modmap
= XGetModifierMapping(dpy
);
897 for(i
= 0; i
< 8; i
++)
898 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
899 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
900 numlockmask
= (1 << i
);
902 XFreeModifiermap(modmap
);
904 for(i
= 0; i
< mcount
; i
++) {
905 Monitor
*m
= &monitors
[i
];
906 XUngrabKey(dpy
, AnyKey
, AnyModifier
, m
->root
);
907 for(j
= 0; j
< LENGTH(keys
); j
++) {
908 code
= XKeysymToKeycode(dpy
, keys
[j
].keysym
);
909 XGrabKey(dpy
, code
, keys
[j
].mod
, m
->root
, True
,
910 GrabModeAsync
, GrabModeAsync
);
911 XGrabKey(dpy
, code
, keys
[j
].mod
| LockMask
, m
->root
, True
,
912 GrabModeAsync
, GrabModeAsync
);
913 XGrabKey(dpy
, code
, keys
[j
].mod
| numlockmask
, m
->root
, True
,
914 GrabModeAsync
, GrabModeAsync
);
915 XGrabKey(dpy
, code
, keys
[j
].mod
| numlockmask
| LockMask
, m
->root
, True
,
916 GrabModeAsync
, GrabModeAsync
);
922 idxoftag(const char *tag
) {
925 for(i
= 0; (i
< LENGTH(tags
)) && (tags
[i
] != tag
); i
++);
926 return (i
< LENGTH(tags
)) ? i
: 0;
930 initfont(Monitor
*m
, const char *fontstr
) {
931 char *def
, **missing
;
936 XFreeFontSet(dpy
, m
->dc
.font
.set
);
937 m
->dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
940 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
941 XFreeStringList(missing
);
944 XFontSetExtents
*font_extents
;
945 XFontStruct
**xfonts
;
947 m
->dc
.font
.ascent
= m
->dc
.font
.descent
= 0;
948 font_extents
= XExtentsOfFontSet(m
->dc
.font
.set
);
949 n
= XFontsOfFontSet(m
->dc
.font
.set
, &xfonts
, &font_names
);
950 for(i
= 0, m
->dc
.font
.ascent
= 0, m
->dc
.font
.descent
= 0; i
< n
; i
++) {
951 if(m
->dc
.font
.ascent
< (*xfonts
)->ascent
)
952 m
->dc
.font
.ascent
= (*xfonts
)->ascent
;
953 if(m
->dc
.font
.descent
< (*xfonts
)->descent
)
954 m
->dc
.font
.descent
= (*xfonts
)->descent
;
960 XFreeFont(dpy
, m
->dc
.font
.xfont
);
961 m
->dc
.font
.xfont
= NULL
;
962 if(!(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
963 && !(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
964 eprint("error, cannot load font: '%s'\n", fontstr
);
965 m
->dc
.font
.ascent
= m
->dc
.font
.xfont
->ascent
;
966 m
->dc
.font
.descent
= m
->dc
.font
.xfont
->descent
;
968 m
->dc
.font
.height
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
972 isoccupied(Monitor
*m
, unsigned int t
) {
975 for(c
= clients
; c
; c
= c
->next
)
976 if(c
->tags
[t
] && c
->monitor
== selmonitor
)
982 isprotodel(Client
*c
) {
987 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
988 for(i
= 0; !ret
&& i
< n
; i
++)
989 if(protocols
[i
] == wmatom
[WMDelete
])
997 isurgent(int monitor
, unsigned int t
) {
1000 for(c
= clients
; c
; c
= c
->next
)
1001 if(c
->monitor
== monitor
&& c
->isurgent
&& c
->tags
[t
])
1007 isvisible(Client
*c
, int monitor
) {
1010 for(i
= 0; i
< LENGTH(tags
); i
++)
1011 if(c
->tags
[i
] && monitors
[c
->monitor
].seltags
[i
] && c
->monitor
== monitor
)
1017 keypress(XEvent
*e
) {
1023 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1024 for(i
= 0; i
< LENGTH(keys
); i
++)
1025 if(keysym
== keys
[i
].keysym
1026 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
1029 keys
[i
].func(keys
[i
].arg
);
1034 killclient(const char *arg
) {
1039 if(isprotodel(sel
)) {
1040 ev
.type
= ClientMessage
;
1041 ev
.xclient
.window
= sel
->win
;
1042 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1043 ev
.xclient
.format
= 32;
1044 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1045 ev
.xclient
.data
.l
[1] = CurrentTime
;
1046 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
1049 XKillClient(dpy
, sel
->win
);
1053 manage(Window w
, XWindowAttributes
*wa
) {
1054 Client
*c
, *t
= NULL
;
1060 c
= emallocz(sizeof(Client
));
1061 c
->tags
= emallocz(sizeof initags
);
1066 m
= &monitors
[c
->monitor
];
1068 c
->x
= wa
->x
+ m
->sx
;
1069 c
->y
= wa
->y
+ m
->sy
;
1072 c
->oldborder
= wa
->border_width
;
1074 if(c
->w
== m
->sw
&& c
->h
== m
->sh
) {
1077 c
->border
= wa
->border_width
;
1080 if(c
->x
+ c
->w
+ 2 * c
->border
> m
->wax
+ m
->waw
)
1081 c
->x
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1082 if(c
->y
+ c
->h
+ 2 * c
->border
> m
->way
+ m
->wah
)
1083 c
->y
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1088 c
->border
= BORDERPX
;
1090 wc
.border_width
= c
->border
;
1091 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1092 XSetWindowBorder(dpy
, w
, m
->dc
.norm
[ColBorder
]);
1093 configure(c
); /* propagates border_width, if size doesn't change */
1095 XSelectInput(dpy
, w
, EnterWindowMask
| FocusChangeMask
| PropertyChangeMask
| StructureNotifyMask
);
1096 grabbuttons(c
, False
);
1098 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
1099 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
1101 memcpy(c
->tags
, t
->tags
, sizeof initags
);
1103 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
1106 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1108 XMapWindow(dpy
, c
->win
);
1109 setclientstate(c
, NormalState
);
1114 mappingnotify(XEvent
*e
) {
1115 XMappingEvent
*ev
= &e
->xmapping
;
1117 XRefreshKeyboardMapping(ev
);
1118 if(ev
->request
== MappingKeyboard
)
1123 maprequest(XEvent
*e
) {
1124 static XWindowAttributes wa
;
1125 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1127 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1129 if(wa
.override_redirect
)
1131 if(!getclient(ev
->window
))
1132 manage(ev
->window
, &wa
);
1141 XQueryPointer(dpy
, monitors
[selmonitor
].root
, &win
, &win
, &x
, &y
, &i
, &i
, &mask
);
1142 for(i
= 0; i
< mcount
; i
++) {
1143 fprintf(stderr
, "checking monitor[%d]: %d %d %d %d\n", i
, monitors
[i
].sx
, monitors
[i
].sy
, monitors
[i
].sw
, monitors
[i
].sh
);
1144 if((x
>= monitors
[i
].sx
&& x
< monitors
[i
].sx
+ monitors
[i
].sw
)
1145 && (y
>= monitors
[i
].sy
&& y
< monitors
[i
].sy
+ monitors
[i
].sh
)) {
1146 fprintf(stderr
, "%d,%d -> %d\n", x
, y
, i
);
1150 fprintf(stderr
, "?,? -> 0\n");
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
))
1726 for(j
= 0, c
= mc
= 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
)
1732 if(j
== 0) { /* master */
1735 nw
= mw
- 2 * c
->border
;
1736 nh
= m
->wah
- 2 * c
->border
;
1738 else { /* tile window */
1741 nx
+= mc
->w
+ 2 * mc
->border
;
1742 nw
= m
->waw
- mw
- 2 * c
->border
;
1744 if(j
+ 1 == n
) /* remainder */
1745 nh
= (m
->way
+ m
->wah
) - ny
- 2 * c
->border
;
1747 nh
= th
- 2 * c
->border
;
1749 fprintf(stderr
, "tile(%d, %d, %d, %d)\n", nx
, ny
, nw
, nh
);
1750 resize(c
, nx
, ny
, nw
, nh
, RESIZEHINTS
);
1751 if((RESIZEHINTS
) && ((c
->h
< bh
) || (c
->h
> nh
) || (c
->w
< bh
) || (c
->w
> nw
)))
1752 /* client doesn't accept size constraints */
1753 resize(c
, nx
, ny
, nw
, nh
, False
);
1754 if(n
> 1 && th
!= m
->wah
)
1755 ny
= c
->y
+ c
->h
+ 2 * c
->border
;
1760 fprintf(stderr
, "done\n");
1763 togglebar(const char *arg
) {
1765 bpos
= (BARPOS
== BarOff
) ? BarTop
: BARPOS
;
1768 updatebarpos(&monitors
[monitorat()]);
1773 togglefloating(const char *arg
) {
1776 sel
->isfloating
= !sel
->isfloating
;
1778 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1783 toggletag(const char *arg
) {
1789 sel
->tags
[i
] = !sel
->tags
[i
];
1790 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1791 if(j
== LENGTH(tags
))
1792 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1797 toggleview(const char *arg
) {
1800 Monitor
*m
= &monitors
[monitorat()];
1803 m
->seltags
[i
] = !m
->seltags
[i
];
1804 for(j
= 0; j
< LENGTH(tags
) && !m
->seltags
[j
]; j
++);
1805 if(j
== LENGTH(tags
))
1806 m
->seltags
[i
] = True
; /* at least one tag must be viewed */
1814 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1815 c
->isbanned
= False
;
1819 unmanage(Client
*c
) {
1822 wc
.border_width
= c
->oldborder
;
1823 /* The server grab construct avoids race conditions. */
1825 XSetErrorHandler(xerrordummy
);
1826 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1831 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1832 setclientstate(c
, WithdrawnState
);
1836 XSetErrorHandler(xerror
);
1842 unmapnotify(XEvent
*e
) {
1844 XUnmapEvent
*ev
= &e
->xunmap
;
1846 if((c
= getclient(ev
->window
)))
1851 updatebarpos(Monitor
*m
) {
1862 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
);
1866 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
+ m
->wah
);
1869 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
- bh
);
1873 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1877 updatesizehints(Client
*c
) {
1881 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1883 c
->flags
= size
.flags
;
1884 if(c
->flags
& PBaseSize
) {
1885 c
->basew
= size
.base_width
;
1886 c
->baseh
= size
.base_height
;
1888 else if(c
->flags
& PMinSize
) {
1889 c
->basew
= size
.min_width
;
1890 c
->baseh
= size
.min_height
;
1893 c
->basew
= c
->baseh
= 0;
1894 if(c
->flags
& PResizeInc
) {
1895 c
->incw
= size
.width_inc
;
1896 c
->inch
= size
.height_inc
;
1899 c
->incw
= c
->inch
= 0;
1900 if(c
->flags
& PMaxSize
) {
1901 c
->maxw
= size
.max_width
;
1902 c
->maxh
= size
.max_height
;
1905 c
->maxw
= c
->maxh
= 0;
1906 if(c
->flags
& PMinSize
) {
1907 c
->minw
= size
.min_width
;
1908 c
->minh
= size
.min_height
;
1910 else if(c
->flags
& PBaseSize
) {
1911 c
->minw
= size
.base_width
;
1912 c
->minh
= size
.base_height
;
1915 c
->minw
= c
->minh
= 0;
1916 if(c
->flags
& PAspect
) {
1917 c
->minax
= size
.min_aspect
.x
;
1918 c
->maxax
= size
.max_aspect
.x
;
1919 c
->minay
= size
.min_aspect
.y
;
1920 c
->maxay
= size
.max_aspect
.y
;
1923 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1924 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1925 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1929 updatetitle(Client
*c
) {
1930 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1931 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1935 updatewmhints(Client
*c
) {
1938 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1939 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1944 /* There's no way to check accesses to destroyed windows, thus those cases are
1945 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1946 * default error handler, which may call exit. */
1948 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1949 if(ee
->error_code
== BadWindow
1950 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1951 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1952 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1953 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1954 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1955 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1956 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1958 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1959 ee
->request_code
, ee
->error_code
);
1960 return xerrorxlib(dpy
, ee
); /* may call exit */
1964 xerrordummy(Display
*dsply
, XErrorEvent
*ee
) {
1968 /* Startup Error handler to check if another window manager
1969 * is already running. */
1971 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
1977 view(const char *arg
) {
1979 Bool tmp
[LENGTH(tags
)];
1980 Monitor
*m
= &monitors
[monitorat()];
1982 for(i
= 0; i
< LENGTH(tags
); i
++)
1983 tmp
[i
] = (NULL
== arg
);
1984 tmp
[idxoftag(arg
)] = True
;
1985 if(memcmp(m
->seltags
, tmp
, sizeof initags
) != 0) {
1986 memcpy(m
->prevtags
, m
->seltags
, sizeof initags
);
1987 memcpy(m
->seltags
, tmp
, sizeof initags
);
1993 viewprevtag(const char *arg
) {
1994 static Bool tmp
[LENGTH(tags
)];
1996 Monitor
*m
= &monitors
[monitorat()];
1998 memcpy(tmp
, m
->seltags
, sizeof initags
);
1999 memcpy(m
->seltags
, m
->prevtags
, sizeof initags
);
2000 memcpy(m
->prevtags
, tmp
, sizeof initags
);
2005 zoom(const char *arg
) {
2008 if(!sel
|| !dozoom
|| sel
->isfloating
)
2010 if((c
= sel
) == nexttiled(clients
, c
->monitor
))
2011 if(!(c
= nexttiled(c
->next
, c
->monitor
)))
2020 movetomonitor(const char *arg
) {
2022 sel
->monitor
= arg
? atoi(arg
) : (sel
->monitor
+1) % mcount
;
2024 memcpy(sel
->tags
, monitors
[sel
->monitor
].seltags
, sizeof initags
);
2025 resize(sel
, monitors
[sel
->monitor
].wax
, monitors
[sel
->monitor
].way
, sel
->w
, sel
->h
, True
);
2031 selectmonitor(const char *arg
) {
2032 Monitor
*m
= &monitors
[arg
? atoi(arg
) : (monitorat()+1) % mcount
];
2034 XWarpPointer(dpy
, None
, m
->root
, 0, 0, 0, 0, m
->wax
+m
->waw
/2, m
->way
+m
->wah
/2);
2040 main(int argc
, char *argv
[]) {
2041 if(argc
== 2 && !strcmp("-v", argv
[1]))
2042 eprint("dwm-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
2043 "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
2045 eprint("usage: dwm [-v]\n");
2047 setlocale(LC_CTYPE
, "");
2048 if(!(dpy
= XOpenDisplay(0)))
2049 eprint("dwm: cannot open display\n");