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>
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 MAX(a, b) ((a) > (b) ? (a) : (b))
45 #define MIN(a, b) ((a) < (b) ? (a) : (b))
46 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
47 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
48 #define LENGTH(x) (sizeof x / sizeof x[0])
50 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
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 basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
64 int minax
, maxax
, minay
, maxay
;
66 unsigned int bw
, oldbw
;
67 Bool isbanned
, isfixed
, isfloating
, isurgent
;
77 unsigned long norm
[ColLast
];
78 unsigned long sel
[ColLast
];
88 } DC
; /* draw context */
93 void (*func
)(const char *arg
);
99 void (*arrange
)(void);
100 void (*updategeom
)(void);
105 const char *instance
;
111 /* function declarations */
112 void applyrules(Client
*c
);
114 void attach(Client
*c
);
115 void attachstack(Client
*c
);
117 void buttonpress(XEvent
*e
);
118 void checkotherwm(void);
120 void configure(Client
*c
);
121 void configurenotify(XEvent
*e
);
122 void configurerequest(XEvent
*e
);
123 void destroynotify(XEvent
*e
);
124 void detach(Client
*c
);
125 void detachstack(Client
*c
);
127 void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
128 void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
129 void *emallocz(unsigned int size
);
130 void enternotify(XEvent
*e
);
131 void eprint(const char *errstr
, ...);
132 void expose(XEvent
*e
);
133 void focus(Client
*c
);
134 void focusin(XEvent
*e
);
135 void focusnext(const char *arg
);
136 void focusprev(const char *arg
);
137 Client
*getclient(Window w
);
138 unsigned long getcolor(const char *colstr
);
139 long getstate(Window w
);
140 Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
141 void grabbuttons(Client
*c
, Bool focused
);
143 unsigned int idxoftag(const char *t
);
144 void initfont(const char *fontstr
);
145 Bool
isoccupied(unsigned int t
);
146 Bool
isprotodel(Client
*c
);
147 Bool
isurgent(unsigned int t
);
148 Bool
isvisible(Client
*c
);
149 void keypress(XEvent
*e
);
150 void killclient(const char *arg
);
151 void manage(Window w
, XWindowAttributes
*wa
);
152 void mappingnotify(XEvent
*e
);
153 void maprequest(XEvent
*e
);
154 void movemouse(Client
*c
);
155 Client
*nextunfloating(Client
*c
);
156 void propertynotify(XEvent
*e
);
157 void quit(const char *arg
);
158 void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
);
159 void resizemouse(Client
*c
);
163 void setclientstate(Client
*c
, long state
);
165 void spawn(const char *arg
);
166 void tag(const char *arg
);
167 unsigned int textnw(const char *text
, unsigned int len
);
168 unsigned int textw(const char *text
);
169 void togglefloating(const char *arg
);
170 void togglelayout(const char *arg
);
171 void toggletag(const char *arg
);
172 void toggleview(const char *arg
);
173 void unban(Client
*c
);
174 void unmanage(Client
*c
);
175 void unmapnotify(XEvent
*e
);
176 void updatebar(void);
177 void updategeom(void);
178 void updatesizehints(Client
*c
);
179 void updatetitle(Client
*c
);
180 void updatewmhints(Client
*c
);
181 void view(const char *arg
);
182 void viewprevtag(const char *arg
); /* views previous selected tags */
183 int xerror(Display
*dpy
, XErrorEvent
*ee
);
184 int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
185 int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
186 void zoom(const char *arg
);
190 int screen
, sx
, sy
, sw
, sh
;
191 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
192 int bx
, by
, bw
, bh
, blw
, wx
, wy
, ww
, wh
;
194 unsigned int numlockmask
= 0;
195 void (*handler
[LASTEvent
]) (XEvent
*) = {
196 [ButtonPress
] = buttonpress
,
197 [ConfigureRequest
] = configurerequest
,
198 [ConfigureNotify
] = configurenotify
,
199 [DestroyNotify
] = destroynotify
,
200 [EnterNotify
] = enternotify
,
203 [KeyPress
] = keypress
,
204 [MappingNotify
] = mappingnotify
,
205 [MapRequest
] = maprequest
,
206 [PropertyNotify
] = propertynotify
,
207 [UnmapNotify
] = unmapnotify
209 Atom wmatom
[WMLast
], netatom
[NetLast
];
210 Bool otherwm
, readin
;
213 Client
*clients
= NULL
;
215 Client
*stack
= NULL
;
216 Cursor cursor
[CurLast
];
220 Layout
*lt
= layouts
;
223 /* configuration, allows nested code to access above variables */
225 #define TAGSZ (LENGTH(tags) * sizeof(Bool))
227 /* function implementations */
230 applyrules(Client
*c
) {
232 Bool matched
= False
;
234 XClassHint ch
= { 0 };
237 XGetClassHint(dpy
, c
->win
, &ch
);
238 for(i
= 0; i
< LENGTH(rules
); i
++) {
240 if((!r
->title
|| strstr(c
->name
, r
->title
))
241 && (!r
->class || (ch
.res_class
&& strstr(ch
.res_class
, r
->class)))
242 && (!r
->instance
|| (ch
.res_name
&& strstr(ch
.res_name
, r
->instance
)))) {
243 c
->isfloating
= r
->isfloating
;
245 c
->tags
[idxoftag(r
->tag
)] = True
;
255 memcpy(c
->tags
, tagset
[seltags
], TAGSZ
);
262 for(c
= clients
; c
; c
= c
->next
)
265 if(!lt
->arrange
|| c
->isfloating
)
266 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
286 attachstack(Client
*c
) {
295 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
300 buttonpress(XEvent
*e
) {
303 XButtonPressedEvent
*ev
= &e
->xbutton
;
305 if(ev
->window
== barwin
) {
307 for(i
= 0; i
< LENGTH(tags
); i
++) {
310 if(ev
->button
== Button1
) {
311 if(ev
->state
& MODKEY
)
316 else if(ev
->button
== Button3
) {
317 if(ev
->state
& MODKEY
)
325 if((ev
->x
< x
+ blw
) && ev
->button
== Button1
)
328 else if((c
= getclient(ev
->window
))) {
330 if(CLEANMASK(ev
->state
) != MODKEY
)
332 if(ev
->button
== Button1
) {
336 else if(ev
->button
== Button2
) {
337 if(lt
->arrange
&& c
->isfloating
)
338 togglefloating(NULL
);
340 else if(ev
->button
== Button3
&& !c
->isfixed
) {
350 XSetErrorHandler(xerrorstart
);
352 /* this causes an error if some other window manager is running */
353 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
356 eprint("dwm: another window manager is already running\n");
358 XSetErrorHandler(NULL
);
359 xerrorxlib
= XSetErrorHandler(xerror
);
371 XFreeFontSet(dpy
, dc
.font
.set
);
373 XFreeFont(dpy
, dc
.font
.xfont
);
374 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
375 XFreePixmap(dpy
, dc
.drawable
);
377 XFreeCursor(dpy
, cursor
[CurNormal
]);
378 XFreeCursor(dpy
, cursor
[CurResize
]);
379 XFreeCursor(dpy
, cursor
[CurMove
]);
380 XDestroyWindow(dpy
, barwin
);
382 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
386 configure(Client
*c
) {
389 ce
.type
= ConfigureNotify
;
397 ce
.border_width
= c
->bw
;
399 ce
.override_redirect
= False
;
400 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
404 configurenotify(XEvent
*e
) {
405 XConfigureEvent
*ev
= &e
->xconfigure
;
407 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
417 configurerequest(XEvent
*e
) {
419 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
422 if((c
= getclient(ev
->window
))) {
423 if(ev
->value_mask
& CWBorderWidth
)
424 c
->bw
= ev
->border_width
;
425 if(c
->isfixed
|| c
->isfloating
|| !lt
->arrange
) {
426 if(ev
->value_mask
& CWX
)
428 if(ev
->value_mask
& CWY
)
430 if(ev
->value_mask
& CWWidth
)
432 if(ev
->value_mask
& CWHeight
)
434 if((c
->x
- sx
+ c
->w
) > sw
&& c
->isfloating
)
435 c
->x
= sx
+ (sw
/ 2 - c
->w
/ 2); /* center in x direction */
436 if((c
->y
- sy
+ c
->h
) > sh
&& c
->isfloating
)
437 c
->y
= sy
+ (sh
/ 2 - c
->h
/ 2); /* center in y direction */
438 if((ev
->value_mask
& (CWX
|CWY
))
439 && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
442 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
450 wc
.width
= ev
->width
;
451 wc
.height
= ev
->height
;
452 wc
.border_width
= ev
->border_width
;
453 wc
.sibling
= ev
->above
;
454 wc
.stack_mode
= ev
->detail
;
455 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
461 destroynotify(XEvent
*e
) {
463 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
465 if((c
= getclient(ev
->window
)))
472 c
->prev
->next
= c
->next
;
474 c
->next
->prev
= c
->prev
;
477 c
->next
= c
->prev
= NULL
;
481 detachstack(Client
*c
) {
484 for(tc
= &stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
494 for(c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
495 for(i
= 0; i
< LENGTH(tags
); i
++) {
496 dc
.w
= textw(tags
[i
]);
497 if(tagset
[seltags
][i
]) {
498 drawtext(tags
[i
], dc
.sel
, isurgent(i
));
499 drawsquare(c
&& c
->tags
[i
], isoccupied(i
), isurgent(i
), dc
.sel
);
502 drawtext(tags
[i
], dc
.norm
, isurgent(i
));
503 drawsquare(c
&& c
->tags
[i
], isoccupied(i
), isurgent(i
), dc
.norm
);
509 drawtext(lt
->symbol
, dc
.norm
, False
);
520 drawtext(stext
, dc
.norm
, False
);
521 if((dc
.w
= dc
.x
- x
) > bh
) {
524 drawtext(c
->name
, dc
.sel
, False
);
525 drawsquare(False
, c
->isfloating
, False
, dc
.sel
);
528 drawtext(NULL
, dc
.norm
, False
);
530 XCopyArea(dpy
, dc
.drawable
, barwin
, dc
.gc
, 0, 0, bw
, bh
, 0, 0);
535 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
538 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
540 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
541 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
542 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
546 r
.width
= r
.height
= x
+ 1;
547 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
550 r
.width
= r
.height
= x
;
551 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
556 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
558 unsigned int len
, olen
;
559 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
562 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
563 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
567 len
= MIN(olen
, sizeof buf
);
568 memcpy(buf
, text
, len
);
570 h
= dc
.font
.ascent
+ dc
.font
.descent
;
571 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
573 /* shorten text if necessary */
574 for(; len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
; len
--);
585 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
587 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
589 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
593 emallocz(unsigned int size
) {
594 void *res
= calloc(1, size
);
597 eprint("fatal: could not malloc() %u bytes\n", size
);
602 enternotify(XEvent
*e
) {
604 XCrossingEvent
*ev
= &e
->xcrossing
;
606 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
608 if((c
= getclient(ev
->window
)))
615 eprint(const char *errstr
, ...) {
618 va_start(ap
, errstr
);
619 vfprintf(stderr
, errstr
, ap
);
626 XExposeEvent
*ev
= &e
->xexpose
;
628 if(ev
->count
== 0 && (ev
->window
== barwin
))
634 if(!c
|| (c
&& !isvisible(c
)))
635 for(c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
636 if(sel
&& sel
!= c
) {
637 grabbuttons(sel
, False
);
638 XSetWindowBorder(dpy
, sel
->win
, dc
.norm
[ColBorder
]);
643 grabbuttons(c
, True
);
647 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
648 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
651 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
656 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
657 XFocusChangeEvent
*ev
= &e
->xfocus
;
659 if(sel
&& ev
->window
!= sel
->win
)
660 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
664 focusnext(const char *arg
) {
669 for(c
= sel
->next
; c
&& !isvisible(c
); c
= c
->next
);
671 for(c
= clients
; c
&& !isvisible(c
); c
= c
->next
);
679 focusprev(const char *arg
) {
684 for(c
= sel
->prev
; c
&& !isvisible(c
); c
= c
->prev
);
686 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
687 for(; c
&& !isvisible(c
); c
= c
->prev
);
696 getclient(Window w
) {
699 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
704 getcolor(const char *colstr
) {
705 Colormap cmap
= DefaultColormap(dpy
, screen
);
708 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
709 eprint("error, cannot allocate color '%s'\n", colstr
);
717 unsigned char *p
= NULL
;
718 unsigned long n
, extra
;
721 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
722 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
723 if(status
!= Success
)
732 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
737 if(!text
|| size
== 0)
740 XGetTextProperty(dpy
, w
, &name
, atom
);
743 if(name
.encoding
== XA_STRING
)
744 strncpy(text
, (char *)name
.value
, size
- 1);
746 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
748 strncpy(text
, *list
, size
- 1);
749 XFreeStringList(list
);
752 text
[size
- 1] = '\0';
758 grabbuttons(Client
*c
, Bool focused
) {
760 unsigned int buttons
[] = { Button1
, Button2
, Button3
};
761 unsigned int modifiers
[] = { MODKEY
, MODKEY
|LockMask
, MODKEY
|numlockmask
,
762 MODKEY
|numlockmask
|LockMask
} ;
764 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
766 for(i
= 0; i
< LENGTH(buttons
); i
++)
767 for(j
= 0; j
< LENGTH(modifiers
); j
++)
768 XGrabButton(dpy
, buttons
[i
], modifiers
[j
], c
->win
, False
,
769 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
771 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
772 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
779 XModifierKeymap
*modmap
;
781 /* init modifier map */
782 modmap
= XGetModifierMapping(dpy
);
783 for(i
= 0; i
< 8; i
++)
784 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
785 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
786 numlockmask
= (1 << i
);
788 XFreeModifiermap(modmap
);
790 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
791 for(i
= 0; i
< LENGTH(keys
); i
++) {
792 code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
);
793 XGrabKey(dpy
, code
, keys
[i
].mod
, root
, True
,
794 GrabModeAsync
, GrabModeAsync
);
795 XGrabKey(dpy
, code
, keys
[i
].mod
|LockMask
, root
, True
,
796 GrabModeAsync
, GrabModeAsync
);
797 XGrabKey(dpy
, code
, keys
[i
].mod
|numlockmask
, root
, True
,
798 GrabModeAsync
, GrabModeAsync
);
799 XGrabKey(dpy
, code
, keys
[i
].mod
|numlockmask
|LockMask
, root
, True
,
800 GrabModeAsync
, GrabModeAsync
);
805 idxoftag(const char *t
) {
808 for(i
= 0; (i
< LENGTH(tags
)) && t
&& strcmp(tags
[i
], t
); i
++);
809 return (i
< LENGTH(tags
)) ? i
: 0;
813 initfont(const char *fontstr
) {
814 char *def
, **missing
;
819 XFreeFontSet(dpy
, dc
.font
.set
);
820 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
823 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
824 XFreeStringList(missing
);
827 XFontSetExtents
*font_extents
;
828 XFontStruct
**xfonts
;
830 dc
.font
.ascent
= dc
.font
.descent
= 0;
831 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
832 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
833 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
834 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
835 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
841 XFreeFont(dpy
, dc
.font
.xfont
);
842 dc
.font
.xfont
= NULL
;
843 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
844 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
845 eprint("error, cannot load font: '%s'\n", fontstr
);
846 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
847 dc
.font
.descent
= dc
.font
.xfont
->descent
;
849 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
853 isoccupied(unsigned int t
) {
856 for(c
= clients
; c
; c
= c
->next
)
863 isprotodel(Client
*c
) {
868 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
869 for(i
= 0; !ret
&& i
< n
; i
++)
870 if(protocols
[i
] == wmatom
[WMDelete
])
878 isurgent(unsigned int t
) {
881 for(c
= clients
; c
; c
= c
->next
)
882 if(c
->isurgent
&& c
->tags
[t
])
888 isvisible(Client
*c
) {
891 for(i
= 0; i
< LENGTH(tags
); i
++)
892 if(c
->tags
[i
] && tagset
[seltags
][i
])
898 keypress(XEvent
*e
) {
904 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
905 for(i
= 0; i
< LENGTH(keys
); i
++)
906 if(keysym
== keys
[i
].keysym
907 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
910 keys
[i
].func(keys
[i
].arg
);
915 killclient(const char *arg
) {
920 if(isprotodel(sel
)) {
921 ev
.type
= ClientMessage
;
922 ev
.xclient
.window
= sel
->win
;
923 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
924 ev
.xclient
.format
= 32;
925 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
926 ev
.xclient
.data
.l
[1] = CurrentTime
;
927 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
930 XKillClient(dpy
, sel
->win
);
934 manage(Window w
, XWindowAttributes
*wa
) {
935 Client
*c
, *t
= NULL
;
940 c
= emallocz(sizeof(Client
));
941 c
->tags
= emallocz(TAGSZ
);
949 c
->oldbw
= wa
->border_width
;
950 if(c
->w
== sw
&& c
->h
== sh
) {
953 c
->bw
= wa
->border_width
;
956 if(c
->x
+ c
->w
+ 2 * c
->bw
> wx
+ ww
)
957 c
->x
= wx
+ ww
- c
->w
- 2 * c
->bw
;
958 if(c
->y
+ c
->h
+ 2 * c
->bw
> wy
+ wh
)
959 c
->y
= wy
+ wh
- c
->h
- 2 * c
->bw
;
960 c
->x
= MAX(c
->x
, wx
);
961 c
->y
= MAX(c
->y
, wy
);
965 wc
.border_width
= c
->bw
;
966 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
967 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
968 configure(c
); /* propagates border_width, if size doesn't change */
970 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
971 grabbuttons(c
, False
);
973 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
974 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
976 memcpy(c
->tags
, t
->tags
, TAGSZ
);
980 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
983 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
985 XMapWindow(dpy
, c
->win
);
986 setclientstate(c
, NormalState
);
991 mappingnotify(XEvent
*e
) {
992 XMappingEvent
*ev
= &e
->xmapping
;
994 XRefreshKeyboardMapping(ev
);
995 if(ev
->request
== MappingKeyboard
)
1000 maprequest(XEvent
*e
) {
1001 static XWindowAttributes wa
;
1002 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1004 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1006 if(wa
.override_redirect
)
1008 if(!getclient(ev
->window
))
1009 manage(ev
->window
, &wa
);
1013 movemouse(Client
*c
) {
1014 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1021 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1022 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1024 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1026 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1029 XUngrabPointer(dpy
, CurrentTime
);
1031 case ConfigureRequest
:
1034 handler
[ev
.type
](&ev
);
1038 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1039 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1040 if(abs(wx
- nx
) < SNAP
)
1042 else if(abs((wx
+ ww
) - (nx
+ c
->w
+ 2 * c
->bw
)) < SNAP
)
1043 nx
= wx
+ ww
- c
->w
- 2 * c
->bw
;
1044 if(abs(wy
- ny
) < SNAP
)
1046 else if(abs((wy
+ wh
) - (ny
+ c
->h
+ 2 * c
->bw
)) < SNAP
)
1047 ny
= wy
+ wh
- c
->h
- 2 * c
->bw
;
1048 if(!c
->isfloating
&& lt
->arrange
&& (abs(nx
- c
->x
) > SNAP
|| abs(ny
- c
->y
) > SNAP
))
1049 togglefloating(NULL
);
1050 if(!lt
->arrange
|| c
->isfloating
)
1051 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1058 nextunfloating(Client
*c
) {
1059 for(; c
&& (c
->isfloating
|| !isvisible(c
)); c
= c
->next
);
1064 propertynotify(XEvent
*e
) {
1067 XPropertyEvent
*ev
= &e
->xproperty
;
1069 if(ev
->state
== PropertyDelete
)
1070 return; /* ignore */
1071 if((c
= getclient(ev
->window
))) {
1074 case XA_WM_TRANSIENT_FOR
:
1075 XGetTransientForHint(dpy
, c
->win
, &trans
);
1076 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1079 case XA_WM_NORMAL_HINTS
:
1087 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1096 quit(const char *arg
) {
1097 readin
= running
= False
;
1101 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1105 /* set minimum possible */
1109 /* temporarily remove base dimensions */
1113 /* adjust for aspect limits */
1114 if(c
->minax
!= c
->maxax
&& c
->minay
!= c
->maxay
1115 && c
->minax
> 0 && c
->maxax
> 0 && c
->minay
> 0 && c
->maxay
> 0) {
1116 if(w
* c
->maxay
> h
* c
->maxax
)
1117 w
= h
* c
->maxax
/ c
->maxay
;
1118 else if(w
* c
->minay
< h
* c
->minax
)
1119 h
= w
* c
->minay
/ c
->minax
;
1122 /* adjust for increment value */
1128 /* restore base dimensions */
1132 w
= MAX(w
, c
->minw
);
1133 h
= MAX(h
, c
->minh
);
1136 w
= MIN(w
, c
->maxw
);
1139 h
= MIN(h
, c
->maxh
);
1141 if(w
<= 0 || h
<= 0)
1144 x
= sw
- w
- 2 * c
->bw
;
1146 y
= sh
- h
- 2 * c
->bw
;
1147 if(x
+ w
+ 2 * c
->bw
< sx
)
1149 if(y
+ h
+ 2 * c
->bw
< sy
)
1151 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1154 c
->w
= wc
.width
= w
;
1155 c
->h
= wc
.height
= h
;
1156 wc
.border_width
= c
->bw
;
1157 XConfigureWindow(dpy
, c
->win
,
1158 CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1165 resizemouse(Client
*c
) {
1172 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1173 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1175 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1177 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1180 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1181 c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1182 XUngrabPointer(dpy
, CurrentTime
);
1183 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1185 case ConfigureRequest
:
1188 handler
[ev
.type
](&ev
);
1192 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1193 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1194 if(!c
->isfloating
&& lt
->arrange
&& (abs(nw
- c
->w
) > SNAP
|| abs(nh
- c
->h
) > SNAP
)) {
1195 togglefloating(NULL
);
1197 if(!lt
->arrange
|| c
->isfloating
)
1198 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1213 if(sel
->isfloating
|| !lt
->arrange
)
1214 XRaiseWindow(dpy
, sel
->win
);
1216 wc
.stack_mode
= Below
;
1217 wc
.sibling
= barwin
;
1218 for(c
= stack
; c
; c
= c
->snext
)
1219 if(!c
->isfloating
&& isvisible(c
)) {
1220 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1221 wc
.sibling
= c
->win
;
1225 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1231 char sbuf
[sizeof stext
];
1234 unsigned int len
, offset
;
1237 /* main event loop, also reads status text from stdin */
1239 xfd
= ConnectionNumber(dpy
);
1242 len
= sizeof stext
- 1;
1243 sbuf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1247 FD_SET(STDIN_FILENO
, &rd
);
1249 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1252 eprint("select failed\n");
1254 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1255 switch((r
= read(STDIN_FILENO
, sbuf
+ offset
, len
- offset
))) {
1257 strncpy(stext
, strerror(errno
), len
);
1261 strncpy(stext
, "EOF", 4);
1265 for(p
= sbuf
+ offset
; r
> 0; p
++, r
--, offset
++)
1266 if(*p
== '\n' || *p
== '\0') {
1268 strncpy(stext
, sbuf
, len
);
1269 p
+= r
- 1; /* p is sbuf + offset + r - 1 */
1270 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1273 memmove(sbuf
, p
- r
+ 1, r
);
1280 while(XPending(dpy
)) {
1281 XNextEvent(dpy
, &ev
);
1282 if(handler
[ev
.type
])
1283 (handler
[ev
.type
])(&ev
); /* call handler */
1290 unsigned int i
, num
;
1291 Window
*wins
, d1
, d2
;
1292 XWindowAttributes wa
;
1295 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1296 for(i
= 0; i
< num
; i
++) {
1297 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1298 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1300 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1301 manage(wins
[i
], &wa
);
1303 for(i
= 0; i
< num
; i
++) { /* now the transients */
1304 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1306 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1307 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1308 manage(wins
[i
], &wa
);
1316 setclientstate(Client
*c
, long state
) {
1317 long data
[] = {state
, None
};
1319 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1320 PropModeReplace
, (unsigned char *)data
, 2);
1326 XSetWindowAttributes wa
;
1329 screen
= DefaultScreen(dpy
);
1330 root
= RootWindow(dpy
, screen
);
1334 sw
= DisplayWidth(dpy
, screen
);
1335 sh
= DisplayHeight(dpy
, screen
);
1336 bh
= dc
.font
.height
+ 2;
1340 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1341 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1342 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1343 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1344 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1345 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1348 wa
.cursor
= cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1349 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1350 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1352 /* init appearance */
1353 dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
);
1354 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
1355 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
1356 dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
);
1357 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
1358 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
1361 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1362 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1363 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1365 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1368 tagset
[0] = emallocz(TAGSZ
);
1369 tagset
[1] = emallocz(TAGSZ
);
1370 tagset
[0][0] = tagset
[1][0] = True
;
1373 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1374 w
= textw(layouts
[i
].symbol
);
1378 wa
.override_redirect
= 1;
1379 wa
.background_pixmap
= ParentRelative
;
1380 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1382 barwin
= XCreateWindow(dpy
, root
, bx
, by
, bw
, bh
, 0, DefaultDepth(dpy
, screen
),
1383 CopyFromParent
, DefaultVisual(dpy
, screen
),
1384 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1385 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
1386 XMapRaised(dpy
, barwin
);
1387 strcpy(stext
, "dwm-"VERSION
);
1390 /* EWMH support per view */
1391 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1392 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1394 /* select for events */
1395 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
1396 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
;
1397 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1398 XSelectInput(dpy
, root
, wa
.event_mask
);
1406 spawn(const char *arg
) {
1407 static char *shell
= NULL
;
1409 if(!shell
&& !(shell
= getenv("SHELL")))
1413 /* The double-fork construct avoids zombie processes and keeps the code
1414 * clean from stupid signal handlers. */
1418 close(ConnectionNumber(dpy
));
1420 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1421 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1430 tag(const char *arg
) {
1435 for(i
= 0; i
< LENGTH(tags
); i
++)
1436 sel
->tags
[i
] = (arg
== NULL
);
1437 sel
->tags
[idxoftag(arg
)] = True
;
1442 textnw(const char *text
, unsigned int len
) {
1446 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1449 return XTextWidth(dc
.font
.xfont
, text
, len
);
1453 textw(const char *text
) {
1454 return textnw(text
, strlen(text
)) + dc
.font
.height
;
1458 togglefloating(const char *arg
) {
1461 sel
->isfloating
= !sel
->isfloating
;
1463 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1468 togglelayout(const char *arg
) {
1472 if(++lt
== &layouts
[LENGTH(layouts
)])
1476 for(i
= 0; i
< LENGTH(layouts
); i
++)
1477 if(!strcmp(arg
, layouts
[i
].symbol
))
1479 if(i
== LENGTH(layouts
))
1490 toggletag(const char *arg
) {
1496 sel
->tags
[i
] = !sel
->tags
[i
];
1497 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1498 if(j
== LENGTH(tags
))
1499 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1504 toggleview(const char *arg
) {
1508 tagset
[seltags
][i
] = !tagset
[seltags
][i
];
1509 for(j
= 0; j
< LENGTH(tags
) && !tagset
[seltags
][j
]; j
++);
1510 if(j
== LENGTH(tags
))
1511 tagset
[seltags
][i
] = True
; /* at least one tag must be viewed */
1519 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1520 c
->isbanned
= False
;
1524 unmanage(Client
*c
) {
1527 wc
.border_width
= c
->oldbw
;
1528 /* The server grab construct avoids race conditions. */
1530 XSetErrorHandler(xerrordummy
);
1531 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1536 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1537 setclientstate(c
, WithdrawnState
);
1541 XSetErrorHandler(xerror
);
1547 unmapnotify(XEvent
*e
) {
1549 XUnmapEvent
*ev
= &e
->xunmap
;
1551 if((c
= getclient(ev
->window
)))
1557 if(dc
.drawable
!= 0)
1558 XFreePixmap(dpy
, dc
.drawable
);
1559 dc
.drawable
= XCreatePixmap(dpy
, root
, bw
, bh
, DefaultDepth(dpy
, screen
));
1560 XMoveResizeWindow(dpy
, barwin
, bx
, by
, bw
, bh
);
1572 /* window area geometry */
1578 /* update layout geometries */
1579 for(i
= 0; i
< LENGTH(layouts
); i
++)
1580 if(layouts
[i
].updategeom
)
1581 layouts
[i
].updategeom();
1585 updatesizehints(Client
*c
) {
1589 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1591 c
->flags
= size
.flags
;
1592 if(c
->flags
& PBaseSize
) {
1593 c
->basew
= size
.base_width
;
1594 c
->baseh
= size
.base_height
;
1596 else if(c
->flags
& PMinSize
) {
1597 c
->basew
= size
.min_width
;
1598 c
->baseh
= size
.min_height
;
1601 c
->basew
= c
->baseh
= 0;
1602 if(c
->flags
& PResizeInc
) {
1603 c
->incw
= size
.width_inc
;
1604 c
->inch
= size
.height_inc
;
1607 c
->incw
= c
->inch
= 0;
1608 if(c
->flags
& PMaxSize
) {
1609 c
->maxw
= size
.max_width
;
1610 c
->maxh
= size
.max_height
;
1613 c
->maxw
= c
->maxh
= 0;
1614 if(c
->flags
& PMinSize
) {
1615 c
->minw
= size
.min_width
;
1616 c
->minh
= size
.min_height
;
1618 else if(c
->flags
& PBaseSize
) {
1619 c
->minw
= size
.base_width
;
1620 c
->minh
= size
.base_height
;
1623 c
->minw
= c
->minh
= 0;
1624 if(c
->flags
& PAspect
) {
1625 c
->minax
= size
.min_aspect
.x
;
1626 c
->maxax
= size
.max_aspect
.x
;
1627 c
->minay
= size
.min_aspect
.y
;
1628 c
->maxay
= size
.max_aspect
.y
;
1631 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1632 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1633 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1637 updatetitle(Client
*c
) {
1638 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1639 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1643 updatewmhints(Client
*c
) {
1646 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1648 sel
->isurgent
= False
;
1650 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1656 view(const char *arg
) {
1657 seltags
^= 1; /* toggle sel tagset */
1658 memset(tagset
[seltags
], (NULL
== arg
), TAGSZ
);
1659 tagset
[seltags
][idxoftag(arg
)] = True
;
1664 viewprevtag(const char *arg
) {
1665 seltags
^= 1; /* toggle sel tagset */
1669 /* There's no way to check accesses to destroyed windows, thus those cases are
1670 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1671 * default error handler, which may call exit. */
1673 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1674 if(ee
->error_code
== BadWindow
1675 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1676 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1677 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1678 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1679 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1680 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1681 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1682 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1684 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1685 ee
->request_code
, ee
->error_code
);
1686 return xerrorxlib(dpy
, ee
); /* may call exit */
1690 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1694 /* Startup Error handler to check if another window manager
1695 * is already running. */
1697 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1703 main(int argc
, char *argv
[]) {
1704 if(argc
== 2 && !strcmp("-v", argv
[1]))
1705 eprint("dwm-"VERSION
", © 2006-2008 dwm engineers, see LICENSE for details\n");
1707 eprint("usage: dwm [-v]\n");
1709 setlocale(LC_CTYPE
, "");
1710 if(!(dpy
= XOpenDisplay(0)))
1711 eprint("dwm: cannot open display\n");