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 View View
;
64 typedef struct Client Client
;
68 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
69 int minax
, maxax
, minay
, maxay
;
72 unsigned int border
, oldborder
;
73 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
)(View
*);
120 int x
, y
, w
, h
, wax
, way
, wah
, waw
;
126 /* function declarations */
127 void applyrules(Client
*c
);
129 void attach(Client
*c
);
130 void attachstack(Client
*c
);
132 void buttonpress(XEvent
*e
);
133 void checkotherwm(void);
135 void compileregs(void);
136 void configure(Client
*c
);
137 void configurenotify(XEvent
*e
);
138 void configurerequest(XEvent
*e
);
139 void destroynotify(XEvent
*e
);
140 void detach(Client
*c
);
141 void detachstack(Client
*c
);
142 void drawbar(View
*v
);
143 void drawsquare(View
*v
, Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
144 void drawtext(View
*v
, const char *text
, unsigned long col
[ColLast
], Bool invert
);
145 void *emallocz(unsigned int size
);
146 void enternotify(XEvent
*e
);
147 void eprint(const char *errstr
, ...);
148 void expose(XEvent
*e
);
149 void floating(View
*v
); /* default floating layout */
150 void focus(Client
*c
);
151 void focusin(XEvent
*e
);
152 void focusnext(const char *arg
);
153 void focusprev(const char *arg
);
154 Client
*getclient(Window w
);
155 unsigned long getcolor(const char *colstr
);
156 View
*getviewbar(Window barwin
);
157 View
*getview(Client
*c
);
158 long getstate(Window w
);
159 Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
160 void grabbuttons(Client
*c
, Bool focused
);
162 unsigned int idxoftag(const char *tag
);
163 void initfont(const char *fontstr
);
164 Bool
isoccupied(unsigned int t
);
165 Bool
isprotodel(Client
*c
);
166 Bool
isurgent(unsigned int t
);
167 Bool
isvisible(Client
*c
);
168 void keypress(XEvent
*e
);
169 void killclient(const char *arg
);
170 void manage(Window w
, XWindowAttributes
*wa
);
171 void mappingnotify(XEvent
*e
);
172 void maprequest(XEvent
*e
);
174 void movemouse(Client
*c
);
175 Client
*nexttiled(Client
*c
, View
*v
);
176 void propertynotify(XEvent
*e
);
177 void quit(const char *arg
);
178 void reapply(const char *arg
);
179 void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
);
180 void resizemouse(Client
*c
);
181 void restack(View
*v
);
184 void setclientstate(Client
*c
, long state
);
185 void setlayout(const char *arg
);
186 void setmwfact(const char *arg
);
188 void spawn(const char *arg
);
189 void tag(const char *arg
);
190 unsigned int textnw(const char *text
, unsigned int len
);
191 unsigned int textw(const char *text
);
193 void togglebar(const char *arg
);
194 void togglefloating(const char *arg
);
195 void toggletag(const char *arg
);
196 void toggleview(const char *arg
);
197 void unban(Client
*c
);
198 void unmanage(Client
*c
);
199 void unmapnotify(XEvent
*e
);
200 void updatebarpos(View
*v
);
201 void updatesizehints(Client
*c
);
202 void updatetitle(Client
*c
);
203 void updatewmhints(Client
*c
);
204 void view(const char *arg
);
205 void viewprevtag(const char *arg
); /* views previous selected tags */
206 int xerror(Display
*dpy
, XErrorEvent
*ee
);
207 int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
208 int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
209 void zoom(const char *arg
);
210 void selectview(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 /* function implementations */
258 applyrules(Client
*c
) {
259 static char buf
[512];
262 Bool matched_tag
= False
;
263 XClassHint ch
= { 0 };
266 XGetClassHint(dpy
, c
->win
, &ch
);
267 snprintf(buf
, sizeof buf
, "%s:%s:%s",
268 ch
.res_class
? ch
.res_class
: "",
269 ch
.res_name
? ch
.res_name
: "", c
->name
);
270 for(i
= 0; i
< LENGTH(rules
); i
++)
271 if(regs
[i
].propregex
&& !regexec(regs
[i
].propregex
, buf
, 1, &tmp
, 0)) {
272 c
->isfloating
= rules
[i
].isfloating
;
273 for(j
= 0; regs
[i
].tagregex
&& j
< LENGTH(tags
); j
++) {
274 if(!regexec(regs
[i
].tagregex
, tags
[j
], 1, &tmp
, 0)) {
276 c
->tags
[j
] = selview
->id
;
285 memcpy(c
->tags
, seltags
, sizeof initags
);
295 for(c
= clients
; c
; c
= c
->next
)
301 for(i
= 0; i
< nviews
; i
++) {
302 views
[i
].layout
->arrange(&views
[i
]);
317 attachstack(Client
*c
) {
326 XMoveWindow(dpy
, c
->win
, c
->x
+ 3 * getview(c
)->w
, c
->y
);
331 buttonpress(XEvent
*e
) {
334 XButtonPressedEvent
*ev
= &e
->xbutton
;
338 if(ev
->window
== v
->barwin
) {
340 for(i
= 0; i
< LENGTH(tags
); i
++) {
343 if(ev
->button
== Button1
) {
344 if(ev
->state
& MODKEY
)
349 else if(ev
->button
== Button3
) {
350 if(ev
->state
& MODKEY
)
358 if((ev
->x
< x
+ blw
) && ev
->button
== Button1
)
361 else if((c
= getclient(ev
->window
))) {
363 if(CLEANMASK(ev
->state
) != MODKEY
)
365 if(ev
->button
== Button1
) {
369 else if(ev
->button
== Button2
) {
370 if((floating
!= v
->layout
->arrange
) && c
->isfloating
)
371 togglefloating(NULL
);
375 else if(ev
->button
== Button3
&& !c
->isfixed
) {
385 XSetErrorHandler(xerrorstart
);
387 /* this causes an error if some other window manager is running */
388 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
391 eprint("dwm: another window manager is already running\n");
393 XSetErrorHandler(NULL
);
394 xerrorxlib
= XSetErrorHandler(xerror
);
407 XFreeFontSet(dpy
, dc
.font
.set
);
409 XFreeFont(dpy
, dc
.font
.xfont
);
411 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
412 XFreePixmap(dpy
, dc
.drawable
);
414 XFreeCursor(dpy
, cursor
[CurNormal
]);
415 XFreeCursor(dpy
, cursor
[CurResize
]);
416 XFreeCursor(dpy
, cursor
[CurMove
]);
417 for(i
= 0; i
< nviews
; i
++)
418 XDestroyWindow(dpy
, views
[i
].barwin
);
420 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
430 regs
= emallocz(LENGTH(rules
) * sizeof(Regs
));
431 for(i
= 0; i
< LENGTH(rules
); i
++) {
433 reg
= emallocz(sizeof(regex_t
));
434 if(regcomp(reg
, rules
[i
].prop
, REG_EXTENDED
))
437 regs
[i
].propregex
= reg
;
440 reg
= emallocz(sizeof(regex_t
));
441 if(regcomp(reg
, rules
[i
].tags
, REG_EXTENDED
))
444 regs
[i
].tagregex
= reg
;
450 configure(Client
*c
) {
453 ce
.type
= ConfigureNotify
;
461 ce
.border_width
= c
->border
;
463 ce
.override_redirect
= False
;
464 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
468 configurenotify(XEvent
*e
) {
469 XConfigureEvent
*ev
= &e
->xconfigure
;
472 if(ev
->window
== root
&& (ev
->width
!= v
->w
|| ev
->height
!= v
->h
)) {
473 /* TODO -- update Xinerama dimensions here */
476 XFreePixmap(dpy
, dc
.drawable
);
477 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(root
, screen
), bh
, DefaultDepth(dpy
, screen
));
478 XResizeWindow(dpy
, v
->barwin
, v
->w
, bh
);
485 configurerequest(XEvent
*e
) {
487 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
490 if((c
= getclient(ev
->window
))) {
491 View
*v
= getview(c
);
492 if(ev
->value_mask
& CWBorderWidth
)
493 c
->border
= ev
->border_width
;
494 if(c
->isfixed
|| c
->isfloating
|| (floating
== v
->layout
->arrange
)) {
495 if(ev
->value_mask
& CWX
)
497 if(ev
->value_mask
& CWY
)
499 if(ev
->value_mask
& CWWidth
)
501 if(ev
->value_mask
& CWHeight
)
503 if((c
->x
- v
->x
+ c
->w
) > v
->w
&& c
->isfloating
)
504 c
->x
= v
->x
+ (v
->w
/ 2 - c
->w
/ 2); /* center in x direction */
505 if((c
->y
- v
->y
+ c
->h
) > v
->h
&& c
->isfloating
)
506 c
->y
= v
->y
+ (v
->h
/ 2 - c
->h
/ 2); /* center in y direction */
507 if((ev
->value_mask
& (CWX
|CWY
))
508 && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
511 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
519 wc
.width
= ev
->width
;
520 wc
.height
= ev
->height
;
521 wc
.border_width
= ev
->border_width
;
522 wc
.sibling
= ev
->above
;
523 wc
.stack_mode
= ev
->detail
;
524 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
530 destroynotify(XEvent
*e
) {
532 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
534 if((c
= getclient(ev
->window
)))
541 c
->prev
->next
= c
->next
;
543 c
->next
->prev
= c
->prev
;
546 c
->next
= c
->prev
= NULL
;
550 detachstack(Client
*c
) {
553 for(tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
563 for(c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
564 for(i
= 0; i
< LENGTH(tags
); i
++) {
565 dc
.w
= textw(tags
[i
]);
567 drawtext(v
, tags
[i
], dc
.sel
, isurgent(i
));
568 drawsquare(v
, c
&& c
->tags
[i
], isoccupied(i
), isurgent(i
), dc
.sel
);
571 drawtext(v
, tags
[i
], dc
.norm
, isurgent(i
));
572 drawsquare(v
, c
&& c
->tags
[i
], isoccupied(i
), isurgent(i
), dc
.norm
);
577 drawtext(v
, v
->layout
->symbol
, dc
.norm
, False
);
586 drawtext(v
, stext
, dc
.norm
, False
);
590 if((dc
.w
= dc
.x
- x
) > bh
) {
593 drawtext(v
, c
->name
, dc
.sel
, False
);
594 drawsquare(v
, False
, c
->isfloating
, False
, dc
.sel
);
597 drawtext(v
, NULL
, dc
.norm
, False
);
599 XCopyArea(dpy
, dc
.drawable
, v
->barwin
, dc
.gc
, 0, 0, v
->w
, bh
, 0, 0);
604 drawsquare(View
*v
, Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
607 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
609 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
610 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
611 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
615 r
.width
= r
.height
= x
+ 1;
616 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
619 r
.width
= r
.height
= x
;
620 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
625 drawtext(View
*v
, const char *text
, unsigned long col
[ColLast
], Bool invert
) {
627 static char buf
[256];
628 unsigned int len
, olen
;
629 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
631 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
632 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
636 olen
= len
= strlen(text
);
637 if(len
>= sizeof buf
)
638 len
= sizeof buf
- 1;
639 memcpy(buf
, text
, len
);
641 h
= dc
.font
.ascent
+ dc
.font
.descent
;
642 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
644 /* shorten text if necessary */
645 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
656 return; /* too long */
657 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
659 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
661 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
665 emallocz(unsigned int size
) {
666 void *res
= calloc(1, size
);
669 eprint("fatal: could not malloc() %u bytes\n", size
);
674 enternotify(XEvent
*e
) {
676 XCrossingEvent
*ev
= &e
->xcrossing
;
678 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) {
679 if(!isxinerama
|| ev
->window
!= root
)
682 if((c
= getclient(ev
->window
)))
689 eprint(const char *errstr
, ...) {
692 va_start(ap
, errstr
);
693 vfprintf(stderr
, errstr
, ap
);
701 XExposeEvent
*ev
= &e
->xexpose
;
703 if(ev
->count
== 0 && (v
= getviewbar(ev
->window
)))
708 floating(View
*v
) { /* default floating layout */
711 domwfact
= dozoom
= False
;
712 for(c
= clients
; c
; c
= c
->next
)
714 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
721 selview
= getview(c
);
726 if(!c
|| (c
&& !isvisible(c
)))
727 for(c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
728 if(sel
&& sel
!= c
) {
729 grabbuttons(sel
, False
);
730 XSetWindowBorder(dpy
, sel
->win
, dc
.norm
[ColBorder
]);
735 grabbuttons(c
, True
);
739 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
740 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
741 selview
= getview(c
);
744 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
749 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
750 XFocusChangeEvent
*ev
= &e
->xfocus
;
752 if(sel
&& ev
->window
!= sel
->win
)
753 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
757 focusnext(const char *arg
) {
762 for(c
= sel
->next
; c
&& !isvisible(c
); c
= c
->next
);
764 for(c
= clients
; c
&& !isvisible(c
); c
= c
->next
);
772 focusprev(const char *arg
) {
777 for(c
= sel
->prev
; c
&& !isvisible(c
); c
= c
->prev
);
779 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
780 for(; c
&& !isvisible(c
); c
= c
->prev
);
789 getclient(Window w
) {
792 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
797 getcolor(const char *colstr
) {
798 Colormap cmap
= DefaultColormap(dpy
, screen
);
801 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
802 eprint("error, cannot allocate color '%s'\n", colstr
);
807 getviewbar(Window barwin
) {
810 for(i
= 0; i
< nviews
; i
++)
811 if(views
[i
].barwin
== barwin
)
820 for(i
= 0; i
< LENGTH(tags
); i
++)
822 return &views
[c
->tags
[i
] - 1];
823 return &views
[0]; /* fallback */
830 unsigned char *p
= NULL
;
831 unsigned long n
, extra
;
834 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
835 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
836 if(status
!= Success
)
845 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
850 if(!text
|| size
== 0)
853 XGetTextProperty(dpy
, w
, &name
, atom
);
856 if(name
.encoding
== XA_STRING
)
857 strncpy(text
, (char *)name
.value
, size
- 1);
859 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
861 strncpy(text
, *list
, size
- 1);
862 XFreeStringList(list
);
865 text
[size
- 1] = '\0';
871 grabbuttons(Client
*c
, Bool focused
) {
872 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
875 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
876 GrabModeAsync
, GrabModeSync
, None
, None
);
877 XGrabButton(dpy
, Button1
, MODKEY
|LockMask
, c
->win
, False
, BUTTONMASK
,
878 GrabModeAsync
, GrabModeSync
, None
, None
);
879 XGrabButton(dpy
, Button1
, MODKEY
|numlockmask
, c
->win
, False
, BUTTONMASK
,
880 GrabModeAsync
, GrabModeSync
, None
, None
);
881 XGrabButton(dpy
, Button1
, MODKEY
|numlockmask
|LockMask
, c
->win
, False
, BUTTONMASK
,
882 GrabModeAsync
, GrabModeSync
, None
, None
);
884 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
885 GrabModeAsync
, GrabModeSync
, None
, None
);
886 XGrabButton(dpy
, Button2
, MODKEY
|LockMask
, c
->win
, False
, BUTTONMASK
,
887 GrabModeAsync
, GrabModeSync
, None
, None
);
888 XGrabButton(dpy
, Button2
, MODKEY
|numlockmask
, c
->win
, False
, BUTTONMASK
,
889 GrabModeAsync
, GrabModeSync
, None
, None
);
890 XGrabButton(dpy
, Button2
, MODKEY
|numlockmask
|LockMask
, c
->win
, False
, BUTTONMASK
,
891 GrabModeAsync
, GrabModeSync
, None
, None
);
893 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
894 GrabModeAsync
, GrabModeSync
, None
, None
);
895 XGrabButton(dpy
, Button3
, MODKEY
|LockMask
, c
->win
, False
, BUTTONMASK
,
896 GrabModeAsync
, GrabModeSync
, None
, None
);
897 XGrabButton(dpy
, Button3
, MODKEY
|numlockmask
, c
->win
, False
, BUTTONMASK
,
898 GrabModeAsync
, GrabModeSync
, None
, None
);
899 XGrabButton(dpy
, Button3
, MODKEY
|numlockmask
|LockMask
, c
->win
, False
, BUTTONMASK
,
900 GrabModeAsync
, GrabModeSync
, None
, None
);
903 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
904 GrabModeAsync
, GrabModeSync
, None
, None
);
911 XModifierKeymap
*modmap
;
913 /* init modifier map */
914 modmap
= XGetModifierMapping(dpy
);
915 for(i
= 0; i
< 8; i
++)
916 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
917 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
918 numlockmask
= (1 << i
);
920 XFreeModifiermap(modmap
);
922 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
923 for(i
= 0; i
< LENGTH(keys
); i
++) {
924 code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
);
925 XGrabKey(dpy
, code
, keys
[i
].mod
, root
, True
,
926 GrabModeAsync
, GrabModeAsync
);
927 XGrabKey(dpy
, code
, keys
[i
].mod
|LockMask
, root
, True
,
928 GrabModeAsync
, GrabModeAsync
);
929 XGrabKey(dpy
, code
, keys
[i
].mod
|numlockmask
, root
, True
,
930 GrabModeAsync
, GrabModeAsync
);
931 XGrabKey(dpy
, code
, keys
[i
].mod
|numlockmask
|LockMask
, root
, True
,
932 GrabModeAsync
, GrabModeAsync
);
937 idxoftag(const char *tag
) {
940 for(i
= 0; (i
< LENGTH(tags
)) && (tags
[i
] != tag
); i
++);
941 return (i
< LENGTH(tags
)) ? i
: 0;
945 initfont(const char *fontstr
) {
946 char *def
, **missing
;
951 XFreeFontSet(dpy
, dc
.font
.set
);
952 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
955 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
956 XFreeStringList(missing
);
959 XFontSetExtents
*font_extents
;
960 XFontStruct
**xfonts
;
962 dc
.font
.ascent
= dc
.font
.descent
= 0;
963 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
964 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
965 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
966 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
967 dc
.font
.ascent
= (*xfonts
)->ascent
;
968 if(dc
.font
.descent
< (*xfonts
)->descent
)
969 dc
.font
.descent
= (*xfonts
)->descent
;
975 XFreeFont(dpy
, dc
.font
.xfont
);
976 dc
.font
.xfont
= NULL
;
977 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
978 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
979 eprint("error, cannot load font: '%s'\n", fontstr
);
980 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
981 dc
.font
.descent
= dc
.font
.xfont
->descent
;
983 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
987 isoccupied(unsigned int t
) {
990 for(c
= clients
; c
; c
= c
->next
)
997 isprotodel(Client
*c
) {
1002 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1003 for(i
= 0; !ret
&& i
< n
; i
++)
1004 if(protocols
[i
] == wmatom
[WMDelete
])
1012 isurgent(unsigned int t
) {
1015 for(c
= clients
; c
; c
= c
->next
)
1016 if(c
->isurgent
&& c
->tags
[t
])
1022 isvisible(Client
*c
) {
1025 for(i
= 0; i
< LENGTH(tags
); i
++)
1026 if(c
->tags
[i
] && 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
+ v
->x
;
1084 c
->y
= wa
->y
+ v
->y
;
1087 c
->oldborder
= wa
->border_width
;
1089 if(c
->w
== v
->w
&& c
->h
== v
->h
) {
1092 c
->border
= wa
->border_width
;
1095 if(c
->x
+ c
->w
+ 2 * c
->border
> v
->wax
+ v
->waw
)
1096 c
->x
= v
->wax
+ v
->waw
- c
->w
- 2 * c
->border
;
1097 if(c
->y
+ c
->h
+ 2 * c
->border
> v
->way
+ v
->wah
)
1098 c
->y
= v
->way
+ v
->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
< nviews
; i
++) {
1158 if((x
>= views
[i
].x
&& x
< views
[i
].x
+ views
[i
].w
)
1159 && (y
>= views
[i
].y
&& y
< views
[i
].y
+ views
[i
].h
)) {
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(v
->wax
- nx
) < SNAP
)
1198 else if(abs((v
->wax
+ v
->waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
1199 nx
= v
->wax
+ v
->waw
- c
->w
- 2 * c
->border
;
1200 if(abs(v
->way
- ny
) < SNAP
)
1202 else if(abs((v
->way
+ v
->wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
1203 ny
= v
->way
+ v
->wah
- c
->h
- 2 * c
->border
;
1204 if(!c
->isfloating
&& (v
->layout
->arrange
!= floating
) && (abs(nx
- c
->x
) > SNAP
|| abs(ny
- c
->y
) > SNAP
))
1205 togglefloating(NULL
);
1206 if((v
->layout
->arrange
== floating
) || c
->isfloating
)
1207 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1214 nexttiled(Client
*c
, View
*v
) {
1215 for(; c
&& (c
->isfloating
|| getview(c
) != v
|| !isvisible(c
)); 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(getview(c
));
1243 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
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
) {
1275 /* set minimum possible */
1281 /* temporarily remove base dimensions */
1285 /* adjust for aspect limits */
1286 if (c
->minay
> 0 && c
->maxay
> 0 && c
->minax
> 0 && c
->maxax
> 0) {
1287 if (w
* c
->maxay
> h
* c
->maxax
)
1288 w
= h
* c
->maxax
/ c
->maxay
;
1289 else if (w
* c
->minay
< h
* c
->minax
)
1290 h
= w
* c
->minay
/ c
->minax
;
1293 /* adjust for increment value */
1299 /* restore base dimensions */
1303 if(c
->minw
> 0 && w
< c
->minw
)
1305 if(c
->minh
> 0 && h
< c
->minh
)
1307 if(c
->maxw
> 0 && w
> c
->maxw
)
1309 if(c
->maxh
> 0 && h
> c
->maxh
)
1312 if(w
<= 0 || h
<= 0)
1315 x
= v
->w
- w
- 2 * c
->border
;
1317 y
= v
->h
- h
- 2 * c
->border
;
1318 if(x
+ w
+ 2 * c
->border
< v
->x
)
1320 if(y
+ h
+ 2 * c
->border
< v
->y
)
1322 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1325 c
->w
= wc
.width
= w
;
1326 c
->h
= wc
.height
= h
;
1327 wc
.border_width
= c
->border
;
1328 XConfigureWindow(dpy
, c
->win
,
1329 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(!c
->isfloating
&& (v
->layout
->arrange
!= floating
) && (abs(nw
- c
->w
) > SNAP
|| abs(nh
- c
->h
) > SNAP
))
1370 togglefloating(NULL
);
1371 if((v
->layout
->arrange
== floating
) || c
->isfloating
)
1372 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1387 if(sel
->isfloating
|| (v
->layout
->arrange
== floating
))
1388 XRaiseWindow(dpy
, sel
->win
);
1389 if(v
->layout
->arrange
!= floating
) {
1390 wc
.stack_mode
= Below
;
1391 wc
.sibling
= v
->barwin
;
1392 if(!sel
->isfloating
) {
1393 XConfigureWindow(dpy
, sel
->win
, CWSibling
|CWStackMode
, &wc
);
1394 wc
.sibling
= sel
->win
;
1396 for(c
= nexttiled(clients
, v
); c
; c
= nexttiled(c
->next
, v
)) {
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
);
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
) {
1509 if(v
->layout
== &layouts
[LENGTH(layouts
)])
1510 v
->layout
= &layouts
[0];
1513 for(i
= 0; i
< LENGTH(layouts
); i
++)
1514 if(!strcmp(arg
, layouts
[i
].symbol
))
1516 if(i
== LENGTH(layouts
))
1518 v
->layout
= &layouts
[i
];
1527 setmwfact(const char *arg
) {
1534 /* arg handling, manipulate mwfact */
1537 else if(sscanf(arg
, "%lf", &delta
) == 1) {
1538 if(arg
[0] == '+' || arg
[0] == '-')
1544 else if(v
->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 if((isxinerama
= XineramaIsActive(dpy
)))
1571 info
= XineramaQueryScreens(dpy
, &nviews
);
1572 #if defined(AIM_XINERAMA)
1574 nviews
= 2; /* aim Xinerama */
1576 selview
= views
= emallocz(nviews
* sizeof(View
));
1578 screen
= DefaultScreen(dpy
);
1579 root
= RootWindow(dpy
, screen
);
1581 /* init appearance */
1582 dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
);
1583 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
1584 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
1585 dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
);
1586 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
1587 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
1589 dc
.h
= bh
= dc
.font
.height
+ 2;
1590 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1591 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1592 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1594 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1596 for(blw
= i
= 0; i
< LENGTH(layouts
); i
++) {
1597 i
= textw(layouts
[i
].symbol
);
1602 seltags
= emallocz(sizeof initags
);
1603 prevtags
= emallocz(sizeof initags
);
1604 memcpy(seltags
, initags
, sizeof initags
);
1605 memcpy(prevtags
, initags
, sizeof initags
);
1607 for(i
= 0; i
< nviews
; i
++) {
1612 if(nviews
!= 1 && isxinerama
) {
1614 #if defined(AIM_XINERAMA)
1615 v
->w
= DisplayWidth(dpy
, screen
) / 2;
1616 v
->x
= (i
== 0) ? 0 : v
->w
;
1618 v
->h
= DisplayHeight(dpy
, screen
);
1620 v
->x
= info
[i
].x_org
;
1621 v
->y
= info
[i
].y_org
;
1622 v
->w
= info
[i
].width
;
1623 v
->h
= info
[i
].height
;
1629 v
->w
= DisplayWidth(dpy
, screen
);
1630 v
->h
= DisplayHeight(dpy
, screen
);
1635 v
->layout
= &layouts
[0];
1637 // TODO: bpos per screen?
1639 wa
.override_redirect
= 1;
1640 wa
.background_pixmap
= ParentRelative
;
1641 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1644 v
->barwin
= XCreateWindow(dpy
, root
, v
->x
, v
->y
, v
->w
, bh
, 0,
1645 DefaultDepth(dpy
, screen
), CopyFromParent
, DefaultVisual(dpy
, screen
),
1646 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1647 XDefineCursor(dpy
, v
->barwin
, cursor
[CurNormal
]);
1649 XMapRaised(dpy
, v
->barwin
);
1650 strcpy(stext
, "dwm-"VERSION
);
1652 /* EWMH support per view */
1653 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1654 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1656 /* select for events */
1657 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
1658 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
;
1659 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1660 XSelectInput(dpy
, root
, wa
.event_mask
);
1673 selview
= &views
[0];
1677 spawn(const char *arg
) {
1678 static char *shell
= NULL
;
1680 if(!shell
&& !(shell
= getenv("SHELL")))
1684 /* The double-fork construct avoids zombie processes and keeps the code
1685 * clean from stupid signal handlers. */
1689 close(ConnectionNumber(dpy
));
1691 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1692 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1701 tag(const char *arg
) {
1706 for(i
= 0; i
< LENGTH(tags
); i
++)
1707 sel
->tags
[i
] = (NULL
== arg
) ? selview
->id
: 0;
1708 sel
->tags
[idxoftag(arg
)] = selview
->id
;
1713 textnw(const char *text
, unsigned int len
) {
1717 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1720 return XTextWidth(dc
.font
.xfont
, text
, len
);
1724 textw(const char *text
) {
1725 return textnw(text
, strlen(text
)) + dc
.font
.height
;
1730 unsigned int i
, n
, nx
, ny
, nw
, nh
, mw
, th
;
1733 domwfact
= dozoom
= True
;
1737 for(n
= 0, c
= nexttiled(clients
, v
); c
; c
= nexttiled(c
->next
, v
))
1741 mw
= (n
== 1) ? v
->waw
: v
->mwfact
* v
->waw
;
1742 th
= (n
> 1) ? v
->wah
/ (n
- 1) : 0;
1743 if(n
> 1 && th
< bh
)
1746 for(i
= 0, c
= mc
= nexttiled(clients
, v
); c
; c
= nexttiled(c
->next
, v
)) {
1747 if(i
== 0) { /* master */
1750 nw
= mw
- 2 * c
->border
;
1751 nh
= v
->wah
- 2 * c
->border
;
1753 else { /* tile window */
1756 nx
+= mc
->w
+ 2 * mc
->border
;
1757 nw
= v
->waw
- mw
- 2 * c
->border
;
1759 if(i
+ 1 == n
) /* remainder */
1760 nh
= (v
->way
+ v
->wah
) - ny
- 2 * c
->border
;
1762 nh
= th
- 2 * c
->border
;
1764 fprintf(stderr
, "tile %d %d %d %d\n", nx
, ny
, nw
, nh
);
1765 resize(c
, nx
, ny
, nw
, nh
, RESIZEHINTS
);
1766 if((RESIZEHINTS
) && ((c
->h
< bh
) || (c
->h
> nh
) || (c
->w
< bh
) || (c
->w
> nw
)))
1767 /* client doesn't accept size constraints */
1768 resize(c
, nx
, ny
, nw
, nh
, False
);
1769 if(n
> 1 && th
!= v
->wah
)
1770 ny
= c
->y
+ c
->h
+ 2 * c
->border
;
1776 togglebar(const char *arg
) {
1778 bpos
= (BARPOS
== BarOff
) ? BarTop
: BARPOS
;
1781 updatebarpos(selview
);
1786 togglefloating(const char *arg
) {
1789 sel
->isfloating
= !sel
->isfloating
;
1791 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1796 toggletag(const char *arg
) {
1802 sel
->tags
[i
] = !sel
->tags
[i
];
1803 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1804 if(j
== LENGTH(tags
))
1805 sel
->tags
[i
] = selview
->id
; /* at least one tag must be enabled */
1810 toggleview(const char *arg
) {
1814 seltags
[i
] = !seltags
[i
];
1815 for(j
= 0; j
< LENGTH(tags
) && !seltags
[j
]; j
++);
1816 if(j
== LENGTH(tags
))
1817 seltags
[i
] = selview
->id
; /* at least one tag must be viewed */
1825 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1826 c
->isbanned
= False
;
1830 unmanage(Client
*c
) {
1833 wc
.border_width
= c
->oldborder
;
1834 /* The server grab construct avoids race conditions. */
1836 XSetErrorHandler(xerrordummy
);
1837 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1842 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1843 setclientstate(c
, WithdrawnState
);
1847 XSetErrorHandler(xerror
);
1853 unmapnotify(XEvent
*e
) {
1855 XUnmapEvent
*ev
= &e
->xunmap
;
1857 if((c
= getclient(ev
->window
)))
1862 updatebarpos(View
*v
) {
1873 XMoveWindow(dpy
, v
->barwin
, v
->x
, v
->y
);
1877 XMoveWindow(dpy
, v
->barwin
, v
->x
, v
->y
+ v
->wah
);
1880 XMoveWindow(dpy
, v
->barwin
, v
->x
, v
->y
- bh
);
1884 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1888 updatesizehints(Client
*c
) {
1892 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1894 c
->flags
= size
.flags
;
1895 if(c
->flags
& PBaseSize
) {
1896 c
->basew
= size
.base_width
;
1897 c
->baseh
= size
.base_height
;
1899 else if(c
->flags
& PMinSize
) {
1900 c
->basew
= size
.min_width
;
1901 c
->baseh
= size
.min_height
;
1904 c
->basew
= c
->baseh
= 0;
1905 if(c
->flags
& PResizeInc
) {
1906 c
->incw
= size
.width_inc
;
1907 c
->inch
= size
.height_inc
;
1910 c
->incw
= c
->inch
= 0;
1911 if(c
->flags
& PMaxSize
) {
1912 c
->maxw
= size
.max_width
;
1913 c
->maxh
= size
.max_height
;
1916 c
->maxw
= c
->maxh
= 0;
1917 if(c
->flags
& PMinSize
) {
1918 c
->minw
= size
.min_width
;
1919 c
->minh
= size
.min_height
;
1921 else if(c
->flags
& PBaseSize
) {
1922 c
->minw
= size
.base_width
;
1923 c
->minh
= size
.base_height
;
1926 c
->minw
= c
->minh
= 0;
1927 if(c
->flags
& PAspect
) {
1928 c
->minax
= size
.min_aspect
.x
;
1929 c
->maxax
= size
.max_aspect
.x
;
1930 c
->minay
= size
.min_aspect
.y
;
1931 c
->maxay
= size
.max_aspect
.y
;
1934 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1935 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1936 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1940 updatetitle(Client
*c
) {
1941 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1942 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1946 updatewmhints(Client
*c
) {
1949 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1950 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1955 /* There's no way to check accesses to destroyed windows, thus those cases are
1956 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1957 * default error handler, which may call exit. */
1959 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1960 if(ee
->error_code
== BadWindow
1961 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1962 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1963 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1964 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1965 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1966 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1967 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1969 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1970 ee
->request_code
, ee
->error_code
);
1971 return xerrorxlib(dpy
, ee
); /* may call exit */
1975 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1979 /* Startup Error handler to check if another window manager
1980 * is already running. */
1982 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1988 view(const char *arg
) {
1990 int tmp
[LENGTH(tags
)];
1992 for(i
= 0; i
< LENGTH(tags
); i
++)
1993 tmp
[i
] = (NULL
== arg
) ? selview
->id
: 0;
1994 tmp
[idxoftag(arg
)] = selview
->id
;
1995 if(memcmp(seltags
, tmp
, sizeof initags
) != 0) {
1996 memcpy(prevtags
, seltags
, sizeof initags
);
1997 memcpy(seltags
, tmp
, sizeof initags
);
2003 viewprevtag(const char *arg
) {
2004 static Bool tmp
[LENGTH(tags
)];
2006 memcpy(tmp
, seltags
, sizeof initags
);
2007 memcpy(seltags
, prevtags
, sizeof initags
);
2008 memcpy(prevtags
, tmp
, sizeof initags
);
2013 zoom(const char *arg
) {
2016 if(!sel
|| !dozoom
|| sel
->isfloating
)
2018 if(c
== nexttiled(clients
, getview(c
)))
2019 if(!(c
= nexttiled(c
->next
, getview(c
))))
2028 selectview(const char *arg
) {
2036 v
= &views
[i
% nviews
];
2037 XWarpPointer(dpy
, None
, root
, 0, 0, 0, 0, v
->wax
+v
->waw
/2, v
->way
+v
->wah
/2);
2043 main(int argc
, char *argv
[]) {
2044 if(argc
== 2 && !strcmp("-v", argv
[1]))
2045 eprint("dwm-"VERSION
", © 2006-2008 dwm engineers, see LICENSE for details\n");
2047 eprint("usage: dwm [-v]\n");
2049 setlocale(LC_CTYPE
, "");
2050 if(!(dpy
= XOpenDisplay(0)))
2051 eprint("dwm: cannot open display\n");