Xinqi Bao's Git
e1c8c200c2baa6038277e14da39fa92609232507
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>
36 #include <X11/cursorfont.h>
37 #include <X11/keysym.h>
38 #include <X11/Xatom.h>
40 #include <X11/Xproto.h>
41 #include <X11/Xutil.h>
44 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
45 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
46 #define LENGTH(x) (sizeof x / sizeof x[0])
48 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
49 #define DEFGEOM(GEONAME,BX,BY,BW,WX,WY,WW,WH,MX,MY,MW,MH,TX,TY,TW,TH,MOX,MOY,MOW,MOH) \
50 void GEONAME(void) { \
51 bx = (BX); by = (BY); bw = (BW); \
52 wx = (WX); wy = (WY); ww = (WW); wh = (WH); \
53 mx = (MX); my = (MY); mw = (MW); mh = (MH); \
54 tx = (TX); ty = (TY); tw = (TW); th = (TH); \
55 mox = (MOX); moy = (MOY); mow = (MOW); moh = (MOH); \
59 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
60 enum { ColBorder
, ColFG
, ColBG
, ColLast
}; /* color */
61 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
62 enum { WMProtocols
, WMDelete
, WMName
, WMState
, WMLast
};/* default atoms */
65 typedef struct Client Client
;
69 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
70 int minax
, maxax
, minay
, maxay
;
72 unsigned int bw
, oldbw
;
73 Bool isbanned
, isfixed
, isfloating
, isurgent
;
83 unsigned long norm
[ColLast
];
84 unsigned long sel
[ColLast
];
94 } DC
; /* draw context */
104 void (*func
)(const char *arg
);
110 void (*arrange
)(void);
116 const char *instance
;
122 /* function declarations */
123 void applyrules(Client
*c
);
125 void attach(Client
*c
);
126 void attachstack(Client
*c
);
128 void buttonpress(XEvent
*e
);
129 void checkotherwm(void);
131 void configure(Client
*c
);
132 void configurenotify(XEvent
*e
);
133 void configurerequest(XEvent
*e
);
134 unsigned int counttiled(void);
135 void destroynotify(XEvent
*e
);
136 void detach(Client
*c
);
137 void detachstack(Client
*c
);
139 void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
140 void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
141 void *emallocz(unsigned int size
);
142 void enternotify(XEvent
*e
);
143 void eprint(const char *errstr
, ...);
144 void expose(XEvent
*e
);
145 void floating(void); /* default floating layout */
146 void focus(Client
*c
);
147 void focusin(XEvent
*e
);
148 void focusnext(const char *arg
);
149 void focusprev(const char *arg
);
150 Client
*getclient(Window w
);
151 unsigned long getcolor(const char *colstr
);
152 long getstate(Window w
);
153 Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
154 void grabbuttons(Client
*c
, Bool focused
);
156 unsigned int idxoftag(const char *t
);
157 void initfont(const char *fontstr
);
158 Bool
isoccupied(unsigned int t
);
159 Bool
isprotodel(Client
*c
);
160 Bool
isurgent(unsigned int t
);
161 Bool
isvisible(Client
*c
);
162 void keypress(XEvent
*e
);
163 void killclient(const char *arg
);
164 void manage(Window w
, XWindowAttributes
*wa
);
165 void mappingnotify(XEvent
*e
);
166 void maprequest(XEvent
*e
);
168 void movemouse(Client
*c
);
169 Client
*nexttiled(Client
*c
);
170 void propertynotify(XEvent
*e
);
171 void quit(const char *arg
);
172 void reapply(const char *arg
);
173 void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
);
174 void resizemouse(Client
*c
);
178 void setclientstate(Client
*c
, long state
);
179 void setgeom(const char *arg
);
180 void setlayout(const char *arg
);
182 void spawn(const char *arg
);
183 void tag(const char *arg
);
184 unsigned int textnw(const char *text
, unsigned int len
);
185 unsigned int textw(const char *text
);
187 void tilehstack(unsigned int n
);
188 Client
*tilemaster(unsigned int n
);
189 void tileresize(Client
*c
, int x
, int y
, int w
, int h
);
191 void tilevstack(unsigned int n
);
192 void togglefloating(const char *arg
);
193 void toggletag(const char *arg
);
194 void toggleview(const char *arg
);
195 void unban(Client
*c
);
196 void unmanage(Client
*c
);
197 void unmapnotify(XEvent
*e
);
198 void updatebarpos(void);
199 void updatesizehints(Client
*c
);
200 void updatetitle(Client
*c
);
201 void updatewmhints(Client
*c
);
202 void view(const char *arg
);
203 void viewprevtag(const char *arg
); /* views previous selected tags */
204 int xerror(Display
*dpy
, XErrorEvent
*ee
);
205 int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
206 int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
207 void zoom(const char *arg
);
210 char stext
[256], buf
[256];
211 int screen
, sx
, sy
, sw
, sh
;
212 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
213 int bx
, by
, bw
, bh
, blw
, bgw
, mx
, my
, mw
, mh
, mox
, moy
, mow
, moh
, tx
, ty
, tw
, th
, wx
, wy
, ww
, wh
;
214 unsigned int numlockmask
= 0;
215 void (*handler
[LASTEvent
]) (XEvent
*) = {
216 [ButtonPress
] = buttonpress
,
217 [ConfigureRequest
] = configurerequest
,
218 [ConfigureNotify
] = configurenotify
,
219 [DestroyNotify
] = destroynotify
,
220 [EnterNotify
] = enternotify
,
223 [KeyPress
] = keypress
,
224 [MappingNotify
] = mappingnotify
,
225 [MapRequest
] = maprequest
,
226 [PropertyNotify
] = propertynotify
,
227 [UnmapNotify
] = unmapnotify
229 Atom wmatom
[WMLast
], netatom
[NetLast
];
230 Bool otherwm
, readin
;
234 Client
*clients
= NULL
;
236 Client
*stack
= NULL
;
237 Cursor cursor
[CurLast
];
244 /* configuration, allows nested code to access above variables */
246 #define TAGSZ (LENGTH(tags) * sizeof(Bool))
247 static Bool tmp
[LENGTH(tags
)];
249 /* function implementations */
252 applyrules(Client
*c
) {
254 Bool matched
= False
;
256 XClassHint ch
= { 0 };
259 XGetClassHint(dpy
, c
->win
, &ch
);
260 for(i
= 0; i
< LENGTH(rules
); i
++) {
262 if((r
->title
&& strstr(c
->name
, r
->title
))
263 || (ch
.res_class
&& r
->class && strstr(ch
.res_class
, r
->class))
264 || (ch
.res_name
&& r
->instance
&& strstr(ch
.res_name
, r
->instance
)))
266 c
->isfloating
= r
->isfloating
;
268 c
->tags
[idxoftag(r
->tag
)] = True
;
278 memcpy(c
->tags
, seltags
, TAGSZ
);
285 for(c
= clients
; c
; c
= c
->next
)
305 attachstack(Client
*c
) {
314 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
319 buttonpress(XEvent
*e
) {
322 XButtonPressedEvent
*ev
= &e
->xbutton
;
324 if(ev
->window
== barwin
) {
325 if((ev
->x
< bgw
) && ev
->button
== Button1
) {
330 for(i
= 0; i
< LENGTH(tags
); i
++) {
332 if(ev
->x
> bgw
&& ev
->x
< x
) {
333 if(ev
->button
== Button1
) {
334 if(ev
->state
& MODKEY
)
339 else if(ev
->button
== Button3
) {
340 if(ev
->state
& MODKEY
)
348 if((ev
->x
< x
+ blw
) && ev
->button
== Button1
)
351 else if((c
= getclient(ev
->window
))) {
353 if(CLEANMASK(ev
->state
) != MODKEY
)
355 if(ev
->button
== Button1
) {
359 else if(ev
->button
== Button2
) {
360 if((floating
!= lt
->arrange
) && c
->isfloating
)
361 togglefloating(NULL
);
365 else if(ev
->button
== Button3
&& !c
->isfixed
) {
375 XSetErrorHandler(xerrorstart
);
377 /* this causes an error if some other window manager is running */
378 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
381 eprint("dwm: another window manager is already running\n");
383 XSetErrorHandler(NULL
);
384 xerrorxlib
= XSetErrorHandler(xerror
);
396 XFreeFontSet(dpy
, dc
.font
.set
);
398 XFreeFont(dpy
, dc
.font
.xfont
);
399 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
400 XFreePixmap(dpy
, dc
.drawable
);
402 XFreeCursor(dpy
, cursor
[CurNormal
]);
403 XFreeCursor(dpy
, cursor
[CurResize
]);
404 XFreeCursor(dpy
, cursor
[CurMove
]);
405 XDestroyWindow(dpy
, barwin
);
407 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
411 configure(Client
*c
) {
414 ce
.type
= ConfigureNotify
;
422 ce
.border_width
= c
->bw
;
424 ce
.override_redirect
= False
;
425 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
429 configurenotify(XEvent
*e
) {
430 XConfigureEvent
*ev
= &e
->xconfigure
;
432 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
435 setgeom(geom
->symbol
);
440 configurerequest(XEvent
*e
) {
442 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
445 if((c
= getclient(ev
->window
))) {
446 if(ev
->value_mask
& CWBorderWidth
)
447 c
->bw
= ev
->border_width
;
448 if(c
->isfixed
|| c
->isfloating
|| lt
->isfloating
) {
449 if(ev
->value_mask
& CWX
)
451 if(ev
->value_mask
& CWY
)
453 if(ev
->value_mask
& CWWidth
)
455 if(ev
->value_mask
& CWHeight
)
457 if((c
->x
- sx
+ c
->w
) > sw
&& c
->isfloating
)
458 c
->x
= sx
+ (sw
/ 2 - c
->w
/ 2); /* center in x direction */
459 if((c
->y
- sy
+ c
->h
) > sh
&& c
->isfloating
)
460 c
->y
= sy
+ (sh
/ 2 - c
->h
/ 2); /* center in y direction */
461 if((ev
->value_mask
& (CWX
|CWY
))
462 && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
465 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
473 wc
.width
= ev
->width
;
474 wc
.height
= ev
->height
;
475 wc
.border_width
= ev
->border_width
;
476 wc
.sibling
= ev
->above
;
477 wc
.stack_mode
= ev
->detail
;
478 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
488 for(n
= 0, c
= nexttiled(clients
); c
; c
= nexttiled(c
->next
), n
++);
493 destroynotify(XEvent
*e
) {
495 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
497 if((c
= getclient(ev
->window
)))
504 c
->prev
->next
= c
->next
;
506 c
->next
->prev
= c
->prev
;
509 c
->next
= c
->prev
= NULL
;
513 detachstack(Client
*c
) {
516 for(tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
528 drawtext(geom
->symbol
, dc
.norm
, False
);
531 for(c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
532 for(i
= 0; i
< LENGTH(tags
); i
++) {
533 dc
.w
= textw(tags
[i
]);
535 drawtext(tags
[i
], dc
.sel
, isurgent(i
));
536 drawsquare(c
&& c
->tags
[i
], isoccupied(i
), isurgent(i
), dc
.sel
);
539 drawtext(tags
[i
], dc
.norm
, isurgent(i
));
540 drawsquare(c
&& c
->tags
[i
], isoccupied(i
), isurgent(i
), dc
.norm
);
546 drawtext(lt
->symbol
, dc
.norm
, False
);
557 drawtext(stext
, dc
.norm
, False
);
558 if((dc
.w
= dc
.x
- x
) > bh
) {
561 drawtext(c
->name
, dc
.sel
, False
);
562 drawsquare(False
, c
->isfloating
, False
, dc
.sel
);
565 drawtext(NULL
, dc
.norm
, False
);
567 XCopyArea(dpy
, dc
.drawable
, barwin
, dc
.gc
, 0, 0, bw
, bh
, 0, 0);
572 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
575 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
577 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
578 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
579 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
583 r
.width
= r
.height
= x
+ 1;
584 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
587 r
.width
= r
.height
= x
;
588 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
593 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
595 unsigned int len
, olen
;
596 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
598 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
599 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
603 olen
= len
= strlen(text
);
604 if(len
>= sizeof buf
)
605 len
= sizeof buf
- 1;
606 memcpy(buf
, text
, len
);
608 h
= dc
.font
.ascent
+ dc
.font
.descent
;
609 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
611 /* shorten text if necessary */
612 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
623 return; /* too long */
624 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
626 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
628 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
632 emallocz(unsigned int size
) {
633 void *res
= calloc(1, size
);
636 eprint("fatal: could not malloc() %u bytes\n", size
);
641 enternotify(XEvent
*e
) {
643 XCrossingEvent
*ev
= &e
->xcrossing
;
645 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
647 if((c
= getclient(ev
->window
)))
654 eprint(const char *errstr
, ...) {
657 va_start(ap
, errstr
);
658 vfprintf(stderr
, errstr
, ap
);
665 XExposeEvent
*ev
= &e
->xexpose
;
667 if(ev
->count
== 0 && (ev
->window
== barwin
))
672 floating(void) { /* default floating layout */
675 for(c
= clients
; c
; c
= c
->next
)
677 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
682 if(!c
|| (c
&& !isvisible(c
)))
683 for(c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
684 if(sel
&& sel
!= c
) {
685 grabbuttons(sel
, False
);
686 XSetWindowBorder(dpy
, sel
->win
, dc
.norm
[ColBorder
]);
691 grabbuttons(c
, True
);
695 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
696 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
699 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
704 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
705 XFocusChangeEvent
*ev
= &e
->xfocus
;
707 if(sel
&& ev
->window
!= sel
->win
)
708 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
712 focusnext(const char *arg
) {
717 for(c
= sel
->next
; c
&& !isvisible(c
); c
= c
->next
);
719 for(c
= clients
; c
&& !isvisible(c
); c
= c
->next
);
727 focusprev(const char *arg
) {
732 for(c
= sel
->prev
; c
&& !isvisible(c
); c
= c
->prev
);
734 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
735 for(; c
&& !isvisible(c
); c
= c
->prev
);
744 getclient(Window w
) {
747 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
752 getcolor(const char *colstr
) {
753 Colormap cmap
= DefaultColormap(dpy
, screen
);
756 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
757 eprint("error, cannot allocate color '%s'\n", colstr
);
765 unsigned char *p
= NULL
;
766 unsigned long n
, extra
;
769 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
770 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
771 if(status
!= Success
)
780 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
785 if(!text
|| size
== 0)
788 XGetTextProperty(dpy
, w
, &name
, atom
);
791 if(name
.encoding
== XA_STRING
)
792 strncpy(text
, (char *)name
.value
, size
- 1);
794 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
796 strncpy(text
, *list
, size
- 1);
797 XFreeStringList(list
);
800 text
[size
- 1] = '\0';
806 grabbuttons(Client
*c
, Bool focused
) {
807 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
810 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
811 GrabModeAsync
, GrabModeSync
, None
, None
);
812 XGrabButton(dpy
, Button1
, MODKEY
|LockMask
, c
->win
, False
, BUTTONMASK
,
813 GrabModeAsync
, GrabModeSync
, None
, None
);
814 XGrabButton(dpy
, Button1
, MODKEY
|numlockmask
, c
->win
, False
, BUTTONMASK
,
815 GrabModeAsync
, GrabModeSync
, None
, None
);
816 XGrabButton(dpy
, Button1
, MODKEY
|numlockmask
|LockMask
, c
->win
, False
, BUTTONMASK
,
817 GrabModeAsync
, GrabModeSync
, None
, None
);
819 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
820 GrabModeAsync
, GrabModeSync
, None
, None
);
821 XGrabButton(dpy
, Button2
, MODKEY
|LockMask
, c
->win
, False
, BUTTONMASK
,
822 GrabModeAsync
, GrabModeSync
, None
, None
);
823 XGrabButton(dpy
, Button2
, MODKEY
|numlockmask
, c
->win
, False
, BUTTONMASK
,
824 GrabModeAsync
, GrabModeSync
, None
, None
);
825 XGrabButton(dpy
, Button2
, MODKEY
|numlockmask
|LockMask
, c
->win
, False
, BUTTONMASK
,
826 GrabModeAsync
, GrabModeSync
, None
, None
);
828 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
829 GrabModeAsync
, GrabModeSync
, None
, None
);
830 XGrabButton(dpy
, Button3
, MODKEY
|LockMask
, c
->win
, False
, BUTTONMASK
,
831 GrabModeAsync
, GrabModeSync
, None
, None
);
832 XGrabButton(dpy
, Button3
, MODKEY
|numlockmask
, c
->win
, False
, BUTTONMASK
,
833 GrabModeAsync
, GrabModeSync
, None
, None
);
834 XGrabButton(dpy
, Button3
, MODKEY
|numlockmask
|LockMask
, c
->win
, False
, BUTTONMASK
,
835 GrabModeAsync
, GrabModeSync
, None
, None
);
838 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
839 GrabModeAsync
, GrabModeSync
, None
, None
);
846 XModifierKeymap
*modmap
;
848 /* init modifier map */
849 modmap
= XGetModifierMapping(dpy
);
850 for(i
= 0; i
< 8; i
++)
851 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
852 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
853 numlockmask
= (1 << i
);
855 XFreeModifiermap(modmap
);
857 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
858 for(i
= 0; i
< LENGTH(keys
); i
++) {
859 code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
);
860 XGrabKey(dpy
, code
, keys
[i
].mod
, root
, True
,
861 GrabModeAsync
, GrabModeAsync
);
862 XGrabKey(dpy
, code
, keys
[i
].mod
|LockMask
, root
, True
,
863 GrabModeAsync
, GrabModeAsync
);
864 XGrabKey(dpy
, code
, keys
[i
].mod
|numlockmask
, root
, True
,
865 GrabModeAsync
, GrabModeAsync
);
866 XGrabKey(dpy
, code
, keys
[i
].mod
|numlockmask
|LockMask
, root
, True
,
867 GrabModeAsync
, GrabModeAsync
);
872 idxoftag(const char *t
) {
875 for(i
= 0; (i
< LENGTH(tags
)) && t
&& strcmp(tags
[i
], t
); i
++);
876 return (i
< LENGTH(tags
)) ? i
: 0;
880 initfont(const char *fontstr
) {
881 char *def
, **missing
;
886 XFreeFontSet(dpy
, dc
.font
.set
);
887 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
890 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
891 XFreeStringList(missing
);
894 XFontSetExtents
*font_extents
;
895 XFontStruct
**xfonts
;
897 dc
.font
.ascent
= dc
.font
.descent
= 0;
898 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
899 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
900 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
901 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
902 dc
.font
.ascent
= (*xfonts
)->ascent
;
903 if(dc
.font
.descent
< (*xfonts
)->descent
)
904 dc
.font
.descent
= (*xfonts
)->descent
;
910 XFreeFont(dpy
, dc
.font
.xfont
);
911 dc
.font
.xfont
= NULL
;
912 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
913 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
914 eprint("error, cannot load font: '%s'\n", fontstr
);
915 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
916 dc
.font
.descent
= dc
.font
.xfont
->descent
;
918 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
922 isoccupied(unsigned int t
) {
925 for(c
= clients
; c
; c
= c
->next
)
932 isprotodel(Client
*c
) {
937 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
938 for(i
= 0; !ret
&& i
< n
; i
++)
939 if(protocols
[i
] == wmatom
[WMDelete
])
947 isurgent(unsigned int t
) {
950 for(c
= clients
; c
; c
= c
->next
)
951 if(c
->isurgent
&& c
->tags
[t
])
957 isvisible(Client
*c
) {
960 for(i
= 0; i
< LENGTH(tags
); i
++)
961 if(c
->tags
[i
] && seltags
[i
])
967 keypress(XEvent
*e
) {
973 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
974 for(i
= 0; i
< LENGTH(keys
); i
++)
975 if(keysym
== keys
[i
].keysym
976 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
979 keys
[i
].func(keys
[i
].arg
);
984 killclient(const char *arg
) {
989 if(isprotodel(sel
)) {
990 ev
.type
= ClientMessage
;
991 ev
.xclient
.window
= sel
->win
;
992 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
993 ev
.xclient
.format
= 32;
994 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
995 ev
.xclient
.data
.l
[1] = CurrentTime
;
996 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
999 XKillClient(dpy
, sel
->win
);
1003 manage(Window w
, XWindowAttributes
*wa
) {
1004 Client
*c
, *t
= NULL
;
1009 c
= emallocz(sizeof(Client
));
1010 c
->tags
= emallocz(TAGSZ
);
1018 c
->oldbw
= wa
->border_width
;
1019 if(c
->w
== sw
&& c
->h
== sh
) {
1022 c
->bw
= wa
->border_width
;
1025 if(c
->x
+ c
->w
+ 2 * c
->bw
> wx
+ ww
)
1026 c
->x
= wx
+ ww
- c
->w
- 2 * c
->bw
;
1027 if(c
->y
+ c
->h
+ 2 * c
->bw
> wy
+ wh
)
1028 c
->y
= wy
+ wh
- c
->h
- 2 * c
->bw
;
1036 wc
.border_width
= c
->bw
;
1037 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1038 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1039 configure(c
); /* propagates border_width, if size doesn't change */
1041 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1042 grabbuttons(c
, False
);
1044 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
1045 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
1047 memcpy(c
->tags
, t
->tags
, TAGSZ
);
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
);
1087 for(c
= clients
; c
; c
= c
->next
)
1089 resize(c
, mox
, moy
, mow
- 2 * c
->bw
, moh
- 2 * c
->bw
, RESIZEHINTS
);
1093 movemouse(Client
*c
) {
1094 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1101 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1102 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1104 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1106 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1109 XUngrabPointer(dpy
, CurrentTime
);
1111 case ConfigureRequest
:
1114 handler
[ev
.type
](&ev
);
1118 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1119 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1120 if(abs(wx
- nx
) < SNAP
)
1122 else if(abs((wx
+ ww
) - (nx
+ c
->w
+ 2 * c
->bw
)) < SNAP
)
1123 nx
= wx
+ ww
- c
->w
- 2 * c
->bw
;
1124 if(abs(wy
- ny
) < SNAP
)
1126 else if(abs((wy
+ wh
) - (ny
+ c
->h
+ 2 * c
->bw
)) < SNAP
)
1127 ny
= wy
+ wh
- c
->h
- 2 * c
->bw
;
1128 if(!c
->isfloating
&& !lt
->isfloating
&& (abs(nx
- c
->x
) > SNAP
|| abs(ny
- c
->y
) > SNAP
))
1129 togglefloating(NULL
);
1130 if((lt
->isfloating
) || c
->isfloating
)
1131 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1138 nexttiled(Client
*c
) {
1139 for(; c
&& (c
->isfloating
|| !isvisible(c
)); c
= c
->next
);
1144 propertynotify(XEvent
*e
) {
1147 XPropertyEvent
*ev
= &e
->xproperty
;
1149 if(ev
->state
== PropertyDelete
)
1150 return; /* ignore */
1151 if((c
= getclient(ev
->window
))) {
1154 case XA_WM_TRANSIENT_FOR
:
1155 XGetTransientForHint(dpy
, c
->win
, &trans
);
1156 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1159 case XA_WM_NORMAL_HINTS
:
1167 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1176 quit(const char *arg
) {
1177 readin
= running
= False
;
1181 reapply(const char *arg
) {
1182 static Bool zerotags
[LENGTH(tags
)] = { 0 };
1185 for(c
= clients
; c
; c
= c
->next
) {
1186 memcpy(c
->tags
, zerotags
, sizeof zerotags
);
1193 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1197 /* set minimum possible */
1203 /* temporarily remove base dimensions */
1207 /* adjust for aspect limits */
1208 if (c
->minay
> 0 && c
->maxay
> 0 && c
->minax
> 0 && c
->maxax
> 0) {
1209 if (w
* c
->maxay
> h
* c
->maxax
)
1210 w
= h
* c
->maxax
/ c
->maxay
;
1211 else if (w
* c
->minay
< h
* c
->minax
)
1212 h
= w
* c
->minay
/ c
->minax
;
1215 /* adjust for increment value */
1221 /* restore base dimensions */
1225 if(c
->minw
> 0 && w
< c
->minw
)
1227 if(c
->minh
> 0 && h
< c
->minh
)
1229 if(c
->maxw
> 0 && w
> c
->maxw
)
1231 if(c
->maxh
> 0 && h
> c
->maxh
)
1234 if(w
<= 0 || h
<= 0)
1237 x
= sw
- w
- 2 * c
->bw
;
1239 y
= sh
- h
- 2 * c
->bw
;
1240 if(x
+ w
+ 2 * c
->bw
< sx
)
1242 if(y
+ h
+ 2 * c
->bw
< sy
)
1244 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1247 c
->w
= wc
.width
= w
;
1248 c
->h
= wc
.height
= h
;
1249 wc
.border_width
= c
->bw
;
1250 XConfigureWindow(dpy
, c
->win
,
1251 CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1258 resizemouse(Client
*c
) {
1265 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1266 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1268 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1270 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1273 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1274 c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1275 XUngrabPointer(dpy
, CurrentTime
);
1276 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1278 case ConfigureRequest
:
1281 handler
[ev
.type
](&ev
);
1285 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1) <= 0)
1287 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1) <= 0)
1289 if(!c
->isfloating
&& !lt
->isfloating
&& (abs(nw
- c
->w
) > SNAP
|| abs(nh
- c
->h
) > SNAP
))
1290 togglefloating(NULL
);
1291 if((lt
->isfloating
) || c
->isfloating
)
1292 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1307 if(sel
->isfloating
|| lt
->isfloating
)
1308 XRaiseWindow(dpy
, sel
->win
);
1309 if(!lt
->isfloating
) {
1310 wc
.stack_mode
= Below
;
1311 wc
.sibling
= barwin
;
1312 if(!sel
->isfloating
) {
1313 XConfigureWindow(dpy
, sel
->win
, CWSibling
|CWStackMode
, &wc
);
1314 wc
.sibling
= sel
->win
;
1316 for(c
= nexttiled(clients
); c
; c
= nexttiled(c
->next
)) {
1319 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1320 wc
.sibling
= c
->win
;
1324 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1330 char sbuf
[sizeof stext
];
1333 unsigned int len
, offset
;
1336 /* main event loop, also reads status text from stdin */
1338 xfd
= ConnectionNumber(dpy
);
1341 len
= sizeof stext
- 1;
1342 sbuf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1346 FD_SET(STDIN_FILENO
, &rd
);
1348 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1351 eprint("select failed\n");
1353 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1354 switch((r
= read(STDIN_FILENO
, sbuf
+ offset
, len
- offset
))) {
1356 strncpy(stext
, strerror(errno
), len
);
1360 strncpy(stext
, "EOF", 4);
1364 for(p
= sbuf
+ offset
; r
> 0; p
++, r
--, offset
++)
1365 if(*p
== '\n' || *p
== '\0') {
1367 strncpy(stext
, sbuf
, len
);
1368 p
+= r
- 1; /* p is sbuf + offset + r - 1 */
1369 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1372 memmove(sbuf
, p
- r
+ 1, r
);
1379 while(XPending(dpy
)) {
1380 XNextEvent(dpy
, &ev
);
1381 if(handler
[ev
.type
])
1382 (handler
[ev
.type
])(&ev
); /* call handler */
1389 unsigned int i
, num
;
1390 Window
*wins
, d1
, d2
;
1391 XWindowAttributes wa
;
1394 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1395 for(i
= 0; i
< num
; i
++) {
1396 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1397 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1399 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1400 manage(wins
[i
], &wa
);
1402 for(i
= 0; i
< num
; i
++) { /* now the transients */
1403 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1405 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1406 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1407 manage(wins
[i
], &wa
);
1415 setclientstate(Client
*c
, long state
) {
1416 long data
[] = {state
, None
};
1418 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1419 PropModeReplace
, (unsigned char *)data
, 2);
1423 setgeom(const char *arg
) {
1427 if(++geom
== &geoms
[LENGTH(geoms
)])
1431 for(i
= 0; i
< LENGTH(geoms
); i
++)
1432 if(!strcmp(geoms
[i
].symbol
, arg
))
1434 if(i
== LENGTH(geoms
))
1444 setlayout(const char *arg
) {
1448 if(++lt
== &layouts
[LENGTH(layouts
)])
1452 for(i
= 0; i
< LENGTH(layouts
); i
++)
1453 if(!strcmp(arg
, layouts
[i
].symbol
))
1455 if(i
== LENGTH(layouts
))
1468 XSetWindowAttributes wa
;
1471 screen
= DefaultScreen(dpy
);
1472 root
= RootWindow(dpy
, screen
);
1475 /* apply default geometry */
1478 sw
= DisplayWidth(dpy
, screen
);
1479 sh
= DisplayHeight(dpy
, screen
);
1480 bh
= dc
.font
.height
+ 2;
1485 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1486 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1487 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1488 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1489 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1490 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1493 wa
.cursor
= cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1494 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1495 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1497 /* init appearance */
1498 dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
);
1499 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
1500 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
1501 dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
);
1502 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
1503 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
1506 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1507 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1508 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1510 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1513 seltags
= emallocz(TAGSZ
);
1514 prevtags
= emallocz(TAGSZ
);
1515 seltags
[0] = prevtags
[0] = True
;
1521 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1522 w
= textw(layouts
[i
].symbol
);
1526 for(bgw
= i
= 0; LENGTH(geoms
) > 1 && i
< LENGTH(geoms
); i
++) {
1527 w
= textw(geoms
[i
].symbol
);
1532 wa
.override_redirect
= 1;
1533 wa
.background_pixmap
= ParentRelative
;
1534 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1536 barwin
= XCreateWindow(dpy
, root
, bx
, by
, bw
, bh
, 0, DefaultDepth(dpy
, screen
),
1537 CopyFromParent
, DefaultVisual(dpy
, screen
),
1538 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1539 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
1540 XMapRaised(dpy
, barwin
);
1541 strcpy(stext
, "dwm-"VERSION
);
1544 /* EWMH support per view */
1545 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1546 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1548 /* select for events */
1549 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
1550 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
;
1551 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1552 XSelectInput(dpy
, root
, wa
.event_mask
);
1560 spawn(const char *arg
) {
1561 static char *shell
= NULL
;
1563 if(!shell
&& !(shell
= getenv("SHELL")))
1567 /* The double-fork construct avoids zombie processes and keeps the code
1568 * clean from stupid signal handlers. */
1572 close(ConnectionNumber(dpy
));
1574 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1575 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1584 tag(const char *arg
) {
1589 for(i
= 0; i
< LENGTH(tags
); i
++)
1590 sel
->tags
[i
] = (NULL
== arg
);
1591 sel
->tags
[idxoftag(arg
)] = True
;
1596 textnw(const char *text
, unsigned int len
) {
1600 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1603 return XTextWidth(dc
.font
.xfont
, text
, len
);
1607 textw(const char *text
) {
1608 return textnw(text
, strlen(text
)) + dc
.font
.height
;
1614 unsigned int i
, n
= counttiled();
1628 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1629 if(i
+ 1 == n
) /* remainder */
1630 tileresize(c
, x
, ty
, (tx
+ tw
) - x
- 2 * c
->bw
, th
- 2 * c
->bw
);
1632 tileresize(c
, x
, ty
, w
- 2 * c
->bw
, th
- 2 * c
->bw
);
1634 x
= c
->x
+ c
->w
+ 2 * c
->bw
;
1639 tilemaster(unsigned int n
) {
1640 Client
*c
= nexttiled(clients
);
1643 tileresize(c
, mox
, moy
, mow
- 2 * c
->bw
, moh
- 2 * c
->bw
);
1645 tileresize(c
, mx
, my
, mw
- 2 * c
->bw
, mh
- 2 * c
->bw
);
1650 tileresize(Client
*c
, int x
, int y
, int w
, int h
) {
1651 resize(c
, x
, y
, w
, h
, RESIZEHINTS
);
1652 if((RESIZEHINTS
) && ((c
->h
< bh
) || (c
->h
> h
) || (c
->w
< bh
) || (c
->w
> w
)))
1653 /* client doesn't accept size constraints */
1654 resize(c
, x
, y
, w
, h
, False
);
1660 unsigned int i
, n
= counttiled();
1674 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1675 if(i
+ 1 == n
) /* remainder */
1676 tileresize(c
, tx
, y
, tw
- 2 * c
->bw
, (ty
+ th
) - y
- 2 * c
->bw
);
1678 tileresize(c
, tx
, y
, tw
- 2 * c
->bw
, h
- 2 * c
->bw
);
1680 y
= c
->y
+ c
->h
+ 2 * c
->bw
;
1685 togglefloating(const char *arg
) {
1688 sel
->isfloating
= !sel
->isfloating
;
1690 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1695 toggletag(const char *arg
) {
1701 sel
->tags
[i
] = !sel
->tags
[i
];
1702 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1703 if(j
== LENGTH(tags
))
1704 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1709 toggleview(const char *arg
) {
1713 seltags
[i
] = !seltags
[i
];
1714 for(j
= 0; j
< LENGTH(tags
) && !seltags
[j
]; j
++);
1715 if(j
== LENGTH(tags
))
1716 seltags
[i
] = True
; /* at least one tag must be viewed */
1724 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1725 c
->isbanned
= False
;
1729 unmanage(Client
*c
) {
1732 wc
.border_width
= c
->oldbw
;
1733 /* The server grab construct avoids race conditions. */
1735 XSetErrorHandler(xerrordummy
);
1736 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1741 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1742 setclientstate(c
, WithdrawnState
);
1746 XSetErrorHandler(xerror
);
1752 unmapnotify(XEvent
*e
) {
1754 XUnmapEvent
*ev
= &e
->xunmap
;
1756 if((c
= getclient(ev
->window
)))
1761 updatebarpos(void) {
1763 if(dc
.drawable
!= 0)
1764 XFreePixmap(dpy
, dc
.drawable
);
1765 dc
.drawable
= XCreatePixmap(dpy
, root
, bw
, bh
, DefaultDepth(dpy
, screen
));
1766 XMoveResizeWindow(dpy
, barwin
, bx
, by
, bw
, bh
);
1770 updatesizehints(Client
*c
) {
1774 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1776 c
->flags
= size
.flags
;
1777 if(c
->flags
& PBaseSize
) {
1778 c
->basew
= size
.base_width
;
1779 c
->baseh
= size
.base_height
;
1781 else if(c
->flags
& PMinSize
) {
1782 c
->basew
= size
.min_width
;
1783 c
->baseh
= size
.min_height
;
1786 c
->basew
= c
->baseh
= 0;
1787 if(c
->flags
& PResizeInc
) {
1788 c
->incw
= size
.width_inc
;
1789 c
->inch
= size
.height_inc
;
1792 c
->incw
= c
->inch
= 0;
1793 if(c
->flags
& PMaxSize
) {
1794 c
->maxw
= size
.max_width
;
1795 c
->maxh
= size
.max_height
;
1798 c
->maxw
= c
->maxh
= 0;
1799 if(c
->flags
& PMinSize
) {
1800 c
->minw
= size
.min_width
;
1801 c
->minh
= size
.min_height
;
1803 else if(c
->flags
& PBaseSize
) {
1804 c
->minw
= size
.base_width
;
1805 c
->minh
= size
.base_height
;
1808 c
->minw
= c
->minh
= 0;
1809 if(c
->flags
& PAspect
) {
1810 c
->minax
= size
.min_aspect
.x
;
1811 c
->maxax
= size
.max_aspect
.x
;
1812 c
->minay
= size
.min_aspect
.y
;
1813 c
->maxay
= size
.max_aspect
.y
;
1816 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1817 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1818 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1822 updatetitle(Client
*c
) {
1823 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1824 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1828 updatewmhints(Client
*c
) {
1831 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1833 sel
->isurgent
= False
;
1835 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1842 view(const char *arg
) {
1845 for(i
= 0; i
< LENGTH(tags
); i
++)
1846 tmp
[i
] = (NULL
== arg
);
1847 tmp
[idxoftag(arg
)] = True
;
1849 if(memcmp(seltags
, tmp
, TAGSZ
) != 0) {
1850 memcpy(prevtags
, seltags
, TAGSZ
);
1851 memcpy(seltags
, tmp
, TAGSZ
);
1857 viewprevtag(const char *arg
) {
1859 memcpy(tmp
, seltags
, TAGSZ
);
1860 memcpy(seltags
, prevtags
, TAGSZ
);
1861 memcpy(prevtags
, tmp
, TAGSZ
);
1865 /* There's no way to check accesses to destroyed windows, thus those cases are
1866 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1867 * default error handler, which may call exit. */
1869 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1870 if(ee
->error_code
== BadWindow
1871 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1872 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1873 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1874 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1875 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1876 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1877 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1879 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1880 ee
->request_code
, ee
->error_code
);
1881 return xerrorxlib(dpy
, ee
); /* may call exit */
1885 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1889 /* Startup Error handler to check if another window manager
1890 * is already running. */
1892 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1898 zoom(const char *arg
) {
1901 if(!sel
|| lt
->isfloating
|| sel
->isfloating
)
1903 if(c
== nexttiled(clients
))
1904 if(!(c
= nexttiled(c
->next
)))
1913 main(int argc
, char *argv
[]) {
1914 if(argc
== 2 && !strcmp("-v", argv
[1]))
1915 eprint("dwm-"VERSION
", © 2006-2008 dwm engineers, see LICENSE for details\n");
1917 eprint("usage: dwm [-v]\n");
1919 setlocale(LC_CTYPE
, "");
1920 if(!(dpy
= XOpenDisplay(0)))
1921 eprint("dwm: cannot open display\n");