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 Monitor Monitor
;
64 typedef struct Client Client
;
68 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
69 int minax
, maxax
, minay
, maxay
;
71 unsigned int border
, oldborder
;
72 Bool isbanned
, isfixed
, isfloating
, isurgent
;
83 unsigned long norm
[ColLast
];
84 unsigned long sel
[ColLast
];
94 } DC
; /* draw context */
99 void (*func
)(const char *arg
);
105 void (*arrange
)(Monitor
*);
122 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
);
147 void drawbar(Monitor
*m
);
148 void drawsquare(Monitor
*m
, Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
149 void drawtext(Monitor
*m
, 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(Monitor
*m
); /* 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
);
161 Monitor
*getmonitor(Window barwin
);
162 long getstate(Window w
);
163 Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
164 void grabbuttons(Client
*c
, Bool focused
);
166 unsigned int idxoftag(const char *tag
);
167 void initfont(const char *fontstr
);
168 Bool
isoccupied(Monitor
*monitor
, unsigned int t
);
169 Bool
isprotodel(Client
*c
);
170 Bool
isurgent(Monitor
*monitor
, unsigned int t
);
171 Bool
isvisible(Client
*c
, Monitor
*m
);
172 void keypress(XEvent
*e
);
173 void killclient(const char *arg
);
174 void manage(Window w
, XWindowAttributes
*wa
);
175 void mappingnotify(XEvent
*e
);
176 void maprequest(XEvent
*e
);
177 Monitor
*monitorat(void);
178 void movemouse(Client
*c
);
179 Client
*nexttiled(Client
*c
, Monitor
*monitor
);
180 void propertynotify(XEvent
*e
);
181 void quit(const char *arg
);
182 void reapply(const char *arg
);
183 void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
);
184 void resizemouse(Client
*c
);
185 void restack(Monitor
*m
);
188 void setclientstate(Client
*c
, long state
);
189 void setlayout(const char *arg
);
190 void setmwfact(const char *arg
);
192 void spawn(const char *arg
);
193 void tag(const char *arg
);
194 unsigned int textnw(const char *text
, unsigned int len
);
195 unsigned int textw(const char *text
);
196 void tile(Monitor
*m
);
197 void togglebar(const char *arg
);
198 void togglefloating(const char *arg
);
199 void toggletag(const char *arg
);
200 void toggleview(const char *arg
);
201 void unban(Client
*c
);
202 void unmanage(Client
*c
);
203 void unmapnotify(XEvent
*e
);
204 void updatebarpos(Monitor
*m
);
205 void updatesizehints(Client
*c
);
206 void updatetitle(Client
*c
);
207 void updatewmhints(Client
*c
);
208 void view(const char *arg
);
209 void viewprevtag(const char *arg
); /* views previous selected tags */
210 int xerror(Display
*dpy
, XErrorEvent
*ee
);
211 int xerrordummy(Display
*dsply
, XErrorEvent
*ee
);
212 int xerrorstart(Display
*dsply
, XErrorEvent
*ee
);
213 void zoom(const char *arg
);
214 void movetomonitor(const char *arg
);
215 void selectmonitor(const char *arg
);
222 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
223 unsigned int bh
, bpos
;
224 unsigned int blw
= 0;
225 unsigned int numlockmask
= 0;
226 void (*handler
[LASTEvent
]) (XEvent
*) = {
227 [ButtonPress
] = buttonpress
,
228 [ConfigureRequest
] = configurerequest
,
229 [ConfigureNotify
] = configurenotify
,
230 [DestroyNotify
] = destroynotify
,
231 [EnterNotify
] = enternotify
,
234 [KeyPress
] = keypress
,
235 [MappingNotify
] = mappingnotify
,
236 [MapRequest
] = maprequest
,
237 [PropertyNotify
] = propertynotify
,
238 [UnmapNotify
] = unmapnotify
240 Atom wmatom
[WMLast
], netatom
[NetLast
];
241 Bool isxinerama
= False
;
242 Bool domwfact
= True
;
244 Bool otherwm
, readin
;
246 Client
*clients
= NULL
;
248 Client
*stack
= NULL
;
249 Cursor cursor
[CurLast
];
256 /* configuration, allows nested code to access above variables */
259 //Bool prevtags[LENGTH(tags)];
261 /* function implementations */
263 applyrules(Client
*c
) {
264 static char buf
[512];
267 Bool matched_tag
= False
;
268 Bool matched_monitor
= False
;
269 XClassHint ch
= { 0 };
272 XGetClassHint(dpy
, c
->win
, &ch
);
273 snprintf(buf
, sizeof buf
, "%s:%s:%s",
274 ch
.res_class
? ch
.res_class
: "",
275 ch
.res_name
? ch
.res_name
: "", c
->name
);
276 for(i
= 0; i
< LENGTH(rules
); i
++)
277 if(regs
[i
].propregex
&& !regexec(regs
[i
].propregex
, buf
, 1, &tmp
, 0)) {
278 if (rules
[i
].monitor
>= 0 && rules
[i
].monitor
< mcount
) {
279 matched_monitor
= True
;
280 c
->monitor
= &monitors
[rules
[i
].monitor
];
283 c
->isfloating
= rules
[i
].isfloating
;
284 for(j
= 0; regs
[i
].tagregex
&& j
< LENGTH(tags
); j
++) {
285 if(!regexec(regs
[i
].tagregex
, tags
[j
], 1, &tmp
, 0)) {
296 memcpy(c
->tags
, monitorat()->seltags
, sizeof initags
);
297 if (!matched_monitor
)
298 c
->monitor
= monitorat();
305 for(c
= clients
; c
; c
= c
->next
)
306 if(isvisible(c
, c
->monitor
))
311 selmonitor
->layout
->arrange(selmonitor
);
325 attachstack(Client
*c
) {
334 XMoveWindow(dpy
, c
->win
, c
->x
+ 3 * c
->monitor
->sw
, c
->y
);
339 buttonpress(XEvent
*e
) {
342 XButtonPressedEvent
*ev
= &e
->xbutton
;
344 Monitor
*m
= monitorat();
346 if(ev
->window
== m
->barwin
) {
348 for(i
= 0; i
< LENGTH(tags
); i
++) {
351 if(ev
->button
== Button1
) {
352 if(ev
->state
& MODKEY
)
357 else if(ev
->button
== Button3
) {
358 if(ev
->state
& MODKEY
)
366 if((ev
->x
< x
+ blw
) && ev
->button
== Button1
)
369 else if((c
= getclient(ev
->window
))) {
371 if(CLEANMASK(ev
->state
) != MODKEY
)
373 if(ev
->button
== Button1
) {
377 else if(ev
->button
== Button2
) {
378 if((floating
!= m
->layout
->arrange
) && c
->isfloating
)
379 togglefloating(NULL
);
383 else if(ev
->button
== Button3
&& !c
->isfixed
) {
393 XSetErrorHandler(xerrorstart
);
395 /* this causes an error if some other window manager is running */
396 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
399 eprint("dwm: another window manager is already running\n");
401 XSetErrorHandler(NULL
);
402 xerrorxlib
= XSetErrorHandler(xerror
);
415 XFreeFontSet(dpy
, dc
.font
.set
);
417 XFreeFont(dpy
, dc
.font
.xfont
);
419 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
420 XFreePixmap(dpy
, dc
.drawable
);
422 XFreeCursor(dpy
, cursor
[CurNormal
]);
423 XFreeCursor(dpy
, cursor
[CurResize
]);
424 XFreeCursor(dpy
, cursor
[CurMove
]);
425 for(i
= 0; i
< mcount
; i
++)
426 XDestroyWindow(dpy
, monitors
[i
].barwin
);
428 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
438 regs
= emallocz(LENGTH(rules
) * sizeof(Regs
));
439 for(i
= 0; i
< LENGTH(rules
); i
++) {
441 reg
= emallocz(sizeof(regex_t
));
442 if(regcomp(reg
, rules
[i
].prop
, REG_EXTENDED
))
445 regs
[i
].propregex
= reg
;
448 reg
= emallocz(sizeof(regex_t
));
449 if(regcomp(reg
, rules
[i
].tags
, REG_EXTENDED
))
452 regs
[i
].tagregex
= reg
;
458 configure(Client
*c
) {
461 ce
.type
= ConfigureNotify
;
469 ce
.border_width
= c
->border
;
471 ce
.override_redirect
= False
;
472 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
476 configurenotify(XEvent
*e
) {
477 XConfigureEvent
*ev
= &e
->xconfigure
;
478 Monitor
*m
= selmonitor
;
480 if(ev
->window
== root
&& (ev
->width
!= m
->sw
|| ev
->height
!= m
->sh
)) {
481 /* TODO -- update Xinerama dimensions here */
484 XFreePixmap(dpy
, dc
.drawable
);
485 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(root
, screen
), bh
, DefaultDepth(dpy
, screen
));
486 XResizeWindow(dpy
, m
->barwin
, m
->sw
, bh
);
493 configurerequest(XEvent
*e
) {
495 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
498 if((c
= getclient(ev
->window
))) {
499 Monitor
*m
= c
->monitor
;
500 if(ev
->value_mask
& CWBorderWidth
)
501 c
->border
= ev
->border_width
;
502 if(c
->isfixed
|| c
->isfloating
|| (floating
== m
->layout
->arrange
)) {
503 if(ev
->value_mask
& CWX
)
505 if(ev
->value_mask
& CWY
)
507 if(ev
->value_mask
& CWWidth
)
509 if(ev
->value_mask
& CWHeight
)
511 if((c
->x
- m
->sx
+ c
->w
) > m
->sw
&& c
->isfloating
)
512 c
->x
= m
->sx
+ (m
->sw
/ 2 - c
->w
/ 2); /* center in x direction */
513 if((c
->y
- m
->sy
+ c
->h
) > m
->sh
&& c
->isfloating
)
514 c
->y
= m
->sy
+ (m
->sh
/ 2 - c
->h
/ 2); /* center in y direction */
515 if((ev
->value_mask
& (CWX
| CWY
))
516 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
518 if(isvisible(c
, monitorat()))
519 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
527 wc
.width
= ev
->width
;
528 wc
.height
= ev
->height
;
529 wc
.border_width
= ev
->border_width
;
530 wc
.sibling
= ev
->above
;
531 wc
.stack_mode
= ev
->detail
;
532 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
538 destroynotify(XEvent
*e
) {
540 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
542 if((c
= getclient(ev
->window
)))
549 c
->prev
->next
= c
->next
;
551 c
->next
->prev
= c
->prev
;
554 c
->next
= c
->prev
= NULL
;
558 detachstack(Client
*c
) {
561 for(tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
566 drawbar(Monitor
*m
) {
571 for(c
= stack
; c
&& !isvisible(c
, m
); c
= c
->snext
);
572 for(j
= 0; j
< LENGTH(tags
); j
++) {
573 dc
.w
= textw(tags
[j
]);
575 drawtext(m
, tags
[j
], dc
.sel
, isurgent(m
, j
));
576 drawsquare(m
, c
&& c
->tags
[j
] && c
->monitor
== m
,
577 isoccupied(m
, j
), isurgent(m
, j
), dc
.sel
);
580 drawtext(m
, tags
[j
], dc
.norm
, isurgent(m
, j
));
581 drawsquare(m
, c
&& c
->tags
[j
] && c
->monitor
== m
,
582 isoccupied(m
, j
), isurgent(m
, j
), dc
.norm
);
587 drawtext(m
, m
->layout
->symbol
, dc
.norm
, False
);
589 if(m
== selmonitor
) {
596 drawtext(m
, stext
, dc
.norm
, False
);
600 if((dc
.w
= dc
.x
- x
) > bh
) {
603 drawtext(m
, c
->name
, dc
.sel
, False
);
604 drawsquare(m
, False
, c
->isfloating
, False
, dc
.sel
);
607 drawtext(m
, NULL
, dc
.norm
, False
);
609 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->sw
, bh
, 0, 0);
614 drawsquare(Monitor
*m
, Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
617 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
619 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
620 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
621 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
625 r
.width
= r
.height
= x
+ 1;
626 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
629 r
.width
= r
.height
= x
;
630 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
635 drawtext(Monitor
*m
, const char *text
, unsigned long col
[ColLast
], Bool invert
) {
637 static char buf
[256];
638 unsigned int len
, olen
;
639 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
641 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
642 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
646 olen
= len
= strlen(text
);
647 if(len
>= sizeof buf
)
648 len
= sizeof buf
- 1;
649 memcpy(buf
, text
, len
);
651 h
= dc
.font
.ascent
+ dc
.font
.descent
;
652 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
654 /* shorten text if necessary */
655 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
666 return; /* too long */
667 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
669 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
671 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
675 emallocz(unsigned int size
) {
676 void *res
= calloc(1, size
);
679 eprint("fatal: could not malloc() %u bytes\n", size
);
684 enternotify(XEvent
*e
) {
686 XCrossingEvent
*ev
= &e
->xcrossing
;
688 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) {
689 if(!isxinerama
|| ev
->window
!= root
)
692 if((c
= getclient(ev
->window
)))
695 selmonitor
= monitorat();
696 fprintf(stderr
, "updating selmonitor %d\n", selmonitor
- monitors
);
702 eprint(const char *errstr
, ...) {
705 va_start(ap
, errstr
);
706 vfprintf(stderr
, errstr
, ap
);
714 XExposeEvent
*ev
= &e
->xexpose
;
716 if(ev
->count
== 0 && (m
= getmonitor(ev
->window
)))
721 floating(Monitor
*m
) { /* default floating layout */
724 domwfact
= dozoom
= False
;
725 for(c
= clients
; c
; c
= c
->next
)
727 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
733 selmonitor
= c
->monitor
;
734 if(!c
|| (c
&& !isvisible(c
, selmonitor
)))
735 for(c
= stack
; c
&& !isvisible(c
, c
->monitor
); c
= c
->snext
);
736 if(sel
&& sel
!= c
) {
737 grabbuttons(sel
, False
);
738 XSetWindowBorder(dpy
, sel
->win
, dc
.norm
[ColBorder
]);
743 grabbuttons(c
, True
);
747 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
748 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
749 selmonitor
= c
->monitor
;
752 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
757 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
758 XFocusChangeEvent
*ev
= &e
->xfocus
;
760 if(sel
&& ev
->window
!= sel
->win
)
761 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
765 focusnext(const char *arg
) {
770 for(c
= sel
->next
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
772 for(c
= clients
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
780 focusprev(const char *arg
) {
785 for(c
= sel
->prev
; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
787 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
788 for(; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
797 getclient(Window w
) {
800 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
805 getcolor(const char *colstr
) {
806 Colormap cmap
= DefaultColormap(dpy
, screen
);
809 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
810 eprint("error, cannot allocate color '%s'\n", colstr
);
815 getmonitor(Window barwin
) {
818 for(i
= 0; i
< mcount
; i
++)
819 if(monitors
[i
].barwin
== barwin
)
828 unsigned char *p
= NULL
;
829 unsigned long n
, extra
;
832 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
833 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
834 if(status
!= Success
)
843 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
848 if(!text
|| size
== 0)
851 XGetTextProperty(dpy
, w
, &name
, atom
);
854 if(name
.encoding
== XA_STRING
)
855 strncpy(text
, (char *)name
.value
, size
- 1);
857 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
859 strncpy(text
, *list
, size
- 1);
860 XFreeStringList(list
);
863 text
[size
- 1] = '\0';
869 grabbuttons(Client
*c
, Bool focused
) {
870 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
873 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
874 GrabModeAsync
, GrabModeSync
, None
, None
);
875 XGrabButton(dpy
, Button1
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
876 GrabModeAsync
, GrabModeSync
, None
, None
);
877 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
878 GrabModeAsync
, GrabModeSync
, None
, None
);
879 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
880 GrabModeAsync
, GrabModeSync
, None
, None
);
882 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
883 GrabModeAsync
, GrabModeSync
, None
, None
);
884 XGrabButton(dpy
, Button2
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
885 GrabModeAsync
, GrabModeSync
, None
, None
);
886 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
887 GrabModeAsync
, GrabModeSync
, None
, None
);
888 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
889 GrabModeAsync
, GrabModeSync
, None
, None
);
891 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
892 GrabModeAsync
, GrabModeSync
, None
, None
);
893 XGrabButton(dpy
, Button3
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
894 GrabModeAsync
, GrabModeSync
, None
, None
);
895 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
896 GrabModeAsync
, GrabModeSync
, None
, None
);
897 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
898 GrabModeAsync
, GrabModeSync
, None
, None
);
901 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
902 GrabModeAsync
, GrabModeSync
, None
, None
);
909 XModifierKeymap
*modmap
;
911 /* init modifier map */
912 modmap
= XGetModifierMapping(dpy
);
913 for(i
= 0; i
< 8; i
++)
914 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
915 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
916 numlockmask
= (1 << i
);
918 XFreeModifiermap(modmap
);
920 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
921 for(i
= 0; i
< LENGTH(keys
); i
++) {
922 code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
);
923 XGrabKey(dpy
, code
, keys
[i
].mod
, root
, True
,
924 GrabModeAsync
, GrabModeAsync
);
925 XGrabKey(dpy
, code
, keys
[i
].mod
| LockMask
, root
, True
,
926 GrabModeAsync
, GrabModeAsync
);
927 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
, root
, True
,
928 GrabModeAsync
, GrabModeAsync
);
929 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
| LockMask
, root
, True
,
930 GrabModeAsync
, GrabModeAsync
);
935 idxoftag(const char *tag
) {
938 for(i
= 0; (i
< LENGTH(tags
)) && (tags
[i
] != tag
); i
++);
939 return (i
< LENGTH(tags
)) ? i
: 0;
943 initfont(const char *fontstr
) {
944 char *def
, **missing
;
949 XFreeFontSet(dpy
, dc
.font
.set
);
950 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
953 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
954 XFreeStringList(missing
);
957 XFontSetExtents
*font_extents
;
958 XFontStruct
**xfonts
;
960 dc
.font
.ascent
= dc
.font
.descent
= 0;
961 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
962 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
963 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
964 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
965 dc
.font
.ascent
= (*xfonts
)->ascent
;
966 if(dc
.font
.descent
< (*xfonts
)->descent
)
967 dc
.font
.descent
= (*xfonts
)->descent
;
973 XFreeFont(dpy
, dc
.font
.xfont
);
974 dc
.font
.xfont
= NULL
;
975 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
976 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
977 eprint("error, cannot load font: '%s'\n", fontstr
);
978 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
979 dc
.font
.descent
= dc
.font
.xfont
->descent
;
981 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
985 isoccupied(Monitor
*monitor
, unsigned int t
) {
988 for(c
= clients
; c
; c
= c
->next
)
989 if(c
->tags
[t
] && c
->monitor
== monitor
)
995 isprotodel(Client
*c
) {
1000 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1001 for(i
= 0; !ret
&& i
< n
; i
++)
1002 if(protocols
[i
] == wmatom
[WMDelete
])
1010 isurgent(Monitor
*monitor
, unsigned int t
) {
1013 for(c
= clients
; c
; c
= c
->next
)
1014 if(c
->monitor
== monitor
&& c
->isurgent
&& c
->tags
[t
])
1020 isvisible(Client
*c
, Monitor
*m
) {
1025 for(i
= 0; i
< LENGTH(tags
); i
++)
1026 if(c
->tags
[i
] && c
->monitor
->seltags
[i
])
1032 keypress(XEvent
*e
) {
1038 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1039 for(i
= 0; i
< LENGTH(keys
); i
++)
1040 if(keysym
== keys
[i
].keysym
1041 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
1044 keys
[i
].func(keys
[i
].arg
);
1049 killclient(const char *arg
) {
1054 if(isprotodel(sel
)) {
1055 ev
.type
= ClientMessage
;
1056 ev
.xclient
.window
= sel
->win
;
1057 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1058 ev
.xclient
.format
= 32;
1059 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1060 ev
.xclient
.data
.l
[1] = CurrentTime
;
1061 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
1064 XKillClient(dpy
, sel
->win
);
1068 manage(Window w
, XWindowAttributes
*wa
) {
1069 Client
*c
, *t
= NULL
;
1075 c
= emallocz(sizeof(Client
));
1076 c
->tags
= emallocz(sizeof initags
);
1083 c
->x
= wa
->x
+ m
->sx
;
1084 c
->y
= wa
->y
+ m
->sy
;
1087 c
->oldborder
= wa
->border_width
;
1089 if(c
->w
== m
->sw
&& c
->h
== m
->sh
) {
1092 c
->border
= wa
->border_width
;
1095 if(c
->x
+ c
->w
+ 2 * c
->border
> m
->wax
+ m
->waw
)
1096 c
->x
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1097 if(c
->y
+ c
->h
+ 2 * c
->border
> m
->way
+ m
->wah
)
1098 c
->y
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1103 c
->border
= BORDERPX
;
1105 wc
.border_width
= c
->border
;
1106 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1107 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1108 configure(c
); /* propagates border_width, if size doesn't change */
1110 XSelectInput(dpy
, w
, EnterWindowMask
| FocusChangeMask
| PropertyChangeMask
| StructureNotifyMask
);
1111 grabbuttons(c
, False
);
1113 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
1114 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
1116 memcpy(c
->tags
, t
->tags
, sizeof initags
);
1118 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
1121 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1123 XMapWindow(dpy
, c
->win
);
1124 setclientstate(c
, NormalState
);
1129 mappingnotify(XEvent
*e
) {
1130 XMappingEvent
*ev
= &e
->xmapping
;
1132 XRefreshKeyboardMapping(ev
);
1133 if(ev
->request
== MappingKeyboard
)
1138 maprequest(XEvent
*e
) {
1139 static XWindowAttributes wa
;
1140 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1142 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1144 if(wa
.override_redirect
)
1146 if(!getclient(ev
->window
))
1147 manage(ev
->window
, &wa
);
1156 XQueryPointer(dpy
, root
, &win
, &win
, &x
, &y
, &i
, &i
, &mask
);
1157 for(i
= 0; i
< mcount
; i
++) {
1158 if((x
>= monitors
[i
].sx
&& x
< monitors
[i
].sx
+ monitors
[i
].sw
)
1159 && (y
>= monitors
[i
].sy
&& y
< monitors
[i
].sy
+ monitors
[i
].sh
)) {
1160 return &monitors
[i
];
1167 movemouse(Client
*c
) {
1168 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1177 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1178 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1180 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1182 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1185 XUngrabPointer(dpy
, CurrentTime
);
1187 case ConfigureRequest
:
1190 handler
[ev
.type
](&ev
);
1194 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1195 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1196 if(abs(m
->wax
- nx
) < SNAP
)
1198 else if(abs((m
->wax
+ m
->waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
1199 nx
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1200 if(abs(m
->way
- ny
) < SNAP
)
1202 else if(abs((m
->way
+ m
->wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
1203 ny
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1204 if((m
->layout
->arrange
!= floating
) && (abs(nx
- c
->x
) > SNAP
|| abs(ny
- c
->y
) > SNAP
))
1205 togglefloating(NULL
);
1206 if((m
->layout
->arrange
== floating
) || c
->isfloating
)
1207 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1214 nexttiled(Client
*c
, Monitor
*monitor
) {
1215 for(; c
&& (c
->isfloating
|| !isvisible(c
, monitor
)); c
= c
->next
);
1220 propertynotify(XEvent
*e
) {
1223 XPropertyEvent
*ev
= &e
->xproperty
;
1225 if(ev
->state
== PropertyDelete
)
1226 return; /* ignore */
1227 if((c
= getclient(ev
->window
))) {
1230 case XA_WM_TRANSIENT_FOR
:
1231 XGetTransientForHint(dpy
, c
->win
, &trans
);
1232 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1235 case XA_WM_NORMAL_HINTS
:
1240 drawbar(c
->monitor
);
1243 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1246 drawbar(c
->monitor
);
1252 quit(const char *arg
) {
1253 readin
= running
= False
;
1257 reapply(const char *arg
) {
1258 static Bool zerotags
[LENGTH(tags
)] = { 0 };
1261 for(c
= clients
; c
; c
= c
->next
) {
1262 memcpy(c
->tags
, zerotags
, sizeof zerotags
);
1269 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1276 /* set minimum possible */
1282 /* temporarily remove base dimensions */
1286 /* adjust for aspect limits */
1287 if (c
->minay
> 0 && c
->maxay
> 0 && c
->minax
> 0 && c
->maxax
> 0) {
1288 if (w
* c
->maxay
> h
* c
->maxax
)
1289 w
= h
* c
->maxax
/ c
->maxay
;
1290 else if (w
* c
->minay
< h
* c
->minax
)
1291 h
= w
* c
->minay
/ c
->minax
;
1294 /* adjust for increment value */
1300 /* restore base dimensions */
1304 if(c
->minw
> 0 && w
< c
->minw
)
1306 if(c
->minh
> 0 && h
< c
->minh
)
1308 if(c
->maxw
> 0 && w
> c
->maxw
)
1310 if(c
->maxh
> 0 && h
> c
->maxh
)
1313 if(w
<= 0 || h
<= 0)
1316 x
= m
->sw
- w
- 2 * c
->border
;
1318 y
= m
->sh
- h
- 2 * c
->border
;
1319 if(x
+ w
+ 2 * c
->border
< m
->sx
)
1321 if(y
+ h
+ 2 * c
->border
< m
->sy
)
1323 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1326 c
->w
= wc
.width
= w
;
1327 c
->h
= wc
.height
= h
;
1328 wc
.border_width
= c
->border
;
1329 XConfigureWindow(dpy
, c
->win
, CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
, &wc
);
1336 resizemouse(Client
*c
) {
1345 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1346 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1348 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1350 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1353 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1354 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1355 XUngrabPointer(dpy
, CurrentTime
);
1356 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1358 case ConfigureRequest
:
1361 handler
[ev
.type
](&ev
);
1365 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
1367 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
1369 if((m
->layout
->arrange
!= floating
) && (abs(nw
- c
->w
) > SNAP
|| abs(nh
- c
->h
) > SNAP
))
1370 togglefloating(NULL
);
1371 if((m
->layout
->arrange
== floating
) || c
->isfloating
)
1372 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1379 restack(Monitor
*m
) {
1387 if(sel
->isfloating
|| (m
->layout
->arrange
== floating
))
1388 XRaiseWindow(dpy
, sel
->win
);
1389 if(m
->layout
->arrange
!= floating
) {
1390 wc
.stack_mode
= Below
;
1391 wc
.sibling
= m
->barwin
;
1392 if(!sel
->isfloating
) {
1393 XConfigureWindow(dpy
, sel
->win
, CWSibling
| CWStackMode
, &wc
);
1394 wc
.sibling
= sel
->win
;
1396 for(c
= nexttiled(clients
, m
); c
; c
= nexttiled(c
->next
, m
)) {
1399 XConfigureWindow(dpy
, c
->win
, CWSibling
| CWStackMode
, &wc
);
1400 wc
.sibling
= c
->win
;
1404 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1410 char buf
[sizeof stext
];
1413 unsigned int len
, offset
;
1416 /* main event loop, also reads status text from stdin */
1418 xfd
= ConnectionNumber(dpy
);
1421 len
= sizeof stext
- 1;
1422 buf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1426 FD_SET(STDIN_FILENO
, &rd
);
1428 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1431 eprint("select failed\n");
1433 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1434 switch((r
= read(STDIN_FILENO
, buf
+ offset
, len
- offset
))) {
1436 strncpy(stext
, strerror(errno
), len
);
1440 strncpy(stext
, "EOF", 4);
1444 for(p
= buf
+ offset
; r
> 0; p
++, r
--, offset
++)
1445 if(*p
== '\n' || *p
== '\0') {
1447 strncpy(stext
, buf
, len
);
1448 p
+= r
- 1; /* p is buf + offset + r - 1 */
1449 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1452 memmove(buf
, p
- r
+ 1, r
);
1457 drawbar(selmonitor
);
1459 while(XPending(dpy
)) {
1460 XNextEvent(dpy
, &ev
);
1461 if(handler
[ev
.type
])
1462 (handler
[ev
.type
])(&ev
); /* call handler */
1469 unsigned int i
, num
;
1470 Window
*wins
, d1
, d2
;
1471 XWindowAttributes wa
;
1474 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1475 for(i
= 0; i
< num
; i
++) {
1476 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1477 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1479 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1480 manage(wins
[i
], &wa
);
1482 for(i
= 0; i
< num
; i
++) { /* now the transients */
1483 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1485 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1486 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1487 manage(wins
[i
], &wa
);
1495 setclientstate(Client
*c
, long state
) {
1496 long data
[] = {state
, None
};
1498 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1499 PropModeReplace
, (unsigned char *)data
, 2);
1503 setlayout(const char *arg
) {
1505 Monitor
*m
= monitorat();
1509 if(m
->layout
== &layouts
[LENGTH(layouts
)])
1510 m
->layout
= &layouts
[0];
1513 for(i
= 0; i
< LENGTH(layouts
); i
++)
1514 if(!strcmp(arg
, layouts
[i
].symbol
))
1516 if(i
== LENGTH(layouts
))
1518 m
->layout
= &layouts
[i
];
1527 setmwfact(const char *arg
) {
1530 Monitor
*m
= monitorat();
1534 /* arg handling, manipulate mwfact */
1537 else if(sscanf(arg
, "%lf", &delta
) == 1) {
1538 if(arg
[0] == '+' || arg
[0] == '-')
1544 else if(m
->mwfact
> 0.9)
1554 XSetWindowAttributes wa
;
1555 XineramaScreenInfo
*info
= NULL
;
1558 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1559 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1560 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1561 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1562 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1563 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1566 wa
.cursor
= cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1567 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1568 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1570 // init screens/monitors first
1571 if((isxinerama
= XineramaIsActive(dpy
)))
1572 info
= XineramaQueryScreens(dpy
, &mcount
);
1573 selmonitor
= monitors
= emallocz(mcount
* sizeof(Monitor
));
1575 screen
= DefaultScreen(dpy
);
1576 root
= RootWindow(dpy
, screen
);
1578 /* init appearance */
1579 dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
);
1580 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
1581 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
1582 dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
);
1583 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
1584 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
1586 dc
.h
= bh
= dc
.font
.height
+ 2;
1587 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1588 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1589 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1591 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1593 for(blw
= i
= 0; i
< LENGTH(layouts
); i
++) {
1594 i
= textw(layouts
[i
].symbol
);
1598 for(i
= 0; i
< mcount
; i
++) {
1603 if (mcount
!= 1 && isxinerama
) {
1604 m
->sx
= info
[i
].x_org
;
1605 m
->sy
= info
[i
].y_org
;
1606 m
->sw
= info
[i
].width
;
1607 m
->sh
= info
[i
].height
;
1608 fprintf(stderr
, "monitor[%d]: %d,%d,%d,%d\n", i
, m
->sx
, m
->sy
, m
->sw
, m
->sh
);
1613 m
->sw
= DisplayWidth(dpy
, screen
);
1614 m
->sh
= DisplayHeight(dpy
, screen
);
1617 m
->seltags
= emallocz(sizeof initags
);
1618 m
->prevtags
= emallocz(sizeof initags
);
1620 memcpy(m
->seltags
, initags
, sizeof initags
);
1621 memcpy(m
->prevtags
, initags
, sizeof initags
);
1625 m
->layout
= &layouts
[0];
1627 // TODO: bpos per screen?
1629 wa
.override_redirect
= 1;
1630 wa
.background_pixmap
= ParentRelative
;
1631 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
1634 m
->barwin
= XCreateWindow(dpy
, root
, m
->sx
, m
->sy
, m
->sw
, bh
, 0,
1635 DefaultDepth(dpy
, screen
), CopyFromParent
, DefaultVisual(dpy
, screen
),
1636 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
1637 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1639 XMapRaised(dpy
, m
->barwin
);
1640 strcpy(stext
, "dwm-"VERSION
);
1642 /* EWMH support per monitor */
1643 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1644 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1646 /* select for events */
1647 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
1648 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
1649 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
1650 XSelectInput(dpy
, root
, wa
.event_mask
);
1663 selmonitor
= monitorat();
1664 fprintf(stderr
, "selmonitor == %d\n", selmonitor
- monitors
);
1668 spawn(const char *arg
) {
1669 static char *shell
= NULL
;
1671 if(!shell
&& !(shell
= getenv("SHELL")))
1675 /* The double-fork construct avoids zombie processes and keeps the code
1676 * clean from stupid signal handlers. */
1680 close(ConnectionNumber(dpy
));
1682 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1683 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1692 tag(const char *arg
) {
1697 for(i
= 0; i
< LENGTH(tags
); i
++)
1698 sel
->tags
[i
] = (NULL
== arg
);
1699 sel
->tags
[idxoftag(arg
)] = True
;
1704 textnw(const char *text
, unsigned int len
) {
1708 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1711 return XTextWidth(dc
.font
.xfont
, text
, len
);
1715 textw(const char *text
) {
1716 return textnw(text
, strlen(text
)) + dc
.font
.height
;
1721 unsigned int i
, n
, nx
, ny
, nw
, nh
, mw
, th
;
1724 domwfact
= dozoom
= True
;
1726 nx
= ny
= nw
= 0; /* gcc stupidity requires this */
1728 for(n
= 0, c
= nexttiled(clients
, m
); c
; c
= nexttiled(c
->next
, m
))
1732 mw
= (n
== 1) ? m
->waw
: m
->mwfact
* m
->waw
;
1733 th
= (n
> 1) ? m
->wah
/ (n
- 1) : 0;
1734 if(n
> 1 && th
< bh
)
1737 for(i
= 0, c
= mc
= nexttiled(clients
, m
); c
; c
= nexttiled(c
->next
, m
)) {
1738 if(i
== 0) { /* master */
1741 nw
= mw
- 2 * c
->border
;
1742 nh
= m
->wah
- 2 * c
->border
;
1744 else { /* tile window */
1747 nx
+= mc
->w
+ 2 * mc
->border
;
1748 nw
= m
->waw
- mw
- 2 * c
->border
;
1750 if(i
+ 1 == n
) /* remainder */
1751 nh
= (m
->way
+ m
->wah
) - ny
- 2 * c
->border
;
1753 nh
= th
- 2 * c
->border
;
1755 resize(c
, nx
, ny
, nw
, nh
, RESIZEHINTS
);
1756 if((RESIZEHINTS
) && ((c
->h
< bh
) || (c
->h
> nh
) || (c
->w
< bh
) || (c
->w
> nw
)))
1757 /* client doesn't accept size constraints */
1758 resize(c
, nx
, ny
, nw
, nh
, False
);
1759 if(n
> 1 && th
!= m
->wah
)
1760 ny
= c
->y
+ c
->h
+ 2 * c
->border
;
1766 togglebar(const char *arg
) {
1768 bpos
= (BARPOS
== BarOff
) ? BarTop
: BARPOS
;
1771 updatebarpos(monitorat());
1776 togglefloating(const char *arg
) {
1779 sel
->isfloating
= !sel
->isfloating
;
1781 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1786 toggletag(const char *arg
) {
1792 sel
->tags
[i
] = !sel
->tags
[i
];
1793 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1794 if(j
== LENGTH(tags
))
1795 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1800 toggleview(const char *arg
) {
1803 Monitor
*m
= monitorat();
1806 m
->seltags
[i
] = !m
->seltags
[i
];
1807 for(j
= 0; j
< LENGTH(tags
) && !m
->seltags
[j
]; j
++);
1808 if(j
== LENGTH(tags
))
1809 m
->seltags
[i
] = True
; /* at least one tag must be viewed */
1817 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1818 c
->isbanned
= False
;
1822 unmanage(Client
*c
) {
1825 wc
.border_width
= c
->oldborder
;
1826 /* The server grab construct avoids race conditions. */
1828 XSetErrorHandler(xerrordummy
);
1829 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1834 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1835 setclientstate(c
, WithdrawnState
);
1839 XSetErrorHandler(xerror
);
1845 unmapnotify(XEvent
*e
) {
1847 XUnmapEvent
*ev
= &e
->xunmap
;
1849 if((c
= getclient(ev
->window
)))
1854 updatebarpos(Monitor
*m
) {
1865 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
);
1869 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
+ m
->wah
);
1872 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
- bh
);
1876 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1880 updatesizehints(Client
*c
) {
1884 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1886 c
->flags
= size
.flags
;
1887 if(c
->flags
& PBaseSize
) {
1888 c
->basew
= size
.base_width
;
1889 c
->baseh
= size
.base_height
;
1891 else if(c
->flags
& PMinSize
) {
1892 c
->basew
= size
.min_width
;
1893 c
->baseh
= size
.min_height
;
1896 c
->basew
= c
->baseh
= 0;
1897 if(c
->flags
& PResizeInc
) {
1898 c
->incw
= size
.width_inc
;
1899 c
->inch
= size
.height_inc
;
1902 c
->incw
= c
->inch
= 0;
1903 if(c
->flags
& PMaxSize
) {
1904 c
->maxw
= size
.max_width
;
1905 c
->maxh
= size
.max_height
;
1908 c
->maxw
= c
->maxh
= 0;
1909 if(c
->flags
& PMinSize
) {
1910 c
->minw
= size
.min_width
;
1911 c
->minh
= size
.min_height
;
1913 else if(c
->flags
& PBaseSize
) {
1914 c
->minw
= size
.base_width
;
1915 c
->minh
= size
.base_height
;
1918 c
->minw
= c
->minh
= 0;
1919 if(c
->flags
& PAspect
) {
1920 c
->minax
= size
.min_aspect
.x
;
1921 c
->maxax
= size
.max_aspect
.x
;
1922 c
->minay
= size
.min_aspect
.y
;
1923 c
->maxay
= size
.max_aspect
.y
;
1926 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1927 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1928 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1932 updatetitle(Client
*c
) {
1933 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1934 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1938 updatewmhints(Client
*c
) {
1941 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1942 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1947 /* There's no way to check accesses to destroyed windows, thus those cases are
1948 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1949 * default error handler, which may call exit. */
1951 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1952 if(ee
->error_code
== BadWindow
1953 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1954 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1955 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1956 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1957 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1958 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1959 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1961 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1962 ee
->request_code
, ee
->error_code
);
1963 return xerrorxlib(dpy
, ee
); /* may call exit */
1967 xerrordummy(Display
*dsply
, XErrorEvent
*ee
) {
1971 /* Startup Error handler to check if another window manager
1972 * is already running. */
1974 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
1980 view(const char *arg
) {
1982 Bool tmp
[LENGTH(tags
)];
1983 Monitor
*m
= monitorat();
1985 for(i
= 0; i
< LENGTH(tags
); i
++)
1986 tmp
[i
] = (NULL
== arg
);
1987 tmp
[idxoftag(arg
)] = True
;
1988 if(memcmp(m
->seltags
, tmp
, sizeof initags
) != 0) {
1989 memcpy(m
->prevtags
, m
->seltags
, sizeof initags
);
1990 memcpy(m
->seltags
, tmp
, sizeof initags
);
1996 viewprevtag(const char *arg
) {
1997 static Bool tmp
[LENGTH(tags
)];
1999 Monitor
*m
= monitorat();
2001 memcpy(tmp
, m
->seltags
, sizeof initags
);
2002 memcpy(m
->seltags
, m
->prevtags
, sizeof initags
);
2003 memcpy(m
->prevtags
, tmp
, sizeof initags
);
2008 zoom(const char *arg
) {
2011 if(!sel
|| !dozoom
|| sel
->isfloating
)
2013 if(c
== nexttiled(clients
, c
->monitor
))
2014 if(!(c
= nexttiled(c
->next
, c
->monitor
)))
2023 movetomonitor(const char *arg
) {
2031 for(i
= 0; &monitors
[i
] != sel
->monitor
&& i
< mcount
; i
++);
2034 sel
->monitor
= &monitors
[i
% mcount
];
2036 memcpy(sel
->tags
, sel
->monitor
->seltags
, sizeof initags
);
2037 resize(sel
, sel
->monitor
->wax
, sel
->monitor
->way
, sel
->w
, sel
->h
, True
);
2042 selectmonitor(const char *arg
) {
2049 for(i
= 0; &monitors
[i
] != sel
->monitor
&& i
< mcount
; i
++);
2052 m
= &monitors
[i
% mcount
];
2053 XWarpPointer(dpy
, None
, root
, 0, 0, 0, 0, m
->wax
+m
->waw
/2, m
->way
+m
->wah
/2);
2059 main(int argc
, char *argv
[]) {
2060 if(argc
== 2 && !strcmp("-v", argv
[1]))
2061 eprint("dwm-"VERSION
", © 2006-2008 Anselm R. Garbe, Sander van Dijk, "
2062 "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
2064 eprint("usage: dwm [-v]\n");
2066 setlocale(LC_CTYPE
, "");
2067 if(!(dpy
= XOpenDisplay(0)))
2068 eprint("dwm: cannot open display\n");