Xinqi Bao's Git
3dc6f8423b145c0a9e45a4ce926993dcec16567c
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
);
155 void movemouse(Client
*c
);
156 Client
*nextunfloating(Client
*c
);
157 void propertynotify(XEvent
*e
);
158 void quit(const char *arg
);
159 void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
);
160 void resizemouse(Client
*c
);
164 void setclientstate(Client
*c
, long state
);
166 void spawn(const char *arg
);
167 void tag(const char *arg
);
168 unsigned int textnw(const char *text
, unsigned int len
);
169 unsigned int textw(const char *text
);
170 void togglefloating(const char *arg
);
171 void togglelayout(const char *arg
);
172 void toggletag(const char *arg
);
173 void toggleview(const char *arg
);
174 void unban(Client
*c
);
175 void unmanage(Client
*c
);
176 void unmapnotify(XEvent
*e
);
177 void updatebar(void);
178 void updategeom(void);
179 void updatesizehints(Client
*c
);
180 void updatetitle(Client
*c
);
181 void updatewmhints(Client
*c
);
182 void view(const char *arg
);
183 void viewprevtag(const char *arg
); /* views previous selected tags */
184 int xerror(Display
*dpy
, XErrorEvent
*ee
);
185 int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
186 int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
187 void zoom(const char *arg
);
191 int screen
, sx
, sy
, sw
, sh
;
192 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
193 int bx
, by
, bw
, bh
, blw
, wx
, wy
, ww
, wh
;
195 unsigned int numlockmask
= 0;
196 void (*handler
[LASTEvent
]) (XEvent
*) = {
197 [ButtonPress
] = buttonpress
,
198 [ConfigureRequest
] = configurerequest
,
199 [ConfigureNotify
] = configurenotify
,
200 [DestroyNotify
] = destroynotify
,
201 [EnterNotify
] = enternotify
,
204 [KeyPress
] = keypress
,
205 [MappingNotify
] = mappingnotify
,
206 [MapRequest
] = maprequest
,
207 [PropertyNotify
] = propertynotify
,
208 [UnmapNotify
] = unmapnotify
210 Atom wmatom
[WMLast
], netatom
[NetLast
];
211 Bool otherwm
, readin
;
214 Client
*clients
= NULL
;
216 Client
*stack
= NULL
;
217 Cursor cursor
[CurLast
];
221 Layout
*lt
= layouts
;
224 /* configuration, allows nested code to access above variables */
226 #define TAGSZ (LENGTH(tags) * sizeof(Bool))
228 /* function implementations */
231 applyrules(Client
*c
) {
233 Bool matched
= False
;
235 XClassHint ch
= { 0 };
238 XGetClassHint(dpy
, c
->win
, &ch
);
239 for(i
= 0; i
< LENGTH(rules
); i
++) {
241 if((!r
->title
|| strstr(c
->name
, r
->title
))
242 && (!r
->class || (ch
.res_class
&& strstr(ch
.res_class
, r
->class)))
243 && (!r
->instance
|| (ch
.res_name
&& strstr(ch
.res_name
, r
->instance
)))) {
244 c
->isfloating
= r
->isfloating
;
246 c
->tags
[idxoftag(r
->tag
)] = True
;
256 memcpy(c
->tags
, tagset
[seltags
], TAGSZ
);
263 for(c
= clients
; c
; c
= c
->next
)
266 if(!lt
->arrange
|| c
->isfloating
)
267 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
287 attachstack(Client
*c
) {
296 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
301 buttonpress(XEvent
*e
) {
304 XButtonPressedEvent
*ev
= &e
->xbutton
;
306 if(ev
->window
== barwin
) {
308 for(i
= 0; i
< LENGTH(tags
); i
++) {
311 if(ev
->button
== Button1
) {
312 if(ev
->state
& MODKEY
)
317 else if(ev
->button
== Button3
) {
318 if(ev
->state
& MODKEY
)
326 if((ev
->x
< x
+ blw
) && ev
->button
== Button1
)
329 else if((c
= getclient(ev
->window
))) {
331 if(CLEANMASK(ev
->state
) != MODKEY
)
333 if(ev
->button
== Button1
) {
337 else if(ev
->button
== Button2
) {
338 if(lt
->arrange
&& c
->isfloating
)
339 togglefloating(NULL
);
343 else if(ev
->button
== Button3
&& !c
->isfixed
) {
353 XSetErrorHandler(xerrorstart
);
355 /* this causes an error if some other window manager is running */
356 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
359 eprint("dwm: another window manager is already running\n");
361 XSetErrorHandler(NULL
);
362 xerrorxlib
= XSetErrorHandler(xerror
);
374 XFreeFontSet(dpy
, dc
.font
.set
);
376 XFreeFont(dpy
, dc
.font
.xfont
);
377 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
378 XFreePixmap(dpy
, dc
.drawable
);
380 XFreeCursor(dpy
, cursor
[CurNormal
]);
381 XFreeCursor(dpy
, cursor
[CurResize
]);
382 XFreeCursor(dpy
, cursor
[CurMove
]);
383 XDestroyWindow(dpy
, barwin
);
385 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
389 configure(Client
*c
) {
392 ce
.type
= ConfigureNotify
;
400 ce
.border_width
= c
->bw
;
402 ce
.override_redirect
= False
;
403 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
407 configurenotify(XEvent
*e
) {
408 XConfigureEvent
*ev
= &e
->xconfigure
;
410 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
420 configurerequest(XEvent
*e
) {
422 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
425 if((c
= getclient(ev
->window
))) {
426 if(ev
->value_mask
& CWBorderWidth
)
427 c
->bw
= ev
->border_width
;
428 if(c
->isfixed
|| c
->isfloating
|| !lt
->arrange
) {
429 if(ev
->value_mask
& CWX
)
431 if(ev
->value_mask
& CWY
)
433 if(ev
->value_mask
& CWWidth
)
435 if(ev
->value_mask
& CWHeight
)
437 if((c
->x
- sx
+ c
->w
) > sw
&& c
->isfloating
)
438 c
->x
= sx
+ (sw
/ 2 - c
->w
/ 2); /* center in x direction */
439 if((c
->y
- sy
+ c
->h
) > sh
&& c
->isfloating
)
440 c
->y
= sy
+ (sh
/ 2 - c
->h
/ 2); /* center in y direction */
441 if((ev
->value_mask
& (CWX
|CWY
))
442 && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
445 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
453 wc
.width
= ev
->width
;
454 wc
.height
= ev
->height
;
455 wc
.border_width
= ev
->border_width
;
456 wc
.sibling
= ev
->above
;
457 wc
.stack_mode
= ev
->detail
;
458 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
464 destroynotify(XEvent
*e
) {
466 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
468 if((c
= getclient(ev
->window
)))
475 c
->prev
->next
= c
->next
;
477 c
->next
->prev
= c
->prev
;
480 c
->next
= c
->prev
= NULL
;
484 detachstack(Client
*c
) {
487 for(tc
= &stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
497 for(c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
498 for(i
= 0; i
< LENGTH(tags
); i
++) {
499 dc
.w
= textw(tags
[i
]);
500 if(tagset
[seltags
][i
]) {
501 drawtext(tags
[i
], dc
.sel
, isurgent(i
));
502 drawsquare(c
&& c
->tags
[i
], isoccupied(i
), isurgent(i
), dc
.sel
);
505 drawtext(tags
[i
], dc
.norm
, isurgent(i
));
506 drawsquare(c
&& c
->tags
[i
], isoccupied(i
), isurgent(i
), dc
.norm
);
512 drawtext(lt
->symbol
, dc
.norm
, False
);
523 drawtext(stext
, dc
.norm
, False
);
524 if((dc
.w
= dc
.x
- x
) > bh
) {
527 drawtext(c
->name
, dc
.sel
, False
);
528 drawsquare(False
, c
->isfloating
, False
, dc
.sel
);
531 drawtext(NULL
, dc
.norm
, False
);
533 XCopyArea(dpy
, dc
.drawable
, barwin
, dc
.gc
, 0, 0, bw
, bh
, 0, 0);
538 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
541 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
543 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
544 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
545 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
549 r
.width
= r
.height
= x
+ 1;
550 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
553 r
.width
= r
.height
= x
;
554 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
559 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
561 unsigned int len
, olen
;
562 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
565 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
566 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
570 len
= MIN(olen
, sizeof buf
);
571 memcpy(buf
, text
, len
);
573 h
= dc
.font
.ascent
+ dc
.font
.descent
;
574 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
576 /* shorten text if necessary */
577 for(; len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
; len
--);
588 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
590 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
592 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
596 emallocz(unsigned int size
) {
597 void *res
= calloc(1, size
);
600 eprint("fatal: could not malloc() %u bytes\n", size
);
605 enternotify(XEvent
*e
) {
607 XCrossingEvent
*ev
= &e
->xcrossing
;
609 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
611 if((c
= getclient(ev
->window
)))
618 eprint(const char *errstr
, ...) {
621 va_start(ap
, errstr
);
622 vfprintf(stderr
, errstr
, ap
);
629 XExposeEvent
*ev
= &e
->xexpose
;
631 if(ev
->count
== 0 && (ev
->window
== barwin
))
637 if(!c
|| (c
&& !isvisible(c
)))
638 for(c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
639 if(sel
&& sel
!= c
) {
640 grabbuttons(sel
, False
);
641 XSetWindowBorder(dpy
, sel
->win
, dc
.norm
[ColBorder
]);
646 grabbuttons(c
, True
);
650 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
651 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
654 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
659 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
660 XFocusChangeEvent
*ev
= &e
->xfocus
;
662 if(sel
&& ev
->window
!= sel
->win
)
663 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
667 focusnext(const char *arg
) {
672 for(c
= sel
->next
; c
&& !isvisible(c
); c
= c
->next
);
674 for(c
= clients
; c
&& !isvisible(c
); c
= c
->next
);
682 focusprev(const char *arg
) {
687 for(c
= sel
->prev
; c
&& !isvisible(c
); c
= c
->prev
);
689 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
690 for(; c
&& !isvisible(c
); c
= c
->prev
);
699 getclient(Window w
) {
702 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
707 getcolor(const char *colstr
) {
708 Colormap cmap
= DefaultColormap(dpy
, screen
);
711 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
712 eprint("error, cannot allocate color '%s'\n", colstr
);
720 unsigned char *p
= NULL
;
721 unsigned long n
, extra
;
724 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
725 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
726 if(status
!= Success
)
735 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
740 if(!text
|| size
== 0)
743 XGetTextProperty(dpy
, w
, &name
, atom
);
746 if(name
.encoding
== XA_STRING
)
747 strncpy(text
, (char *)name
.value
, size
- 1);
749 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
751 strncpy(text
, *list
, size
- 1);
752 XFreeStringList(list
);
755 text
[size
- 1] = '\0';
761 grabbuttons(Client
*c
, Bool focused
) {
763 unsigned int buttons
[] = { Button1
, Button2
, Button3
};
764 unsigned int modifiers
[] = { MODKEY
, MODKEY
|LockMask
, MODKEY
|numlockmask
,
765 MODKEY
|numlockmask
|LockMask
} ;
767 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
769 for(i
= 0; i
< LENGTH(buttons
); i
++)
770 for(j
= 0; j
< LENGTH(modifiers
); j
++)
771 XGrabButton(dpy
, buttons
[i
], modifiers
[j
], c
->win
, False
,
772 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
774 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
775 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
782 XModifierKeymap
*modmap
;
784 /* init modifier map */
785 modmap
= XGetModifierMapping(dpy
);
786 for(i
= 0; i
< 8; i
++)
787 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
788 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
789 numlockmask
= (1 << i
);
791 XFreeModifiermap(modmap
);
793 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
794 for(i
= 0; i
< LENGTH(keys
); i
++) {
795 code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
);
796 XGrabKey(dpy
, code
, keys
[i
].mod
, root
, True
,
797 GrabModeAsync
, GrabModeAsync
);
798 XGrabKey(dpy
, code
, keys
[i
].mod
|LockMask
, root
, True
,
799 GrabModeAsync
, GrabModeAsync
);
800 XGrabKey(dpy
, code
, keys
[i
].mod
|numlockmask
, root
, True
,
801 GrabModeAsync
, GrabModeAsync
);
802 XGrabKey(dpy
, code
, keys
[i
].mod
|numlockmask
|LockMask
, root
, True
,
803 GrabModeAsync
, GrabModeAsync
);
808 idxoftag(const char *t
) {
811 for(i
= 0; (i
< LENGTH(tags
)) && t
&& strcmp(tags
[i
], t
); i
++);
812 return (i
< LENGTH(tags
)) ? i
: 0;
816 initfont(const char *fontstr
) {
817 char *def
, **missing
;
822 XFreeFontSet(dpy
, dc
.font
.set
);
823 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
826 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
827 XFreeStringList(missing
);
830 XFontSetExtents
*font_extents
;
831 XFontStruct
**xfonts
;
833 dc
.font
.ascent
= dc
.font
.descent
= 0;
834 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
835 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
836 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
837 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
838 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
844 XFreeFont(dpy
, dc
.font
.xfont
);
845 dc
.font
.xfont
= NULL
;
846 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
847 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
848 eprint("error, cannot load font: '%s'\n", fontstr
);
849 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
850 dc
.font
.descent
= dc
.font
.xfont
->descent
;
852 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
856 isoccupied(unsigned int t
) {
859 for(c
= clients
; c
; c
= c
->next
)
866 isprotodel(Client
*c
) {
871 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
872 for(i
= 0; !ret
&& i
< n
; i
++)
873 if(protocols
[i
] == wmatom
[WMDelete
])
881 isurgent(unsigned int t
) {
884 for(c
= clients
; c
; c
= c
->next
)
885 if(c
->isurgent
&& c
->tags
[t
])
891 isvisible(Client
*c
) {
894 for(i
= 0; i
< LENGTH(tags
); i
++)
895 if(c
->tags
[i
] && tagset
[seltags
][i
])
901 keypress(XEvent
*e
) {
907 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
908 for(i
= 0; i
< LENGTH(keys
); i
++)
909 if(keysym
== keys
[i
].keysym
910 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
913 keys
[i
].func(keys
[i
].arg
);
918 killclient(const char *arg
) {
923 if(isprotodel(sel
)) {
924 ev
.type
= ClientMessage
;
925 ev
.xclient
.window
= sel
->win
;
926 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
927 ev
.xclient
.format
= 32;
928 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
929 ev
.xclient
.data
.l
[1] = CurrentTime
;
930 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
933 XKillClient(dpy
, sel
->win
);
937 manage(Window w
, XWindowAttributes
*wa
) {
938 Client
*c
, *t
= NULL
;
943 c
= emallocz(sizeof(Client
));
944 c
->tags
= emallocz(TAGSZ
);
952 c
->oldbw
= wa
->border_width
;
953 if(c
->w
== sw
&& c
->h
== sh
) {
956 c
->bw
= wa
->border_width
;
959 if(c
->x
+ c
->w
+ 2 * c
->bw
> wx
+ ww
)
960 c
->x
= wx
+ ww
- c
->w
- 2 * c
->bw
;
961 if(c
->y
+ c
->h
+ 2 * c
->bw
> wy
+ wh
)
962 c
->y
= wy
+ wh
- c
->h
- 2 * c
->bw
;
963 c
->x
= MAX(c
->x
, wx
);
964 c
->y
= MAX(c
->y
, wy
);
968 wc
.border_width
= c
->bw
;
969 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
970 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
971 configure(c
); /* propagates border_width, if size doesn't change */
973 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
974 grabbuttons(c
, False
);
976 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
977 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
979 memcpy(c
->tags
, t
->tags
, TAGSZ
);
983 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
986 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
988 XMapWindow(dpy
, c
->win
);
989 setclientstate(c
, NormalState
);
994 mappingnotify(XEvent
*e
) {
995 XMappingEvent
*ev
= &e
->xmapping
;
997 XRefreshKeyboardMapping(ev
);
998 if(ev
->request
== MappingKeyboard
)
1003 maprequest(XEvent
*e
) {
1004 static XWindowAttributes wa
;
1005 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1007 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1009 if(wa
.override_redirect
)
1011 if(!getclient(ev
->window
))
1012 manage(ev
->window
, &wa
);
1019 for(c
= clients
; c
; c
= c
->next
)
1020 if(!c
->isfloating
&& isvisible(c
))
1021 resize(c
, wx
, wy
, ww
- 2 * c
->bw
, wh
- 2 * c
->bw
, RESIZEHINTS
);
1025 movemouse(Client
*c
) {
1026 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1033 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1034 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1036 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1038 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1041 XUngrabPointer(dpy
, CurrentTime
);
1043 case ConfigureRequest
:
1046 handler
[ev
.type
](&ev
);
1050 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1051 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1052 if(abs(wx
- nx
) < SNAP
)
1054 else if(abs((wx
+ ww
) - (nx
+ c
->w
+ 2 * c
->bw
)) < SNAP
)
1055 nx
= wx
+ ww
- c
->w
- 2 * c
->bw
;
1056 if(abs(wy
- ny
) < SNAP
)
1058 else if(abs((wy
+ wh
) - (ny
+ c
->h
+ 2 * c
->bw
)) < SNAP
)
1059 ny
= wy
+ wh
- c
->h
- 2 * c
->bw
;
1060 if(!c
->isfloating
&& lt
->arrange
&& (abs(nx
- c
->x
) > SNAP
|| abs(ny
- c
->y
) > SNAP
))
1061 togglefloating(NULL
);
1062 if(!lt
->arrange
|| c
->isfloating
)
1063 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1070 nextunfloating(Client
*c
) {
1071 for(; c
&& (c
->isfloating
|| !isvisible(c
)); c
= c
->next
);
1076 propertynotify(XEvent
*e
) {
1079 XPropertyEvent
*ev
= &e
->xproperty
;
1081 if(ev
->state
== PropertyDelete
)
1082 return; /* ignore */
1083 if((c
= getclient(ev
->window
))) {
1086 case XA_WM_TRANSIENT_FOR
:
1087 XGetTransientForHint(dpy
, c
->win
, &trans
);
1088 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1091 case XA_WM_NORMAL_HINTS
:
1099 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1108 quit(const char *arg
) {
1109 readin
= running
= False
;
1113 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1117 /* set minimum possible */
1121 /* temporarily remove base dimensions */
1125 /* adjust for aspect limits */
1126 if(c
->minax
!= c
->maxax
&& c
->minay
!= c
->maxay
1127 && c
->minax
> 0 && c
->maxax
> 0 && c
->minay
> 0 && c
->maxay
> 0) {
1128 if(w
* c
->maxay
> h
* c
->maxax
)
1129 w
= h
* c
->maxax
/ c
->maxay
;
1130 else if(w
* c
->minay
< h
* c
->minax
)
1131 h
= w
* c
->minay
/ c
->minax
;
1134 /* adjust for increment value */
1140 /* restore base dimensions */
1144 w
= MAX(w
, c
->minw
);
1145 h
= MAX(h
, c
->minh
);
1148 w
= MIN(w
, c
->maxw
);
1151 h
= MIN(h
, c
->maxh
);
1153 if(w
<= 0 || h
<= 0)
1156 x
= sw
- w
- 2 * c
->bw
;
1158 y
= sh
- h
- 2 * c
->bw
;
1159 if(x
+ w
+ 2 * c
->bw
< sx
)
1161 if(y
+ h
+ 2 * c
->bw
< sy
)
1163 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1166 c
->w
= wc
.width
= w
;
1167 c
->h
= wc
.height
= h
;
1168 wc
.border_width
= c
->bw
;
1169 XConfigureWindow(dpy
, c
->win
,
1170 CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1177 resizemouse(Client
*c
) {
1184 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1185 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1187 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1189 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1192 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1193 c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1194 XUngrabPointer(dpy
, CurrentTime
);
1195 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1197 case ConfigureRequest
:
1200 handler
[ev
.type
](&ev
);
1204 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1205 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1206 if(!c
->isfloating
&& lt
->arrange
&& (abs(nw
- c
->w
) > SNAP
|| abs(nh
- c
->h
) > SNAP
)) {
1207 togglefloating(NULL
);
1209 if(!lt
->arrange
|| c
->isfloating
)
1210 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1225 if(sel
->isfloating
|| !lt
->arrange
)
1226 XRaiseWindow(dpy
, sel
->win
);
1228 wc
.stack_mode
= Below
;
1229 wc
.sibling
= barwin
;
1230 for(c
= stack
; c
; c
= c
->snext
)
1231 if(!c
->isfloating
&& isvisible(c
)) {
1232 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1233 wc
.sibling
= c
->win
;
1237 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1243 char sbuf
[sizeof stext
];
1246 unsigned int len
, offset
;
1249 /* main event loop, also reads status text from stdin */
1251 xfd
= ConnectionNumber(dpy
);
1254 len
= sizeof stext
- 1;
1255 sbuf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1259 FD_SET(STDIN_FILENO
, &rd
);
1261 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1264 eprint("select failed\n");
1266 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1267 switch((r
= read(STDIN_FILENO
, sbuf
+ offset
, len
- offset
))) {
1269 strncpy(stext
, strerror(errno
), len
);
1273 strncpy(stext
, "EOF", 4);
1277 for(p
= sbuf
+ offset
; r
> 0; p
++, r
--, offset
++)
1278 if(*p
== '\n' || *p
== '\0') {
1280 strncpy(stext
, sbuf
, len
);
1281 p
+= r
- 1; /* p is sbuf + offset + r - 1 */
1282 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1285 memmove(sbuf
, p
- r
+ 1, r
);
1292 while(XPending(dpy
)) {
1293 XNextEvent(dpy
, &ev
);
1294 if(handler
[ev
.type
])
1295 (handler
[ev
.type
])(&ev
); /* call handler */
1302 unsigned int i
, num
;
1303 Window
*wins
, d1
, d2
;
1304 XWindowAttributes wa
;
1307 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1308 for(i
= 0; i
< num
; i
++) {
1309 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1310 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1312 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1313 manage(wins
[i
], &wa
);
1315 for(i
= 0; i
< num
; i
++) { /* now the transients */
1316 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1318 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1319 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1320 manage(wins
[i
], &wa
);
1328 setclientstate(Client
*c
, long state
) {
1329 long data
[] = {state
, None
};
1331 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1332 PropModeReplace
, (unsigned char *)data
, 2);
1338 XSetWindowAttributes wa
;
1341 screen
= DefaultScreen(dpy
);
1342 root
= RootWindow(dpy
, screen
);
1346 sw
= DisplayWidth(dpy
, screen
);
1347 sh
= DisplayHeight(dpy
, screen
);
1348 bh
= dc
.font
.height
+ 2;
1352 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1353 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1354 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1355 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1356 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1357 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1360 wa
.cursor
= cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1361 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1362 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1364 /* init appearance */
1365 dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
);
1366 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
1367 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
1368 dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
);
1369 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
1370 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
1373 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1374 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1375 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1377 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1380 tagset
[0] = emallocz(TAGSZ
);
1381 tagset
[1] = emallocz(TAGSZ
);
1382 tagset
[0][0] = tagset
[1][0] = True
;
1385 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1386 w
= textw(layouts
[i
].symbol
);
1390 wa
.override_redirect
= 1;
1391 wa
.background_pixmap
= ParentRelative
;
1392 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1394 barwin
= XCreateWindow(dpy
, root
, bx
, by
, bw
, bh
, 0, DefaultDepth(dpy
, screen
),
1395 CopyFromParent
, DefaultVisual(dpy
, screen
),
1396 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1397 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
1398 XMapRaised(dpy
, barwin
);
1399 strcpy(stext
, "dwm-"VERSION
);
1402 /* EWMH support per view */
1403 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1404 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1406 /* select for events */
1407 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
1408 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
;
1409 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1410 XSelectInput(dpy
, root
, wa
.event_mask
);
1418 spawn(const char *arg
) {
1419 static char *shell
= NULL
;
1421 if(!shell
&& !(shell
= getenv("SHELL")))
1425 /* The double-fork construct avoids zombie processes and keeps the code
1426 * clean from stupid signal handlers. */
1430 close(ConnectionNumber(dpy
));
1432 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1433 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1442 tag(const char *arg
) {
1447 for(i
= 0; i
< LENGTH(tags
); i
++)
1448 sel
->tags
[i
] = (arg
== NULL
);
1449 sel
->tags
[idxoftag(arg
)] = True
;
1454 textnw(const char *text
, unsigned int len
) {
1458 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1461 return XTextWidth(dc
.font
.xfont
, text
, len
);
1465 textw(const char *text
) {
1466 return textnw(text
, strlen(text
)) + dc
.font
.height
;
1470 togglefloating(const char *arg
) {
1473 sel
->isfloating
= !sel
->isfloating
;
1475 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1480 togglelayout(const char *arg
) {
1484 if(++lt
== &layouts
[LENGTH(layouts
)])
1488 for(i
= 0; i
< LENGTH(layouts
); i
++)
1489 if(!strcmp(arg
, layouts
[i
].symbol
))
1491 if(i
== LENGTH(layouts
))
1502 toggletag(const char *arg
) {
1508 sel
->tags
[i
] = !sel
->tags
[i
];
1509 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1510 if(j
== LENGTH(tags
))
1511 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1516 toggleview(const char *arg
) {
1520 tagset
[seltags
][i
] = !tagset
[seltags
][i
];
1521 for(j
= 0; j
< LENGTH(tags
) && !tagset
[seltags
][j
]; j
++);
1522 if(j
== LENGTH(tags
))
1523 tagset
[seltags
][i
] = True
; /* at least one tag must be viewed */
1531 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1532 c
->isbanned
= False
;
1536 unmanage(Client
*c
) {
1539 wc
.border_width
= c
->oldbw
;
1540 /* The server grab construct avoids race conditions. */
1542 XSetErrorHandler(xerrordummy
);
1543 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1548 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1549 setclientstate(c
, WithdrawnState
);
1553 XSetErrorHandler(xerror
);
1559 unmapnotify(XEvent
*e
) {
1561 XUnmapEvent
*ev
= &e
->xunmap
;
1563 if((c
= getclient(ev
->window
)))
1569 if(dc
.drawable
!= 0)
1570 XFreePixmap(dpy
, dc
.drawable
);
1571 dc
.drawable
= XCreatePixmap(dpy
, root
, bw
, bh
, DefaultDepth(dpy
, screen
));
1572 XMoveResizeWindow(dpy
, barwin
, bx
, by
, bw
, bh
);
1584 /* window area geometry */
1590 /* update layout geometries */
1591 for(i
= 0; i
< LENGTH(layouts
); i
++)
1592 if(layouts
[i
].updategeom
)
1593 layouts
[i
].updategeom();
1597 updatesizehints(Client
*c
) {
1601 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1603 c
->flags
= size
.flags
;
1604 if(c
->flags
& PBaseSize
) {
1605 c
->basew
= size
.base_width
;
1606 c
->baseh
= size
.base_height
;
1608 else if(c
->flags
& PMinSize
) {
1609 c
->basew
= size
.min_width
;
1610 c
->baseh
= size
.min_height
;
1613 c
->basew
= c
->baseh
= 0;
1614 if(c
->flags
& PResizeInc
) {
1615 c
->incw
= size
.width_inc
;
1616 c
->inch
= size
.height_inc
;
1619 c
->incw
= c
->inch
= 0;
1620 if(c
->flags
& PMaxSize
) {
1621 c
->maxw
= size
.max_width
;
1622 c
->maxh
= size
.max_height
;
1625 c
->maxw
= c
->maxh
= 0;
1626 if(c
->flags
& PMinSize
) {
1627 c
->minw
= size
.min_width
;
1628 c
->minh
= size
.min_height
;
1630 else if(c
->flags
& PBaseSize
) {
1631 c
->minw
= size
.base_width
;
1632 c
->minh
= size
.base_height
;
1635 c
->minw
= c
->minh
= 0;
1636 if(c
->flags
& PAspect
) {
1637 c
->minax
= size
.min_aspect
.x
;
1638 c
->maxax
= size
.max_aspect
.x
;
1639 c
->minay
= size
.min_aspect
.y
;
1640 c
->maxay
= size
.max_aspect
.y
;
1643 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1644 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1645 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1649 updatetitle(Client
*c
) {
1650 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1651 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1655 updatewmhints(Client
*c
) {
1658 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1660 sel
->isurgent
= False
;
1662 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1668 view(const char *arg
) {
1669 seltags
^= 1; /* toggle sel tagset */
1670 memset(tagset
[seltags
], (NULL
== arg
), TAGSZ
);
1671 tagset
[seltags
][idxoftag(arg
)] = True
;
1676 viewprevtag(const char *arg
) {
1677 seltags
^= 1; /* toggle sel tagset */
1681 /* There's no way to check accesses to destroyed windows, thus those cases are
1682 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1683 * default error handler, which may call exit. */
1685 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1686 if(ee
->error_code
== BadWindow
1687 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1688 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1689 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1690 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1691 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1692 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1693 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1694 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1696 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1697 ee
->request_code
, ee
->error_code
);
1698 return xerrorxlib(dpy
, ee
); /* may call exit */
1702 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1706 /* Startup Error handler to check if another window manager
1707 * is already running. */
1709 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1715 main(int argc
, char *argv
[]) {
1716 if(argc
== 2 && !strcmp("-v", argv
[1]))
1717 eprint("dwm-"VERSION
", © 2006-2008 dwm engineers, see LICENSE for details\n");
1719 eprint("usage: dwm [-v]\n");
1721 setlocale(LC_CTYPE
, "");
1722 if(!(dpy
= XOpenDisplay(0)))
1723 eprint("dwm: cannot open display\n");