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. For each client dwm
21 * creates a small title window, which is resized whenever the (_NET_)WM_NAME
22 * properties are updated or the client is moved/resized.
24 * Keys and tagging rules are organized as arrays and defined in config.h.
26 * To understand everything else, start reading main().
35 #include <sys/select.h>
36 #include <sys/types.h>
39 #include <X11/cursorfont.h>
40 #include <X11/keysym.h>
41 #include <X11/Xatom.h>
43 #include <X11/Xproto.h>
44 #include <X11/Xutil.h>
47 #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
48 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
49 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
52 enum { BarTop
, BarBot
, BarOff
}; /* bar position */
53 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
54 enum { ColBorder
, ColFG
, ColBG
, ColLast
}; /* color */
55 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
56 enum { WMProtocols
, WMDelete
, WMName
, WMState
, WMLast
};/* default atoms */
59 typedef struct Client Client
;
63 int rx
, ry
, rw
, rh
; /* revert geometry */
64 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
65 int minax
, maxax
, minay
, maxay
;
67 unsigned int border
, oldborder
;
68 Bool isbanned
, isfixed
, ismax
, isfloating
, wasfloating
;
78 unsigned long norm
[ColLast
];
79 unsigned long sel
[ColLast
];
89 } DC
; /* draw context */
94 void (*func
)(const char *arg
);
100 void (*arrange
)(void);
114 /* forward declarations */
115 void applyrules(Client
*c
);
117 void attach(Client
*c
);
118 void attachstack(Client
*c
);
120 void buttonpress(XEvent
*e
);
121 void checkotherwm(void);
123 void compileregs(void);
124 void configure(Client
*c
);
125 void configurenotify(XEvent
*e
);
126 void configurerequest(XEvent
*e
);
127 void destroynotify(XEvent
*e
);
128 void detach(Client
*c
);
129 void detachstack(Client
*c
);
131 void drawsquare(Bool filled
, Bool empty
, unsigned long col
[ColLast
]);
132 void drawtext(const char *text
, unsigned long col
[ColLast
]);
133 void *emallocz(unsigned int size
);
134 void enternotify(XEvent
*e
);
135 void eprint(const char *errstr
, ...);
136 void expose(XEvent
*e
);
137 void floating(void); /* default floating layout */
138 void focus(Client
*c
);
139 void focusnext(const char *arg
);
140 void focusprev(const char *arg
);
141 Client
*getclient(Window w
);
142 unsigned long getcolor(const char *colstr
);
143 long getstate(Window w
);
144 Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
145 void grabbuttons(Client
*c
, Bool focused
);
146 unsigned int idxoftag(const char *tag
);
147 void initfont(const char *fontstr
);
148 Bool
isarrange(void (*func
)());
149 Bool
isoccupied(unsigned int t
);
150 Bool
isprotodel(Client
*c
);
151 Bool
isvisible(Client
*c
);
152 void keypress(XEvent
*e
);
153 void killclient(const char *arg
);
154 void leavenotify(XEvent
*e
);
155 void manage(Window w
, XWindowAttributes
*wa
);
156 void mappingnotify(XEvent
*e
);
157 void maprequest(XEvent
*e
);
158 void movemouse(Client
*c
);
159 Client
*nexttiled(Client
*c
);
160 void propertynotify(XEvent
*e
);
161 void quit(const char *arg
);
162 void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
);
163 void resizemouse(Client
*c
);
167 void setclientstate(Client
*c
, long state
);
168 void setlayout(const char *arg
);
169 void setmwfact(const char *arg
);
171 void spawn(const char *arg
);
172 void tag(const char *arg
);
173 unsigned int textnw(const char *text
, unsigned int len
);
174 unsigned int textw(const char *text
);
176 void togglebar(const char *arg
);
177 void togglefloating(const char *arg
);
178 void togglemax(const char *arg
);
179 void toggletag(const char *arg
);
180 void toggleview(const char *arg
);
181 void unban(Client
*c
);
182 void unmanage(Client
*c
);
183 void unmapnotify(XEvent
*e
);
184 void updatebarpos(void);
185 void updatesizehints(Client
*c
);
186 void updatetitle(Client
*c
);
187 void view(const char *arg
);
188 int xerror(Display
*dpy
, XErrorEvent
*ee
);
189 int xerrordummy(Display
*dsply
, XErrorEvent
*ee
);
190 int xerrorstart(Display
*dsply
, XErrorEvent
*ee
);
191 void zoom(const char *arg
);
196 int screen
, sx
, sy
, sw
, sh
, wax
, way
, waw
, wah
;
197 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
198 unsigned int bh
, bpos
, ntags
;
199 unsigned int blw
= 0;
200 unsigned int ltidx
= 0; /* default */
201 unsigned int nlayouts
= 0;
202 unsigned int nrules
= 0;
203 unsigned int numlockmask
= 0;
204 void (*handler
[LASTEvent
]) (XEvent
*) = {
205 [ButtonPress
] = buttonpress
,
206 [ConfigureRequest
] = configurerequest
,
207 [ConfigureNotify
] = configurenotify
,
208 [DestroyNotify
] = destroynotify
,
209 [EnterNotify
] = enternotify
,
210 [LeaveNotify
] = leavenotify
,
212 [KeyPress
] = keypress
,
213 [MappingNotify
] = mappingnotify
,
214 [MapRequest
] = maprequest
,
215 [PropertyNotify
] = propertynotify
,
216 [UnmapNotify
] = unmapnotify
218 Atom wmatom
[WMLast
], netatom
[NetLast
];
219 Bool otherwm
, readin
;
222 Bool selscreen
= True
;
223 Client
*clients
= NULL
;
225 Client
*stack
= NULL
;
226 Cursor cursor
[CurLast
];
232 /* configuration, allows nested code to access above variables */
237 applyrules(Client
*c
) {
238 static char buf
[512];
241 Bool matched
= False
;
242 XClassHint ch
= { 0 };
245 XGetClassHint(dpy
, c
->win
, &ch
);
246 snprintf(buf
, sizeof buf
, "%s:%s:%s",
247 ch
.res_class
? ch
.res_class
: "",
248 ch
.res_name
? ch
.res_name
: "", c
->name
);
249 for(i
= 0; i
< nrules
; i
++)
250 if(regs
[i
].propregex
&& !regexec(regs
[i
].propregex
, buf
, 1, &tmp
, 0)) {
251 c
->isfloating
= rules
[i
].isfloating
;
252 for(j
= 0; regs
[i
].tagregex
&& j
< ntags
; j
++) {
253 if(!regexec(regs
[i
].tagregex
, tags
[j
], 1, &tmp
, 0)) {
264 for(i
= 0; i
< ntags
; i
++)
265 c
->tags
[i
] = seltags
[i
];
272 for(c
= clients
; c
; c
= c
->next
)
277 layouts
[ltidx
].arrange();
291 attachstack(Client
*c
) {
300 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
305 buttonpress(XEvent
*e
) {
308 XButtonPressedEvent
*ev
= &e
->xbutton
;
310 if(barwin
== ev
->window
) {
312 for(i
= 0; i
< ntags
; i
++) {
315 if(ev
->button
== Button1
) {
316 if(ev
->state
& MODKEY
)
321 else if(ev
->button
== Button3
) {
322 if(ev
->state
& MODKEY
)
330 if((ev
->x
< x
+ blw
) && ev
->button
== Button1
)
333 else if((c
= getclient(ev
->window
))) {
335 if(CLEANMASK(ev
->state
) != MODKEY
)
337 if(ev
->button
== Button1
) {
338 if(isarrange(floating
) || c
->isfloating
)
341 togglefloating(NULL
);
344 else if(ev
->button
== Button2
) {
345 if(ISTILE
&& !c
->isfixed
&& c
->isfloating
)
346 togglefloating(NULL
);
350 else if(ev
->button
== Button3
&& !c
->isfixed
) {
351 if(isarrange(floating
) || c
->isfloating
)
354 togglefloating(NULL
);
363 XSetErrorHandler(xerrorstart
);
365 /* this causes an error if some other window manager is running */
366 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
369 eprint("dwm: another window manager is already running\n");
371 XSetErrorHandler(NULL
);
372 xerrorxlib
= XSetErrorHandler(xerror
);
384 XFreeFontSet(dpy
, dc
.font
.set
);
386 XFreeFont(dpy
, dc
.font
.xfont
);
387 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
388 XFreePixmap(dpy
, dc
.drawable
);
390 XDestroyWindow(dpy
, barwin
);
391 XFreeCursor(dpy
, cursor
[CurNormal
]);
392 XFreeCursor(dpy
, cursor
[CurResize
]);
393 XFreeCursor(dpy
, cursor
[CurMove
]);
394 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
406 nrules
= sizeof rules
/ sizeof rules
[0];
407 regs
= emallocz(nrules
* sizeof(Regs
));
408 for(i
= 0; i
< nrules
; i
++) {
410 reg
= emallocz(sizeof(regex_t
));
411 if(regcomp(reg
, rules
[i
].prop
, REG_EXTENDED
))
414 regs
[i
].propregex
= reg
;
417 reg
= emallocz(sizeof(regex_t
));
418 if(regcomp(reg
, rules
[i
].tags
, REG_EXTENDED
))
421 regs
[i
].tagregex
= reg
;
427 configure(Client
*c
) {
430 ce
.type
= ConfigureNotify
;
438 ce
.border_width
= c
->border
;
440 ce
.override_redirect
= False
;
441 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
445 configurenotify(XEvent
*e
) {
446 XConfigureEvent
*ev
= &e
->xconfigure
;
448 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
451 XFreePixmap(dpy
, dc
.drawable
);
452 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
453 XResizeWindow(dpy
, barwin
, sw
, bh
);
460 configurerequest(XEvent
*e
) {
462 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
465 if((c
= getclient(ev
->window
))) {
467 if(ev
->value_mask
& CWBorderWidth
)
468 c
->border
= ev
->border_width
;
469 if(c
->isfixed
|| c
->isfloating
|| isarrange(floating
)) {
470 if(ev
->value_mask
& CWX
)
472 if(ev
->value_mask
& CWY
)
474 if(ev
->value_mask
& CWWidth
)
476 if(ev
->value_mask
& CWHeight
)
478 if((c
->x
+ c
->w
) > sw
&& c
->isfloating
)
479 c
->x
= sw
/ 2 - c
->w
/ 2; /* center in x direction */
480 if((c
->y
+ c
->h
) > sh
&& c
->isfloating
)
481 c
->y
= sh
/ 2 - c
->h
/ 2; /* center in y direction */
482 if((ev
->value_mask
& (CWX
| CWY
))
483 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
486 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
494 wc
.width
= ev
->width
;
495 wc
.height
= ev
->height
;
496 wc
.border_width
= ev
->border_width
;
497 wc
.sibling
= ev
->above
;
498 wc
.stack_mode
= ev
->detail
;
499 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
505 destroynotify(XEvent
*e
) {
507 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
509 if((c
= getclient(ev
->window
)))
516 c
->prev
->next
= c
->next
;
518 c
->next
->prev
= c
->prev
;
521 c
->next
= c
->prev
= NULL
;
525 detachstack(Client
*c
) {
528 for(tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
537 for(i
= 0; i
< ntags
; i
++) {
538 dc
.w
= textw(tags
[i
]);
540 drawtext(tags
[i
], dc
.sel
);
541 drawsquare(sel
&& sel
->tags
[i
], isoccupied(i
), dc
.sel
);
544 drawtext(tags
[i
], dc
.norm
);
545 drawsquare(sel
&& sel
->tags
[i
], isoccupied(i
), dc
.norm
);
550 drawtext(layouts
[ltidx
].symbol
, dc
.norm
);
558 drawtext(stext
, dc
.norm
);
559 if((dc
.w
= dc
.x
- x
) > bh
) {
562 drawtext(sel
->name
, dc
.sel
);
563 drawsquare(sel
->ismax
, sel
->isfloating
, dc
.sel
);
566 drawtext(NULL
, dc
.norm
);
568 XCopyArea(dpy
, dc
.drawable
, barwin
, dc
.gc
, 0, 0, sw
, bh
, 0, 0);
573 drawsquare(Bool filled
, Bool empty
, unsigned long col
[ColLast
]) {
576 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
578 gcv
.foreground
= col
[ColFG
];
579 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
580 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
584 r
.width
= r
.height
= x
+ 1;
585 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
588 r
.width
= r
.height
= x
;
589 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
594 drawtext(const char *text
, unsigned long col
[ColLast
]) {
596 static char buf
[256];
597 unsigned int len
, olen
;
598 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
600 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
601 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
605 olen
= len
= strlen(text
);
606 if(len
>= sizeof buf
)
607 len
= sizeof buf
- 1;
608 memcpy(buf
, text
, len
);
610 h
= dc
.font
.ascent
+ dc
.font
.descent
;
611 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
613 /* shorten text if necessary */
614 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
625 return; /* too long */
626 XSetForeground(dpy
, dc
.gc
, col
[ColFG
]);
628 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
630 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
634 emallocz(unsigned int size
) {
635 void *res
= calloc(1, size
);
638 eprint("fatal: could not malloc() %u bytes\n", size
);
643 enternotify(XEvent
*e
) {
645 XCrossingEvent
*ev
= &e
->xcrossing
;
647 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
649 if((c
= getclient(ev
->window
))) {
651 if(ISTILE
&& !c
->isfloating
)
654 else if(ev
->window
== root
) {
661 eprint(const char *errstr
, ...) {
664 va_start(ap
, errstr
);
665 vfprintf(stderr
, errstr
, ap
);
672 XExposeEvent
*ev
= &e
->xexpose
;
675 if(barwin
== ev
->window
)
681 floating(void) { /* default floating layout */
684 for(c
= clients
; c
; c
= c
->next
)
686 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
691 if((!c
&& selscreen
) || (c
&& !isvisible(c
)))
692 for(c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
693 if(sel
&& sel
!= c
) {
694 grabbuttons(sel
, False
);
695 XSetWindowBorder(dpy
, sel
->win
, dc
.norm
[ColBorder
]);
700 grabbuttons(c
, True
);
707 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
708 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
711 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
715 focusnext(const char *arg
) {
720 for(c
= sel
->next
; c
&& !isvisible(c
); c
= c
->next
);
722 for(c
= clients
; c
&& !isvisible(c
); c
= c
->next
);
730 focusprev(const char *arg
) {
735 for(c
= sel
->prev
; c
&& !isvisible(c
); c
= c
->prev
);
737 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
738 for(; c
&& !isvisible(c
); c
= c
->prev
);
747 getclient(Window w
) {
750 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
755 getcolor(const char *colstr
) {
756 Colormap cmap
= DefaultColormap(dpy
, screen
);
759 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
760 eprint("error, cannot allocate color '%s'\n", colstr
);
768 unsigned char *p
= NULL
;
769 unsigned long n
, extra
;
772 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
773 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
774 if(status
!= Success
)
783 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
788 if(!text
|| size
== 0)
791 XGetTextProperty(dpy
, w
, &name
, atom
);
794 if(name
.encoding
== XA_STRING
)
795 strncpy(text
, (char *)name
.value
, size
- 1);
797 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
800 strncpy(text
, *list
, size
- 1);
801 XFreeStringList(list
);
804 text
[size
- 1] = '\0';
810 grabbuttons(Client
*c
, Bool focused
) {
811 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
814 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
815 GrabModeAsync
, GrabModeSync
, None
, None
);
816 XGrabButton(dpy
, Button1
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
817 GrabModeAsync
, GrabModeSync
, None
, None
);
818 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
819 GrabModeAsync
, GrabModeSync
, None
, None
);
820 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
821 GrabModeAsync
, GrabModeSync
, None
, None
);
823 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
824 GrabModeAsync
, GrabModeSync
, None
, None
);
825 XGrabButton(dpy
, Button2
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
826 GrabModeAsync
, GrabModeSync
, None
, None
);
827 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
828 GrabModeAsync
, GrabModeSync
, None
, None
);
829 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
830 GrabModeAsync
, GrabModeSync
, None
, None
);
832 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
833 GrabModeAsync
, GrabModeSync
, None
, None
);
834 XGrabButton(dpy
, Button3
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
835 GrabModeAsync
, GrabModeSync
, None
, None
);
836 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
837 GrabModeAsync
, GrabModeSync
, None
, None
);
838 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
839 GrabModeAsync
, GrabModeSync
, None
, None
);
842 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
843 GrabModeAsync
, GrabModeSync
, None
, None
);
847 idxoftag(const char *tag
) {
850 for(i
= 0; i
< ntags
; i
++)
857 initfont(const char *fontstr
) {
858 char *def
, **missing
;
863 XFreeFontSet(dpy
, dc
.font
.set
);
864 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
867 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
868 XFreeStringList(missing
);
871 XFontSetExtents
*font_extents
;
872 XFontStruct
**xfonts
;
874 dc
.font
.ascent
= dc
.font
.descent
= 0;
875 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
876 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
877 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
878 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
879 dc
.font
.ascent
= (*xfonts
)->ascent
;
880 if(dc
.font
.descent
< (*xfonts
)->descent
)
881 dc
.font
.descent
= (*xfonts
)->descent
;
887 XFreeFont(dpy
, dc
.font
.xfont
);
888 dc
.font
.xfont
= NULL
;
889 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
890 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
891 eprint("error, cannot load font: '%s'\n", fontstr
);
892 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
893 dc
.font
.descent
= dc
.font
.xfont
->descent
;
895 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
899 isarrange(void (*func
)())
901 return func
== layouts
[ltidx
].arrange
;
905 isoccupied(unsigned int t
) {
908 for(c
= clients
; c
; c
= c
->next
)
915 isprotodel(Client
*c
) {
920 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
921 for(i
= 0; !ret
&& i
< n
; i
++)
922 if(protocols
[i
] == wmatom
[WMDelete
])
930 isvisible(Client
*c
) {
933 for(i
= 0; i
< ntags
; i
++)
934 if(c
->tags
[i
] && seltags
[i
])
940 keypress(XEvent
*e
) {
942 unsigned int len
= sizeof keys
/ sizeof keys
[0];
948 if(!e
) { /* grabkeys */
949 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
950 for(i
= 0; i
< len
; i
++) {
951 code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
);
952 XGrabKey(dpy
, code
, keys
[i
].mod
, root
, True
,
953 GrabModeAsync
, GrabModeAsync
);
954 XGrabKey(dpy
, code
, keys
[i
].mod
| LockMask
, root
, True
,
955 GrabModeAsync
, GrabModeAsync
);
956 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
, root
, True
,
957 GrabModeAsync
, GrabModeAsync
);
958 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
| LockMask
, root
, True
,
959 GrabModeAsync
, GrabModeAsync
);
964 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
965 for(i
= 0; i
< len
; i
++)
966 if(keysym
== keys
[i
].keysym
967 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
970 keys
[i
].func(keys
[i
].arg
);
975 killclient(const char *arg
) {
980 if(isprotodel(sel
)) {
981 ev
.type
= ClientMessage
;
982 ev
.xclient
.window
= sel
->win
;
983 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
984 ev
.xclient
.format
= 32;
985 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
986 ev
.xclient
.data
.l
[1] = CurrentTime
;
987 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
990 XKillClient(dpy
, sel
->win
);
994 leavenotify(XEvent
*e
) {
995 XCrossingEvent
*ev
= &e
->xcrossing
;
997 if((ev
->window
== root
) && !ev
->same_screen
) {
1004 manage(Window w
, XWindowAttributes
*wa
) {
1006 Client
*c
, *t
= NULL
;
1011 c
= emallocz(sizeof(Client
));
1012 c
->tags
= emallocz(ntags
* sizeof(Bool
));
1018 c
->oldborder
= wa
->border_width
;
1019 if(c
->w
== sw
&& c
->h
== sh
) {
1022 c
->border
= wa
->border_width
;
1025 if(c
->x
+ c
->w
+ 2 * c
->border
> wax
+ waw
)
1026 c
->x
= wax
+ waw
- c
->w
- 2 * c
->border
;
1027 if(c
->y
+ c
->h
+ 2 * c
->border
> way
+ wah
)
1028 c
->y
= way
+ wah
- c
->h
- 2 * c
->border
;
1033 c
->border
= BORDERPX
;
1035 wc
.border_width
= c
->border
;
1036 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1037 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1038 configure(c
); /* propagates border_width, if size doesn't change */
1040 XSelectInput(dpy
, w
,
1041 StructureNotifyMask
| PropertyChangeMask
| EnterWindowMask
);
1042 grabbuttons(c
, False
);
1044 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
1045 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
1047 for(i
= 0; i
< ntags
; i
++)
1048 c
->tags
[i
] = t
->tags
[i
];
1051 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
1054 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1056 XMapWindow(dpy
, c
->win
);
1057 setclientstate(c
, NormalState
);
1062 mappingnotify(XEvent
*e
) {
1063 XMappingEvent
*ev
= &e
->xmapping
;
1065 XRefreshKeyboardMapping(ev
);
1066 if(ev
->request
== MappingKeyboard
)
1071 maprequest(XEvent
*e
) {
1072 static XWindowAttributes wa
;
1073 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1075 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1077 if(wa
.override_redirect
)
1079 if(!getclient(ev
->window
))
1080 manage(ev
->window
, &wa
);
1084 movemouse(Client
*c
) {
1085 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1092 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1093 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1096 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1098 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1101 XUngrabPointer(dpy
, CurrentTime
);
1103 case ConfigureRequest
:
1106 handler
[ev
.type
](&ev
);
1110 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1111 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1112 if(abs(wax
+ nx
) < SNAP
)
1114 else if(abs((wax
+ waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
1115 nx
= wax
+ waw
- c
->w
- 2 * c
->border
;
1116 if(abs(way
- ny
) < SNAP
)
1118 else if(abs((way
+ wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
1119 ny
= way
+ wah
- c
->h
- 2 * c
->border
;
1120 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1127 nexttiled(Client
*c
) {
1128 for(; c
&& (c
->isfloating
|| !isvisible(c
)); c
= c
->next
);
1133 propertynotify(XEvent
*e
) {
1136 XPropertyEvent
*ev
= &e
->xproperty
;
1138 if(ev
->state
== PropertyDelete
)
1139 return; /* ignore */
1140 if((c
= getclient(ev
->window
))) {
1143 case XA_WM_TRANSIENT_FOR
:
1144 XGetTransientForHint(dpy
, c
->win
, &trans
);
1145 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1148 case XA_WM_NORMAL_HINTS
:
1152 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1161 quit(const char *arg
) {
1162 readin
= running
= False
;
1166 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1167 double dx
, dy
, max
, min
, ratio
;
1171 if(c
->minay
> 0 && c
->maxay
> 0 && (h
- c
->baseh
) > 0 && (w
- c
->basew
) > 0) {
1172 dx
= (double)(w
- c
->basew
);
1173 dy
= (double)(h
- c
->baseh
);
1174 min
= (double)(c
->minax
) / (double)(c
->minay
);
1175 max
= (double)(c
->maxax
) / (double)(c
->maxay
);
1177 if(max
> 0 && min
> 0 && ratio
> 0) {
1179 dy
= (dx
* min
+ dy
) / (min
* min
+ 1);
1181 w
= (int)dx
+ c
->basew
;
1182 h
= (int)dy
+ c
->baseh
;
1184 else if(ratio
> max
) {
1185 dy
= (dx
* min
+ dy
) / (max
* max
+ 1);
1187 w
= (int)dx
+ c
->basew
;
1188 h
= (int)dy
+ c
->baseh
;
1192 if(c
->minw
&& w
< c
->minw
)
1194 if(c
->minh
&& h
< c
->minh
)
1196 if(c
->maxw
&& w
> c
->maxw
)
1198 if(c
->maxh
&& h
> c
->maxh
)
1201 w
-= (w
- c
->basew
) % c
->incw
;
1203 h
-= (h
- c
->baseh
) % c
->inch
;
1205 if(w
<= 0 || h
<= 0)
1207 /* offscreen appearance fixes */
1209 x
= sw
- w
- 2 * c
->border
;
1211 y
= sh
- h
- 2 * c
->border
;
1212 if(x
+ w
+ 2 * c
->border
< sx
)
1214 if(y
+ h
+ 2 * c
->border
< sy
)
1216 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1219 c
->w
= wc
.width
= w
;
1220 c
->h
= wc
.height
= h
;
1221 wc
.border_width
= c
->border
;
1222 XConfigureWindow(dpy
, c
->win
, CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
, &wc
);
1229 resizemouse(Client
*c
) {
1236 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1237 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1240 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1242 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1245 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1246 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1247 XUngrabPointer(dpy
, CurrentTime
);
1248 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1250 case ConfigureRequest
:
1253 handler
[ev
.type
](&ev
);
1257 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
1259 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
1261 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1276 if(sel
->isfloating
|| isarrange(floating
))
1277 XRaiseWindow(dpy
, sel
->win
);
1278 if(!isarrange(floating
)) {
1279 wc
.stack_mode
= Below
;
1280 wc
.sibling
= barwin
;
1281 if(!sel
->isfloating
) {
1282 XConfigureWindow(dpy
, sel
->win
, CWSibling
| CWStackMode
, &wc
);
1283 wc
.sibling
= sel
->win
;
1285 for(c
= nexttiled(clients
); c
; c
= nexttiled(c
->next
)) {
1288 XConfigureWindow(dpy
, c
->win
, CWSibling
| CWStackMode
, &wc
);
1289 wc
.sibling
= c
->win
;
1293 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1303 /* main event loop, also reads status text from stdin */
1305 xfd
= ConnectionNumber(dpy
);
1310 FD_SET(STDIN_FILENO
, &rd
);
1312 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1315 eprint("select failed\n");
1317 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1318 switch(r
= read(STDIN_FILENO
, stext
, sizeof stext
- 1)) {
1320 strncpy(stext
, strerror(errno
), sizeof stext
- 1);
1321 stext
[sizeof stext
- 1] = '\0';
1325 strncpy(stext
, "EOF", 4);
1329 for(stext
[r
] = '\0', p
= stext
+ strlen(stext
) - 1; p
>= stext
&& *p
== '\n'; *p
-- = '\0');
1330 for(; p
>= stext
&& *p
!= '\n'; --p
);
1332 strncpy(stext
, p
+ 1, sizeof stext
);
1336 while(XPending(dpy
)) {
1337 XNextEvent(dpy
, &ev
);
1338 if(handler
[ev
.type
])
1339 (handler
[ev
.type
])(&ev
); /* call handler */
1346 unsigned int i
, num
;
1347 Window
*wins
, d1
, d2
;
1348 XWindowAttributes wa
;
1351 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1352 for(i
= 0; i
< num
; i
++) {
1353 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1354 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1356 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1357 manage(wins
[i
], &wa
);
1359 for(i
= 0; i
< num
; i
++) { /* now the transients */
1360 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1362 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1363 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1364 manage(wins
[i
], &wa
);
1372 setclientstate(Client
*c
, long state
) {
1373 long data
[] = {state
, None
};
1375 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1376 PropModeReplace
, (unsigned char *)data
, 2);
1380 setlayout(const char *arg
) {
1384 if(++ltidx
== nlayouts
)
1388 for(i
= 0; i
< nlayouts
; i
++)
1389 if(!strcmp(arg
, layouts
[i
].symbol
))
1402 setmwfact(const char *arg
) {
1407 /* arg handling, manipulate mwfact */
1410 else if(1 == sscanf(arg
, "%lf", &delta
)) {
1411 if(arg
[0] == '+' || arg
[0] == '-')
1417 else if(mwfact
> 0.9)
1426 unsigned int i
, j
, mask
;
1428 XModifierKeymap
*modmap
;
1429 XSetWindowAttributes wa
;
1432 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1433 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1434 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1435 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1436 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1437 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1438 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1439 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1442 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1443 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1444 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1448 sw
= DisplayWidth(dpy
, screen
);
1449 sh
= DisplayHeight(dpy
, screen
);
1451 /* init modifier map */
1452 modmap
= XGetModifierMapping(dpy
);
1453 for(i
= 0; i
< 8; i
++)
1454 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
1455 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1456 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1457 numlockmask
= (1 << i
);
1459 XFreeModifiermap(modmap
);
1461 /* select for events */
1462 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
1463 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
1464 wa
.cursor
= cursor
[CurNormal
];
1465 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
1466 XSelectInput(dpy
, root
, wa
.event_mask
);
1473 for(ntags
= 0; tags
[ntags
]; ntags
++);
1474 seltags
= emallocz(sizeof(Bool
) * ntags
);
1477 /* init appearance */
1478 dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
);
1479 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
1480 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
1481 dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
);
1482 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
1483 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
1485 dc
.h
= bh
= dc
.font
.height
+ 2;
1489 nlayouts
= sizeof layouts
/ sizeof layouts
[0];
1490 for(blw
= i
= 0; i
< nlayouts
; i
++) {
1491 j
= textw(layouts
[i
].symbol
);
1498 wa
.override_redirect
= 1;
1499 wa
.background_pixmap
= ParentRelative
;
1500 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
1501 barwin
= XCreateWindow(dpy
, root
, sx
, sy
, sw
, bh
, 0,
1502 DefaultDepth(dpy
, screen
), CopyFromParent
, DefaultVisual(dpy
, screen
),
1503 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
1504 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
1506 XMapRaised(dpy
, barwin
);
1507 strcpy(stext
, "dwm-"VERSION
);
1508 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
1509 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1510 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1512 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1514 /* multihead support */
1515 selscreen
= XQueryPointer(dpy
, root
, &w
, &w
, &d
, &d
, &d
, &d
, &mask
);
1519 spawn(const char *arg
) {
1520 static char *shell
= NULL
;
1522 if(!shell
&& !(shell
= getenv("SHELL")))
1526 /* The double-fork construct avoids zombie processes and keeps the code
1527 * clean from stupid signal handlers. */
1531 close(ConnectionNumber(dpy
));
1533 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1534 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1543 tag(const char *arg
) {
1548 for(i
= 0; i
< ntags
; i
++)
1549 sel
->tags
[i
] = arg
== NULL
;
1551 if(i
>= 0 && i
< ntags
)
1552 sel
->tags
[i
] = True
;
1557 textnw(const char *text
, unsigned int len
) {
1561 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1564 return XTextWidth(dc
.font
.xfont
, text
, len
);
1568 textw(const char *text
) {
1569 return textnw(text
, strlen(text
)) + dc
.font
.height
;
1574 unsigned int i
, n
, nx
, ny
, nw
, nh
, mw
, th
;
1577 for(n
= 0, c
= nexttiled(clients
); c
; c
= nexttiled(c
->next
))
1581 mw
= (n
== 1) ? waw
: mwfact
* waw
;
1582 th
= (n
> 1) ? wah
/ (n
- 1) : 0;
1583 if(n
> 1 && th
< bh
)
1588 nw
= 0; /* gcc stupidity requires this */
1589 for(i
= 0, c
= mc
= nexttiled(clients
); c
; c
= nexttiled(c
->next
), i
++) {
1591 if(i
== 0) { /* master */
1592 nw
= mw
- 2 * c
->border
;
1593 nh
= wah
- 2 * c
->border
;
1595 else { /* tile window */
1598 nx
+= mc
->w
+ mc
->border
;
1599 nw
= waw
- nx
- 2 * c
->border
;
1601 if(i
+ 1 == n
) /* remainder */
1602 nh
= (way
+ wah
) - ny
- 2 * c
->border
;
1604 nh
= th
- 2 * c
->border
;
1606 resize(c
, nx
, ny
, nw
, nh
, RESIZEHINTS
);
1607 if(n
> 1 && th
!= wah
)
1608 ny
= c
->y
+ c
->h
+ c
->border
;
1613 togglebar(const char *arg
) {
1615 bpos
= (BARPOS
== BarOff
) ? BarTop
: BARPOS
;
1623 togglefloating(const char *arg
) {
1626 sel
->isfloating
= !sel
->isfloating
;
1628 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1633 togglemax(const char *arg
) {
1636 if(!sel
|| sel
->isfixed
)
1638 if((sel
->ismax
= !sel
->ismax
)) {
1639 if(isarrange(floating
) || sel
->isfloating
)
1640 sel
->wasfloating
= True
;
1642 togglefloating(NULL
);
1643 sel
->wasfloating
= False
;
1649 resize(sel
, wax
, way
, waw
- 2 * sel
->border
, wah
- 2 * sel
->border
, True
);
1652 resize(sel
, sel
->rx
, sel
->ry
, sel
->rw
, sel
->rh
, True
);
1653 if(!sel
->wasfloating
)
1654 togglefloating(NULL
);
1657 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1661 toggletag(const char *arg
) {
1667 sel
->tags
[i
] = !sel
->tags
[i
];
1668 for(j
= 0; j
< ntags
&& !sel
->tags
[j
]; j
++);
1670 sel
->tags
[i
] = True
;
1675 toggleview(const char *arg
) {
1679 seltags
[i
] = !seltags
[i
];
1680 for(j
= 0; j
< ntags
&& !seltags
[j
]; j
++);
1682 seltags
[i
] = True
; /* at least one tag must be viewed */
1690 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1691 c
->isbanned
= False
;
1695 unmanage(Client
*c
) {
1698 wc
.border_width
= c
->oldborder
;
1699 /* The server grab construct avoids race conditions. */
1701 XSetErrorHandler(xerrordummy
);
1702 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1707 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1708 setclientstate(c
, WithdrawnState
);
1712 XSetErrorHandler(xerror
);
1718 unmapnotify(XEvent
*e
) {
1720 XUnmapEvent
*ev
= &e
->xunmap
;
1722 if((c
= getclient(ev
->window
)))
1727 updatebarpos(void) {
1738 XMoveWindow(dpy
, barwin
, sx
, sy
);
1742 XMoveWindow(dpy
, barwin
, sx
, sy
+ wah
);
1745 XMoveWindow(dpy
, barwin
, sx
, sy
- bh
);
1749 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1753 updatesizehints(Client
*c
) {
1757 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1759 c
->flags
= size
.flags
;
1760 if(c
->flags
& PBaseSize
) {
1761 c
->basew
= size
.base_width
;
1762 c
->baseh
= size
.base_height
;
1764 else if(c
->flags
& PMinSize
) {
1765 c
->basew
= size
.min_width
;
1766 c
->baseh
= size
.min_height
;
1769 c
->basew
= c
->baseh
= 0;
1770 if(c
->flags
& PResizeInc
) {
1771 c
->incw
= size
.width_inc
;
1772 c
->inch
= size
.height_inc
;
1775 c
->incw
= c
->inch
= 0;
1776 if(c
->flags
& PMaxSize
) {
1777 c
->maxw
= size
.max_width
;
1778 c
->maxh
= size
.max_height
;
1781 c
->maxw
= c
->maxh
= 0;
1782 if(c
->flags
& PMinSize
) {
1783 c
->minw
= size
.min_width
;
1784 c
->minh
= size
.min_height
;
1786 else if(c
->flags
& PBaseSize
) {
1787 c
->minw
= size
.base_width
;
1788 c
->minh
= size
.base_height
;
1791 c
->minw
= c
->minh
= 0;
1792 if(c
->flags
& PAspect
) {
1793 c
->minax
= size
.min_aspect
.x
;
1794 c
->maxax
= size
.max_aspect
.x
;
1795 c
->minay
= size
.min_aspect
.y
;
1796 c
->maxay
= size
.max_aspect
.y
;
1799 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1800 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1801 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1805 updatetitle(Client
*c
) {
1806 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1807 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1810 /* There's no way to check accesses to destroyed windows, thus those cases are
1811 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1812 * default error handler, which may call exit. */
1814 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1815 if(ee
->error_code
== BadWindow
1816 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1817 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1818 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1819 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1820 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1821 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1822 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1824 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1825 ee
->request_code
, ee
->error_code
);
1826 return xerrorxlib(dpy
, ee
); /* may call exit */
1830 xerrordummy(Display
*dsply
, XErrorEvent
*ee
) {
1834 /* Startup Error handler to check if another window manager
1835 * is already running. */
1837 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
1843 view(const char *arg
) {
1846 for(i
= 0; i
< ntags
; i
++)
1847 seltags
[i
] = arg
== NULL
;
1849 if(i
>= 0 && i
< ntags
)
1855 zoom(const char *arg
) {
1858 if(!sel
|| !ISTILE
|| sel
->isfloating
)
1860 if((c
= sel
) == nexttiled(clients
))
1861 if(!(c
= nexttiled(c
->next
)))
1870 main(int argc
, char *argv
[]) {
1871 if(argc
== 2 && !strcmp("-v", argv
[1]))
1872 eprint("dwm-"VERSION
", © 2006-2007 A. R. Garbe, S. van Dijk, J. Salmi, P. Hruby, S. Nagy\n");
1874 eprint("usage: dwm [-v]\n");
1876 setlocale(LC_CTYPE
, "");
1877 if(!(dpy
= XOpenDisplay(0)))
1878 eprint("dwm: cannot open display\n");
1879 screen
= DefaultScreen(dpy
);
1880 root
= RootWindow(dpy
, screen
);