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
);
342 else if(ev
->button
== Button3
&& !c
->isfixed
) {
352 XSetErrorHandler(xerrorstart
);
354 /* this causes an error if some other window manager is running */
355 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
358 eprint("dwm: another window manager is already running\n");
360 XSetErrorHandler(NULL
);
361 xerrorxlib
= XSetErrorHandler(xerror
);
373 XFreeFontSet(dpy
, dc
.font
.set
);
375 XFreeFont(dpy
, dc
.font
.xfont
);
376 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
377 XFreePixmap(dpy
, dc
.drawable
);
379 XFreeCursor(dpy
, cursor
[CurNormal
]);
380 XFreeCursor(dpy
, cursor
[CurResize
]);
381 XFreeCursor(dpy
, cursor
[CurMove
]);
382 XDestroyWindow(dpy
, barwin
);
384 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
388 configure(Client
*c
) {
391 ce
.type
= ConfigureNotify
;
399 ce
.border_width
= c
->bw
;
401 ce
.override_redirect
= False
;
402 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
406 configurenotify(XEvent
*e
) {
407 XConfigureEvent
*ev
= &e
->xconfigure
;
409 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
419 configurerequest(XEvent
*e
) {
421 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
424 if((c
= getclient(ev
->window
))) {
425 if(ev
->value_mask
& CWBorderWidth
)
426 c
->bw
= ev
->border_width
;
427 if(c
->isfixed
|| c
->isfloating
|| !lt
->arrange
) {
428 if(ev
->value_mask
& CWX
)
430 if(ev
->value_mask
& CWY
)
432 if(ev
->value_mask
& CWWidth
)
434 if(ev
->value_mask
& CWHeight
)
436 if((c
->x
- sx
+ c
->w
) > sw
&& c
->isfloating
)
437 c
->x
= sx
+ (sw
/ 2 - c
->w
/ 2); /* center in x direction */
438 if((c
->y
- sy
+ c
->h
) > sh
&& c
->isfloating
)
439 c
->y
= sy
+ (sh
/ 2 - c
->h
/ 2); /* center in y direction */
440 if((ev
->value_mask
& (CWX
|CWY
))
441 && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
444 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
452 wc
.width
= ev
->width
;
453 wc
.height
= ev
->height
;
454 wc
.border_width
= ev
->border_width
;
455 wc
.sibling
= ev
->above
;
456 wc
.stack_mode
= ev
->detail
;
457 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
463 destroynotify(XEvent
*e
) {
465 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
467 if((c
= getclient(ev
->window
)))
474 c
->prev
->next
= c
->next
;
476 c
->next
->prev
= c
->prev
;
479 c
->next
= c
->prev
= NULL
;
483 detachstack(Client
*c
) {
486 for(tc
= &stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
496 for(c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
497 for(i
= 0; i
< LENGTH(tags
); i
++) {
498 dc
.w
= textw(tags
[i
]);
499 if(tagset
[seltags
][i
]) {
500 drawtext(tags
[i
], dc
.sel
, isurgent(i
));
501 drawsquare(c
&& c
->tags
[i
], isoccupied(i
), isurgent(i
), dc
.sel
);
504 drawtext(tags
[i
], dc
.norm
, isurgent(i
));
505 drawsquare(c
&& c
->tags
[i
], isoccupied(i
), isurgent(i
), dc
.norm
);
511 drawtext(lt
->symbol
, dc
.norm
, False
);
522 drawtext(stext
, dc
.norm
, False
);
523 if((dc
.w
= dc
.x
- x
) > bh
) {
526 drawtext(c
->name
, dc
.sel
, False
);
527 drawsquare(False
, c
->isfloating
, False
, dc
.sel
);
530 drawtext(NULL
, dc
.norm
, False
);
532 XCopyArea(dpy
, dc
.drawable
, barwin
, dc
.gc
, 0, 0, bw
, bh
, 0, 0);
537 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
540 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
542 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
543 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
544 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
548 r
.width
= r
.height
= x
+ 1;
549 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
552 r
.width
= r
.height
= x
;
553 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
558 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
560 unsigned int len
, olen
;
561 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
564 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
565 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
569 len
= MIN(olen
, sizeof buf
);
570 memcpy(buf
, text
, len
);
572 h
= dc
.font
.ascent
+ dc
.font
.descent
;
573 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
575 /* shorten text if necessary */
576 for(; len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
; len
--);
587 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
589 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
591 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
595 emallocz(unsigned int size
) {
596 void *res
= calloc(1, size
);
599 eprint("fatal: could not malloc() %u bytes\n", size
);
604 enternotify(XEvent
*e
) {
606 XCrossingEvent
*ev
= &e
->xcrossing
;
608 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
610 if((c
= getclient(ev
->window
)))
617 eprint(const char *errstr
, ...) {
620 va_start(ap
, errstr
);
621 vfprintf(stderr
, errstr
, ap
);
628 XExposeEvent
*ev
= &e
->xexpose
;
630 if(ev
->count
== 0 && (ev
->window
== barwin
))
636 if(!c
|| (c
&& !isvisible(c
)))
637 for(c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
638 if(sel
&& sel
!= c
) {
639 grabbuttons(sel
, False
);
640 XSetWindowBorder(dpy
, sel
->win
, dc
.norm
[ColBorder
]);
645 grabbuttons(c
, True
);
649 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
650 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
653 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
658 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
659 XFocusChangeEvent
*ev
= &e
->xfocus
;
661 if(sel
&& ev
->window
!= sel
->win
)
662 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
666 focusnext(const char *arg
) {
671 for(c
= sel
->next
; c
&& !isvisible(c
); c
= c
->next
);
673 for(c
= clients
; c
&& !isvisible(c
); c
= c
->next
);
681 focusprev(const char *arg
) {
686 for(c
= sel
->prev
; c
&& !isvisible(c
); c
= c
->prev
);
688 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
689 for(; c
&& !isvisible(c
); c
= c
->prev
);
698 getclient(Window w
) {
701 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
706 getcolor(const char *colstr
) {
707 Colormap cmap
= DefaultColormap(dpy
, screen
);
710 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
711 eprint("error, cannot allocate color '%s'\n", colstr
);
719 unsigned char *p
= NULL
;
720 unsigned long n
, extra
;
723 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
724 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
725 if(status
!= Success
)
734 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
739 if(!text
|| size
== 0)
742 XGetTextProperty(dpy
, w
, &name
, atom
);
745 if(name
.encoding
== XA_STRING
)
746 strncpy(text
, (char *)name
.value
, size
- 1);
748 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
750 strncpy(text
, *list
, size
- 1);
751 XFreeStringList(list
);
754 text
[size
- 1] = '\0';
760 grabbuttons(Client
*c
, Bool focused
) {
762 unsigned int buttons
[] = { Button1
, Button2
, Button3
};
763 unsigned int modifiers
[] = { MODKEY
, MODKEY
|LockMask
, MODKEY
|numlockmask
,
764 MODKEY
|numlockmask
|LockMask
} ;
766 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
768 for(i
= 0; i
< LENGTH(buttons
); i
++)
769 for(j
= 0; j
< LENGTH(modifiers
); j
++)
770 XGrabButton(dpy
, buttons
[i
], modifiers
[j
], c
->win
, False
,
771 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
773 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
774 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
781 XModifierKeymap
*modmap
;
783 /* init modifier map */
784 modmap
= XGetModifierMapping(dpy
);
785 for(i
= 0; i
< 8; i
++)
786 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
787 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
788 numlockmask
= (1 << i
);
790 XFreeModifiermap(modmap
);
792 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
793 for(i
= 0; i
< LENGTH(keys
); i
++) {
794 code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
);
795 XGrabKey(dpy
, code
, keys
[i
].mod
, root
, True
,
796 GrabModeAsync
, GrabModeAsync
);
797 XGrabKey(dpy
, code
, keys
[i
].mod
|LockMask
, root
, True
,
798 GrabModeAsync
, GrabModeAsync
);
799 XGrabKey(dpy
, code
, keys
[i
].mod
|numlockmask
, root
, True
,
800 GrabModeAsync
, GrabModeAsync
);
801 XGrabKey(dpy
, code
, keys
[i
].mod
|numlockmask
|LockMask
, root
, True
,
802 GrabModeAsync
, GrabModeAsync
);
807 idxoftag(const char *t
) {
810 for(i
= 0; (i
< LENGTH(tags
)) && t
&& strcmp(tags
[i
], t
); i
++);
811 return (i
< LENGTH(tags
)) ? i
: 0;
815 initfont(const char *fontstr
) {
816 char *def
, **missing
;
821 XFreeFontSet(dpy
, dc
.font
.set
);
822 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
825 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
826 XFreeStringList(missing
);
829 XFontSetExtents
*font_extents
;
830 XFontStruct
**xfonts
;
832 dc
.font
.ascent
= dc
.font
.descent
= 0;
833 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
834 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
835 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
836 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
837 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
843 XFreeFont(dpy
, dc
.font
.xfont
);
844 dc
.font
.xfont
= NULL
;
845 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
846 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
847 eprint("error, cannot load font: '%s'\n", fontstr
);
848 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
849 dc
.font
.descent
= dc
.font
.xfont
->descent
;
851 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
855 isoccupied(unsigned int t
) {
858 for(c
= clients
; c
; c
= c
->next
)
865 isprotodel(Client
*c
) {
870 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
871 for(i
= 0; !ret
&& i
< n
; i
++)
872 if(protocols
[i
] == wmatom
[WMDelete
])
880 isurgent(unsigned int t
) {
883 for(c
= clients
; c
; c
= c
->next
)
884 if(c
->isurgent
&& c
->tags
[t
])
890 isvisible(Client
*c
) {
893 for(i
= 0; i
< LENGTH(tags
); i
++)
894 if(c
->tags
[i
] && tagset
[seltags
][i
])
900 keypress(XEvent
*e
) {
906 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
907 for(i
= 0; i
< LENGTH(keys
); i
++)
908 if(keysym
== keys
[i
].keysym
909 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
912 keys
[i
].func(keys
[i
].arg
);
917 killclient(const char *arg
) {
922 if(isprotodel(sel
)) {
923 ev
.type
= ClientMessage
;
924 ev
.xclient
.window
= sel
->win
;
925 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
926 ev
.xclient
.format
= 32;
927 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
928 ev
.xclient
.data
.l
[1] = CurrentTime
;
929 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
932 XKillClient(dpy
, sel
->win
);
936 manage(Window w
, XWindowAttributes
*wa
) {
937 Client
*c
, *t
= NULL
;
942 c
= emallocz(sizeof(Client
));
943 c
->tags
= emallocz(TAGSZ
);
951 c
->oldbw
= wa
->border_width
;
952 if(c
->w
== sw
&& c
->h
== sh
) {
955 c
->bw
= wa
->border_width
;
958 if(c
->x
+ c
->w
+ 2 * c
->bw
> wx
+ ww
)
959 c
->x
= wx
+ ww
- c
->w
- 2 * c
->bw
;
960 if(c
->y
+ c
->h
+ 2 * c
->bw
> wy
+ wh
)
961 c
->y
= wy
+ wh
- c
->h
- 2 * c
->bw
;
962 c
->x
= MAX(c
->x
, wx
);
963 c
->y
= MAX(c
->y
, wy
);
967 wc
.border_width
= c
->bw
;
968 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
969 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
970 configure(c
); /* propagates border_width, if size doesn't change */
972 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
973 grabbuttons(c
, False
);
975 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
976 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
978 memcpy(c
->tags
, t
->tags
, TAGSZ
);
982 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
985 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
987 XMapWindow(dpy
, c
->win
);
988 setclientstate(c
, NormalState
);
993 mappingnotify(XEvent
*e
) {
994 XMappingEvent
*ev
= &e
->xmapping
;
996 XRefreshKeyboardMapping(ev
);
997 if(ev
->request
== MappingKeyboard
)
1002 maprequest(XEvent
*e
) {
1003 static XWindowAttributes wa
;
1004 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1006 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1008 if(wa
.override_redirect
)
1010 if(!getclient(ev
->window
))
1011 manage(ev
->window
, &wa
);
1015 movemouse(Client
*c
) {
1016 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1023 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1024 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1026 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1028 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1031 XUngrabPointer(dpy
, CurrentTime
);
1033 case ConfigureRequest
:
1036 handler
[ev
.type
](&ev
);
1040 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1041 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1042 if(abs(wx
- nx
) < SNAP
)
1044 else if(abs((wx
+ ww
) - (nx
+ c
->w
+ 2 * c
->bw
)) < SNAP
)
1045 nx
= wx
+ ww
- c
->w
- 2 * c
->bw
;
1046 if(abs(wy
- ny
) < SNAP
)
1048 else if(abs((wy
+ wh
) - (ny
+ c
->h
+ 2 * c
->bw
)) < SNAP
)
1049 ny
= wy
+ wh
- c
->h
- 2 * c
->bw
;
1050 if(!c
->isfloating
&& lt
->arrange
&& (abs(nx
- c
->x
) > SNAP
|| abs(ny
- c
->y
) > SNAP
))
1051 togglefloating(NULL
);
1052 if(!lt
->arrange
|| c
->isfloating
)
1053 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1060 nextunfloating(Client
*c
) {
1061 for(; c
&& (c
->isfloating
|| !isvisible(c
)); c
= c
->next
);
1066 propertynotify(XEvent
*e
) {
1069 XPropertyEvent
*ev
= &e
->xproperty
;
1071 if(ev
->state
== PropertyDelete
)
1072 return; /* ignore */
1073 if((c
= getclient(ev
->window
))) {
1076 case XA_WM_TRANSIENT_FOR
:
1077 XGetTransientForHint(dpy
, c
->win
, &trans
);
1078 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1081 case XA_WM_NORMAL_HINTS
:
1089 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1098 quit(const char *arg
) {
1099 readin
= running
= False
;
1103 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1107 /* set minimum possible */
1111 /* temporarily remove base dimensions */
1115 /* adjust for aspect limits */
1116 if(c
->minax
!= c
->maxax
&& c
->minay
!= c
->maxay
1117 && c
->minax
> 0 && c
->maxax
> 0 && c
->minay
> 0 && c
->maxay
> 0) {
1118 if(w
* c
->maxay
> h
* c
->maxax
)
1119 w
= h
* c
->maxax
/ c
->maxay
;
1120 else if(w
* c
->minay
< h
* c
->minax
)
1121 h
= w
* c
->minay
/ c
->minax
;
1124 /* adjust for increment value */
1130 /* restore base dimensions */
1134 w
= MAX(w
, c
->minw
);
1135 h
= MAX(h
, c
->minh
);
1138 w
= MIN(w
, c
->maxw
);
1141 h
= MIN(h
, c
->maxh
);
1143 if(w
<= 0 || h
<= 0)
1146 x
= sw
- w
- 2 * c
->bw
;
1148 y
= sh
- h
- 2 * c
->bw
;
1149 if(x
+ w
+ 2 * c
->bw
< sx
)
1151 if(y
+ h
+ 2 * c
->bw
< sy
)
1153 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1156 c
->w
= wc
.width
= w
;
1157 c
->h
= wc
.height
= h
;
1158 wc
.border_width
= c
->bw
;
1159 XConfigureWindow(dpy
, c
->win
,
1160 CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1167 resizemouse(Client
*c
) {
1174 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1175 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1177 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1179 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1182 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1183 c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1184 XUngrabPointer(dpy
, CurrentTime
);
1185 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1187 case ConfigureRequest
:
1190 handler
[ev
.type
](&ev
);
1194 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1195 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1196 if(!c
->isfloating
&& lt
->arrange
&& (abs(nw
- c
->w
) > SNAP
|| abs(nh
- c
->h
) > SNAP
)) {
1197 togglefloating(NULL
);
1199 if(!lt
->arrange
|| c
->isfloating
)
1200 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1215 if(sel
->isfloating
|| !lt
->arrange
)
1216 XRaiseWindow(dpy
, sel
->win
);
1218 wc
.stack_mode
= Below
;
1219 wc
.sibling
= barwin
;
1220 for(c
= stack
; c
; c
= c
->snext
)
1221 if(!c
->isfloating
&& isvisible(c
)) {
1222 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1223 wc
.sibling
= c
->win
;
1227 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1233 char sbuf
[sizeof stext
];
1236 unsigned int len
, offset
;
1239 /* main event loop, also reads status text from stdin */
1241 xfd
= ConnectionNumber(dpy
);
1244 len
= sizeof stext
- 1;
1245 sbuf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1249 FD_SET(STDIN_FILENO
, &rd
);
1251 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1254 eprint("select failed\n");
1256 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1257 switch((r
= read(STDIN_FILENO
, sbuf
+ offset
, len
- offset
))) {
1259 strncpy(stext
, strerror(errno
), len
);
1263 strncpy(stext
, "EOF", 4);
1267 for(p
= sbuf
+ offset
; r
> 0; p
++, r
--, offset
++)
1268 if(*p
== '\n' || *p
== '\0') {
1270 strncpy(stext
, sbuf
, len
);
1271 p
+= r
- 1; /* p is sbuf + offset + r - 1 */
1272 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1275 memmove(sbuf
, p
- r
+ 1, r
);
1282 while(XPending(dpy
)) {
1283 XNextEvent(dpy
, &ev
);
1284 if(handler
[ev
.type
])
1285 (handler
[ev
.type
])(&ev
); /* call handler */
1292 unsigned int i
, num
;
1293 Window
*wins
, d1
, d2
;
1294 XWindowAttributes wa
;
1297 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1298 for(i
= 0; i
< num
; i
++) {
1299 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1300 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1302 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1303 manage(wins
[i
], &wa
);
1305 for(i
= 0; i
< num
; i
++) { /* now the transients */
1306 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1308 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1309 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1310 manage(wins
[i
], &wa
);
1318 setclientstate(Client
*c
, long state
) {
1319 long data
[] = {state
, None
};
1321 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1322 PropModeReplace
, (unsigned char *)data
, 2);
1328 XSetWindowAttributes wa
;
1331 screen
= DefaultScreen(dpy
);
1332 root
= RootWindow(dpy
, screen
);
1336 sw
= DisplayWidth(dpy
, screen
);
1337 sh
= DisplayHeight(dpy
, screen
);
1338 bh
= dc
.font
.height
+ 2;
1342 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1343 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1344 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1345 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1346 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1347 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1350 wa
.cursor
= cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1351 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1352 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1354 /* init appearance */
1355 dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
);
1356 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
1357 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
1358 dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
);
1359 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
1360 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
1363 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1364 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1365 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1367 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1370 tagset
[0] = emallocz(TAGSZ
);
1371 tagset
[1] = emallocz(TAGSZ
);
1372 tagset
[0][0] = tagset
[1][0] = True
;
1375 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1376 w
= textw(layouts
[i
].symbol
);
1380 wa
.override_redirect
= 1;
1381 wa
.background_pixmap
= ParentRelative
;
1382 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1384 barwin
= XCreateWindow(dpy
, root
, bx
, by
, bw
, bh
, 0, DefaultDepth(dpy
, screen
),
1385 CopyFromParent
, DefaultVisual(dpy
, screen
),
1386 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1387 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
1388 XMapRaised(dpy
, barwin
);
1389 strcpy(stext
, "dwm-"VERSION
);
1392 /* EWMH support per view */
1393 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1394 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1396 /* select for events */
1397 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
1398 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
;
1399 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1400 XSelectInput(dpy
, root
, wa
.event_mask
);
1408 spawn(const char *arg
) {
1409 static char *shell
= NULL
;
1411 if(!shell
&& !(shell
= getenv("SHELL")))
1415 /* The double-fork construct avoids zombie processes and keeps the code
1416 * clean from stupid signal handlers. */
1420 close(ConnectionNumber(dpy
));
1422 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1423 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1432 tag(const char *arg
) {
1437 for(i
= 0; i
< LENGTH(tags
); i
++)
1438 sel
->tags
[i
] = (arg
== NULL
);
1439 sel
->tags
[idxoftag(arg
)] = True
;
1444 textnw(const char *text
, unsigned int len
) {
1448 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1451 return XTextWidth(dc
.font
.xfont
, text
, len
);
1455 textw(const char *text
) {
1456 return textnw(text
, strlen(text
)) + dc
.font
.height
;
1460 togglefloating(const char *arg
) {
1463 sel
->isfloating
= !sel
->isfloating
;
1465 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1470 togglelayout(const char *arg
) {
1474 if(++lt
== &layouts
[LENGTH(layouts
)])
1478 for(i
= 0; i
< LENGTH(layouts
); i
++)
1479 if(!strcmp(arg
, layouts
[i
].symbol
))
1481 if(i
== LENGTH(layouts
))
1492 toggletag(const char *arg
) {
1498 sel
->tags
[i
] = !sel
->tags
[i
];
1499 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1500 if(j
== LENGTH(tags
))
1501 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1506 toggleview(const char *arg
) {
1510 tagset
[seltags
][i
] = !tagset
[seltags
][i
];
1511 for(j
= 0; j
< LENGTH(tags
) && !tagset
[seltags
][j
]; j
++);
1512 if(j
== LENGTH(tags
))
1513 tagset
[seltags
][i
] = True
; /* at least one tag must be viewed */
1521 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1522 c
->isbanned
= False
;
1526 unmanage(Client
*c
) {
1529 wc
.border_width
= c
->oldbw
;
1530 /* The server grab construct avoids race conditions. */
1532 XSetErrorHandler(xerrordummy
);
1533 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1538 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1539 setclientstate(c
, WithdrawnState
);
1543 XSetErrorHandler(xerror
);
1549 unmapnotify(XEvent
*e
) {
1551 XUnmapEvent
*ev
= &e
->xunmap
;
1553 if((c
= getclient(ev
->window
)))
1559 if(dc
.drawable
!= 0)
1560 XFreePixmap(dpy
, dc
.drawable
);
1561 dc
.drawable
= XCreatePixmap(dpy
, root
, bw
, bh
, DefaultDepth(dpy
, screen
));
1562 XMoveResizeWindow(dpy
, barwin
, bx
, by
, bw
, bh
);
1574 /* window area geometry */
1580 /* update layout geometries */
1581 for(i
= 0; i
< LENGTH(layouts
); i
++)
1582 if(layouts
[i
].updategeom
)
1583 layouts
[i
].updategeom();
1587 updatesizehints(Client
*c
) {
1591 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1593 c
->flags
= size
.flags
;
1594 if(c
->flags
& PBaseSize
) {
1595 c
->basew
= size
.base_width
;
1596 c
->baseh
= size
.base_height
;
1598 else if(c
->flags
& PMinSize
) {
1599 c
->basew
= size
.min_width
;
1600 c
->baseh
= size
.min_height
;
1603 c
->basew
= c
->baseh
= 0;
1604 if(c
->flags
& PResizeInc
) {
1605 c
->incw
= size
.width_inc
;
1606 c
->inch
= size
.height_inc
;
1609 c
->incw
= c
->inch
= 0;
1610 if(c
->flags
& PMaxSize
) {
1611 c
->maxw
= size
.max_width
;
1612 c
->maxh
= size
.max_height
;
1615 c
->maxw
= c
->maxh
= 0;
1616 if(c
->flags
& PMinSize
) {
1617 c
->minw
= size
.min_width
;
1618 c
->minh
= size
.min_height
;
1620 else if(c
->flags
& PBaseSize
) {
1621 c
->minw
= size
.base_width
;
1622 c
->minh
= size
.base_height
;
1625 c
->minw
= c
->minh
= 0;
1626 if(c
->flags
& PAspect
) {
1627 c
->minax
= size
.min_aspect
.x
;
1628 c
->maxax
= size
.max_aspect
.x
;
1629 c
->minay
= size
.min_aspect
.y
;
1630 c
->maxay
= size
.max_aspect
.y
;
1633 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1634 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1635 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1639 updatetitle(Client
*c
) {
1640 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1641 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1645 updatewmhints(Client
*c
) {
1648 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1650 sel
->isurgent
= False
;
1652 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1658 view(const char *arg
) {
1659 seltags
^= 1; /* toggle sel tagset */
1660 memset(tagset
[seltags
], (NULL
== arg
), TAGSZ
);
1661 tagset
[seltags
][idxoftag(arg
)] = True
;
1666 viewprevtag(const char *arg
) {
1667 seltags
^= 1; /* toggle sel tagset */
1671 /* There's no way to check accesses to destroyed windows, thus those cases are
1672 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1673 * default error handler, which may call exit. */
1675 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1676 if(ee
->error_code
== BadWindow
1677 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1678 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1679 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1680 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1681 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1682 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1683 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1684 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1686 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1687 ee
->request_code
, ee
->error_code
);
1688 return xerrorxlib(dpy
, ee
); /* may call exit */
1692 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1696 /* Startup Error handler to check if another window manager
1697 * is already running. */
1699 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1705 main(int argc
, char *argv
[]) {
1706 if(argc
== 2 && !strcmp("-v", argv
[1]))
1707 eprint("dwm-"VERSION
", © 2006-2008 dwm engineers, see LICENSE for details\n");
1709 eprint("usage: dwm [-v]\n");
1711 setlocale(LC_CTYPE
, "");
1712 if(!(dpy
= XOpenDisplay(0)))
1713 eprint("dwm: cannot open display\n");