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>
37 #include <X11/cursorfont.h>
38 #include <X11/keysym.h>
39 #include <X11/Xatom.h>
41 #include <X11/Xproto.h>
42 #include <X11/Xutil.h>
44 #include <X11/extensions/Xinerama.h>
48 #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
49 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
50 #define LENGTH(x) (sizeof x / sizeof x[0])
52 #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
56 enum { BarTop
, BarBot
, BarOff
}; /* bar position */
57 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
58 enum { ColBorder
, ColFG
, ColBG
, ColLast
}; /* color */
59 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
60 enum { WMProtocols
, WMDelete
, WMName
, WMState
, WMLast
};/* default atoms */
63 typedef struct Client Client
;
67 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
68 int minax
, maxax
, minay
, maxay
;
70 unsigned int border
, oldborder
;
71 Bool isbanned
, isfixed
, isfloating
, isurgent
;
82 unsigned long norm
[ColLast
];
83 unsigned long sel
[ColLast
];
93 } DC
; /* draw context */
98 void (*func
)(const char *arg
);
104 void (*arrange
)(void);
121 int sx
, sy
, sw
, sh
, wax
, way
, wah
, waw
;
128 /* function declarations */
129 void applyrules(Client
*c
);
131 void attach(Client
*c
);
132 void attachstack(Client
*c
);
134 void buttonpress(XEvent
*e
);
135 void checkotherwm(void);
137 void compileregs(void);
138 void configure(Client
*c
);
139 void configurenotify(XEvent
*e
);
140 void configurerequest(XEvent
*e
);
141 void destroynotify(XEvent
*e
);
142 void detach(Client
*c
);
143 void detachstack(Client
*c
);
145 void drawsquare(Monitor
*, Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
146 void drawtext(Monitor
*, const char *text
, unsigned long col
[ColLast
], Bool invert
);
147 void *emallocz(unsigned int size
);
148 void enternotify(XEvent
*e
);
149 void eprint(const char *errstr
, ...);
150 void expose(XEvent
*e
);
151 void floating(void); /* default floating layout */
152 void focus(Client
*c
);
153 void focusin(XEvent
*e
);
154 void focusnext(const char *arg
);
155 void focusprev(const char *arg
);
156 Client
*getclient(Window w
);
157 unsigned long getcolor(const char *colstr
);
158 long getstate(Window w
);
159 Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
160 void grabbuttons(Client
*c
, Bool focused
);
162 unsigned int idxoftag(const char *tag
);
163 void initfont(const char *fontstr
);
164 Bool
isoccupied(unsigned int monitor
, unsigned int t
);
165 Bool
isprotodel(Client
*c
);
166 Bool
isurgent(unsigned int monitor
, unsigned int t
);
167 Bool
isvisible(Client
*c
, int monitor
);
168 void keypress(XEvent
*e
);
169 void killclient(const char *arg
);
170 void manage(Window w
, XWindowAttributes
*wa
);
171 void mappingnotify(XEvent
*e
);
172 void maprequest(XEvent
*e
);
173 void movemouse(Client
*c
);
174 Client
*nexttiled(Client
*c
, int monitor
);
175 void propertynotify(XEvent
*e
);
176 void quit(const char *arg
);
177 void reapply(const char *arg
);
178 void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
);
179 void resizemouse(Client
*c
);
183 void setclientstate(Client
*c
, long state
);
184 void setlayout(const char *arg
);
185 void setmwfact(const char *arg
);
187 void spawn(const char *arg
);
188 void tag(const char *arg
);
189 unsigned int textnw(const char *text
, unsigned int len
);
190 unsigned int textw(const char *text
);
192 void togglebar(const char *arg
);
193 void togglefloating(const char *arg
);
194 void toggletag(const char *arg
);
195 void toggleview(const char *arg
);
196 void unban(Client
*c
);
197 void unmanage(Client
*c
);
198 void unmapnotify(XEvent
*e
);
199 void updatebarpos(Monitor
*m
);
200 void updatesizehints(Client
*c
);
201 void updatetitle(Client
*c
);
202 void updatewmhints(Client
*c
);
203 void view(const char *arg
);
204 void viewprevtag(const char *arg
); /* views previous selected tags */
205 int xerror(Display
*dpy
, XErrorEvent
*ee
);
206 int xerrordummy(Display
*dsply
, XErrorEvent
*ee
);
207 int xerrorstart(Display
*dsply
, XErrorEvent
*ee
);
208 void zoom(const char *arg
);
210 void movetomonitor(const char *arg
);
211 void selectmonitor(const char *arg
);
218 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
219 unsigned int bh
, bpos
;
220 unsigned int blw
= 0;
221 unsigned int numlockmask
= 0;
222 void (*handler
[LASTEvent
]) (XEvent
*) = {
223 [ButtonPress
] = buttonpress
,
224 [ConfigureRequest
] = configurerequest
,
225 [ConfigureNotify
] = configurenotify
,
226 [DestroyNotify
] = destroynotify
,
227 [EnterNotify
] = enternotify
,
230 [KeyPress
] = keypress
,
231 [MappingNotify
] = mappingnotify
,
232 [MapRequest
] = maprequest
,
233 [PropertyNotify
] = propertynotify
,
234 [UnmapNotify
] = unmapnotify
236 Atom wmatom
[WMLast
], netatom
[NetLast
];
237 Bool isxinerama
= False
;
238 Bool domwfact
= True
;
240 Bool otherwm
, readin
;
242 Client
*clients
= NULL
;
244 Client
*stack
= NULL
;
245 Cursor cursor
[CurLast
];
252 /* configuration, allows nested code to access above variables */
255 //Bool prevtags[LENGTH(tags)];
257 /* function implementations */
259 applyrules(Client
*c
) {
260 static char buf
[512];
263 Bool matched_tag
= False
;
264 Bool matched_monitor
= False
;
265 XClassHint ch
= { 0 };
268 XGetClassHint(dpy
, c
->win
, &ch
);
269 snprintf(buf
, sizeof buf
, "%s:%s:%s",
270 ch
.res_class
? ch
.res_class
: "",
271 ch
.res_name
? ch
.res_name
: "", c
->name
);
272 for(i
= 0; i
< LENGTH(rules
); i
++)
273 if(regs
[i
].propregex
&& !regexec(regs
[i
].propregex
, buf
, 1, &tmp
, 0)) {
274 if (rules
[i
].monitor
>= 0 && rules
[i
].monitor
< mcount
) {
275 matched_monitor
= True
;
276 c
->monitor
= rules
[i
].monitor
;
279 c
->isfloating
= rules
[i
].isfloating
;
280 for(j
= 0; regs
[i
].tagregex
&& j
< LENGTH(tags
); j
++) {
281 if(!regexec(regs
[i
].tagregex
, tags
[j
], 1, &tmp
, 0)) {
292 memcpy(c
->tags
, monitors
[monitorat()].seltags
, sizeof initags
);
293 if (!matched_monitor
)
294 c
->monitor
= monitorat();
301 for(c
= clients
; c
; c
= c
->next
)
302 if(isvisible(c
, c
->monitor
))
307 monitors
[selmonitor
].layout
->arrange();
321 attachstack(Client
*c
) {
330 XMoveWindow(dpy
, c
->win
, c
->x
+ 3 * monitors
[c
->monitor
].sw
, c
->y
);
335 buttonpress(XEvent
*e
) {
338 XButtonPressedEvent
*ev
= &e
->xbutton
;
340 Monitor
*m
= &monitors
[monitorat()];
342 if(ev
->window
== m
->barwin
) {
344 for(i
= 0; i
< LENGTH(tags
); i
++) {
347 if(ev
->button
== Button1
) {
348 if(ev
->state
& MODKEY
)
353 else if(ev
->button
== Button3
) {
354 if(ev
->state
& MODKEY
)
362 if((ev
->x
< x
+ blw
) && ev
->button
== Button1
)
365 else if((c
= getclient(ev
->window
))) {
367 if(CLEANMASK(ev
->state
) != MODKEY
)
369 if(ev
->button
== Button1
) {
373 else if(ev
->button
== Button2
) {
374 if((floating
!= m
->layout
->arrange
) && c
->isfloating
)
375 togglefloating(NULL
);
379 else if(ev
->button
== Button3
&& !c
->isfixed
) {
389 XSetErrorHandler(xerrorstart
);
391 /* this causes an error if some other window manager is running */
392 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
395 eprint("dwm: another window manager is already running\n");
397 XSetErrorHandler(NULL
);
398 xerrorxlib
= XSetErrorHandler(xerror
);
411 XFreeFontSet(dpy
, dc
.font
.set
);
413 XFreeFont(dpy
, dc
.font
.xfont
);
415 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
416 XFreePixmap(dpy
, dc
.drawable
);
418 XFreeCursor(dpy
, cursor
[CurNormal
]);
419 XFreeCursor(dpy
, cursor
[CurResize
]);
420 XFreeCursor(dpy
, cursor
[CurMove
]);
421 for(i
= 0; i
< mcount
; i
++)
422 XDestroyWindow(dpy
, monitors
[i
].barwin
);
424 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
434 regs
= emallocz(LENGTH(rules
) * sizeof(Regs
));
435 for(i
= 0; i
< LENGTH(rules
); i
++) {
437 reg
= emallocz(sizeof(regex_t
));
438 if(regcomp(reg
, rules
[i
].prop
, REG_EXTENDED
))
441 regs
[i
].propregex
= reg
;
444 reg
= emallocz(sizeof(regex_t
));
445 if(regcomp(reg
, rules
[i
].tags
, REG_EXTENDED
))
448 regs
[i
].tagregex
= reg
;
454 configure(Client
*c
) {
457 ce
.type
= ConfigureNotify
;
465 ce
.border_width
= c
->border
;
467 ce
.override_redirect
= False
;
468 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
472 configurenotify(XEvent
*e
) {
473 XConfigureEvent
*ev
= &e
->xconfigure
;
474 Monitor
*m
= &monitors
[selmonitor
];
476 if(ev
->window
== root
&& (ev
->width
!= m
->sw
|| ev
->height
!= m
->sh
)) {
477 /* TODO -- update Xinerama dimensions here */
480 XFreePixmap(dpy
, dc
.drawable
);
481 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(root
, screen
), bh
, DefaultDepth(dpy
, screen
));
482 XResizeWindow(dpy
, m
->barwin
, m
->sw
, bh
);
489 configurerequest(XEvent
*e
) {
491 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
494 if((c
= getclient(ev
->window
))) {
495 Monitor
*m
= &monitors
[c
->monitor
];
496 if(ev
->value_mask
& CWBorderWidth
)
497 c
->border
= ev
->border_width
;
498 if(c
->isfixed
|| c
->isfloating
|| (floating
== m
->layout
->arrange
)) {
499 if(ev
->value_mask
& CWX
)
501 if(ev
->value_mask
& CWY
)
503 if(ev
->value_mask
& CWWidth
)
505 if(ev
->value_mask
& CWHeight
)
507 if((c
->x
- m
->sx
+ c
->w
) > m
->sw
&& c
->isfloating
)
508 c
->x
= m
->sx
+ (m
->sw
/ 2 - c
->w
/ 2); /* center in x direction */
509 if((c
->y
- m
->sy
+ c
->h
) > m
->sh
&& c
->isfloating
)
510 c
->y
= m
->sy
+ (m
->sh
/ 2 - c
->h
/ 2); /* center in y direction */
511 if((ev
->value_mask
& (CWX
| CWY
))
512 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
514 if(isvisible(c
, monitorat()))
515 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
523 wc
.width
= ev
->width
;
524 wc
.height
= ev
->height
;
525 wc
.border_width
= ev
->border_width
;
526 wc
.sibling
= ev
->above
;
527 wc
.stack_mode
= ev
->detail
;
528 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
534 destroynotify(XEvent
*e
) {
536 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
538 if((c
= getclient(ev
->window
)))
545 c
->prev
->next
= c
->next
;
547 c
->next
->prev
= c
->prev
;
550 c
->next
= c
->prev
= NULL
;
554 detachstack(Client
*c
) {
557 for(tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
566 for(i
= 0; i
< mcount
; i
++) {
567 Monitor
*m
= &monitors
[i
];
569 for(c
= stack
; c
&& !isvisible(c
, i
); c
= c
->snext
);
570 fprintf(stderr
, "m%d %s\n", i
, c
? c
->name
: "NIL");
571 for(j
= 0; j
< LENGTH(tags
); j
++) {
572 dc
.w
= textw(tags
[j
]);
574 drawtext(m
, tags
[j
], dc
.sel
, isurgent(i
, j
));
575 drawsquare(m
, c
&& c
->tags
[j
] && c
->monitor
== i
,
576 isoccupied(i
, j
), isurgent(i
, j
), dc
.sel
);
579 drawtext(m
, tags
[j
], dc
.norm
, isurgent(i
, j
));
580 drawsquare(m
, c
&& c
->tags
[j
] && c
->monitor
== i
,
581 isoccupied(i
, j
), isurgent(i
, j
), dc
.norm
);
586 drawtext(m
, m
->layout
->symbol
, dc
.norm
, False
);
588 if(i
== selmonitor
) {
595 drawtext(m
, stext
, dc
.norm
, False
);
599 if((dc
.w
= dc
.x
- x
) > bh
) {
602 drawtext(m
, c
->name
, dc
.sel
, False
);
603 drawsquare(m
, False
, c
->isfloating
, False
, dc
.sel
);
606 drawtext(m
, NULL
, dc
.norm
, False
);
608 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->sw
, bh
, 0, 0);
614 drawsquare(Monitor
*m
, Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
617 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
619 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
620 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
621 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
625 r
.width
= r
.height
= x
+ 1;
626 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
629 r
.width
= r
.height
= x
;
630 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
635 drawtext(Monitor
*m
, const char *text
, unsigned long col
[ColLast
], Bool invert
) {
637 static char buf
[256];
638 unsigned int len
, olen
;
639 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
641 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
642 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
646 olen
= len
= strlen(text
);
647 if(len
>= sizeof buf
)
648 len
= sizeof buf
- 1;
649 memcpy(buf
, text
, len
);
651 h
= dc
.font
.ascent
+ dc
.font
.descent
;
652 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
654 /* shorten text if necessary */
655 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
666 return; /* too long */
667 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
669 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
671 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
675 emallocz(unsigned int size
) {
676 void *res
= calloc(1, size
);
679 eprint("fatal: could not malloc() %u bytes\n", size
);
684 enternotify(XEvent
*e
) {
686 XCrossingEvent
*ev
= &e
->xcrossing
;
688 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) {
689 if(!isxinerama
|| ev
->window
!= root
)
692 if((c
= getclient(ev
->window
)))
695 selmonitor
= monitorat();
696 fprintf(stderr
, "updating selmonitor %d\n", selmonitor
);
702 eprint(const char *errstr
, ...) {
705 va_start(ap
, errstr
);
706 vfprintf(stderr
, errstr
, ap
);
713 XExposeEvent
*ev
= &e
->xexpose
;
716 if(ev
->window
== monitors
[selmonitor
].barwin
)
722 floating(void) { /* default floating layout */
725 domwfact
= dozoom
= False
;
726 for(c
= clients
; c
; c
= c
->next
)
727 if(isvisible(c
, selmonitor
))
728 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
736 selmonitor
= c
->monitor
;
737 m
= &monitors
[selmonitor
];
738 if(!c
|| (c
&& !isvisible(c
, selmonitor
)))
739 for(c
= stack
; c
&& !isvisible(c
, c
->monitor
); c
= c
->snext
);
740 if(sel
&& sel
!= c
) {
741 grabbuttons(sel
, False
);
742 XSetWindowBorder(dpy
, sel
->win
, dc
.norm
[ColBorder
]);
747 grabbuttons(c
, True
);
752 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
753 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
754 selmonitor
= c
->monitor
;
757 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
763 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
764 XFocusChangeEvent
*ev
= &e
->xfocus
;
766 if(sel
&& ev
->window
!= sel
->win
)
767 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
771 focusnext(const char *arg
) {
776 for(c
= sel
->next
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
778 for(c
= clients
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
786 focusprev(const char *arg
) {
791 for(c
= sel
->prev
; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
793 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
794 for(; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
803 getclient(Window w
) {
806 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
811 getcolor(const char *colstr
) {
812 Colormap cmap
= DefaultColormap(dpy
, screen
);
815 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
816 eprint("error, cannot allocate color '%s'\n", colstr
);
824 unsigned char *p
= NULL
;
825 unsigned long n
, extra
;
828 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
829 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
830 if(status
!= Success
)
839 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
844 if(!text
|| size
== 0)
847 XGetTextProperty(dpy
, w
, &name
, atom
);
850 if(name
.encoding
== XA_STRING
)
851 strncpy(text
, (char *)name
.value
, size
- 1);
853 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
855 strncpy(text
, *list
, size
- 1);
856 XFreeStringList(list
);
859 text
[size
- 1] = '\0';
865 grabbuttons(Client
*c
, Bool focused
) {
866 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
869 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
870 GrabModeAsync
, GrabModeSync
, None
, None
);
871 XGrabButton(dpy
, Button1
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
872 GrabModeAsync
, GrabModeSync
, None
, None
);
873 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
874 GrabModeAsync
, GrabModeSync
, None
, None
);
875 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
876 GrabModeAsync
, GrabModeSync
, None
, None
);
878 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
879 GrabModeAsync
, GrabModeSync
, None
, None
);
880 XGrabButton(dpy
, Button2
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
881 GrabModeAsync
, GrabModeSync
, None
, None
);
882 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
883 GrabModeAsync
, GrabModeSync
, None
, None
);
884 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
885 GrabModeAsync
, GrabModeSync
, None
, None
);
887 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
888 GrabModeAsync
, GrabModeSync
, None
, None
);
889 XGrabButton(dpy
, Button3
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
890 GrabModeAsync
, GrabModeSync
, None
, None
);
891 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
892 GrabModeAsync
, GrabModeSync
, None
, None
);
893 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
894 GrabModeAsync
, GrabModeSync
, None
, None
);
897 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
898 GrabModeAsync
, GrabModeSync
, None
, None
);
905 XModifierKeymap
*modmap
;
907 /* init modifier map */
908 modmap
= XGetModifierMapping(dpy
);
909 for(i
= 0; i
< 8; i
++)
910 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
911 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
912 numlockmask
= (1 << i
);
914 XFreeModifiermap(modmap
);
916 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
917 for(i
= 0; i
< LENGTH(keys
); i
++) {
918 code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
);
919 XGrabKey(dpy
, code
, keys
[i
].mod
, root
, True
,
920 GrabModeAsync
, GrabModeAsync
);
921 XGrabKey(dpy
, code
, keys
[i
].mod
| LockMask
, root
, True
,
922 GrabModeAsync
, GrabModeAsync
);
923 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
, root
, True
,
924 GrabModeAsync
, GrabModeAsync
);
925 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
| LockMask
, root
, True
,
926 GrabModeAsync
, GrabModeAsync
);
931 idxoftag(const char *tag
) {
934 for(i
= 0; (i
< LENGTH(tags
)) && (tags
[i
] != tag
); i
++);
935 return (i
< LENGTH(tags
)) ? i
: 0;
939 initfont(const char *fontstr
) {
940 char *def
, **missing
;
945 XFreeFontSet(dpy
, dc
.font
.set
);
946 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
949 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
950 XFreeStringList(missing
);
953 XFontSetExtents
*font_extents
;
954 XFontStruct
**xfonts
;
956 dc
.font
.ascent
= dc
.font
.descent
= 0;
957 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
958 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
959 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
960 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
961 dc
.font
.ascent
= (*xfonts
)->ascent
;
962 if(dc
.font
.descent
< (*xfonts
)->descent
)
963 dc
.font
.descent
= (*xfonts
)->descent
;
969 XFreeFont(dpy
, dc
.font
.xfont
);
970 dc
.font
.xfont
= NULL
;
971 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
972 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
973 eprint("error, cannot load font: '%s'\n", fontstr
);
974 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
975 dc
.font
.descent
= dc
.font
.xfont
->descent
;
977 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
981 isoccupied(unsigned int monitor
, unsigned int t
) {
984 for(c
= clients
; c
; c
= c
->next
)
985 if(c
->tags
[t
] && c
->monitor
== monitor
)
991 isprotodel(Client
*c
) {
996 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
997 for(i
= 0; !ret
&& i
< n
; i
++)
998 if(protocols
[i
] == wmatom
[WMDelete
])
1006 isurgent(unsigned int monitor
, unsigned int t
) {
1009 for(c
= clients
; c
; c
= c
->next
)
1010 if(c
->monitor
== monitor
&& c
->isurgent
&& c
->tags
[t
])
1016 isvisible(Client
*c
, int monitor
) {
1019 if(c
->monitor
!= monitor
)
1021 for(i
= 0; i
< LENGTH(tags
); i
++)
1022 if(c
->tags
[i
] && monitors
[c
->monitor
].seltags
[i
])
1028 keypress(XEvent
*e
) {
1034 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1035 for(i
= 0; i
< LENGTH(keys
); i
++)
1036 if(keysym
== keys
[i
].keysym
1037 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
1040 keys
[i
].func(keys
[i
].arg
);
1045 killclient(const char *arg
) {
1050 if(isprotodel(sel
)) {
1051 ev
.type
= ClientMessage
;
1052 ev
.xclient
.window
= sel
->win
;
1053 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1054 ev
.xclient
.format
= 32;
1055 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1056 ev
.xclient
.data
.l
[1] = CurrentTime
;
1057 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
1060 XKillClient(dpy
, sel
->win
);
1064 manage(Window w
, XWindowAttributes
*wa
) {
1065 Client
*c
, *t
= NULL
;
1071 c
= emallocz(sizeof(Client
));
1072 c
->tags
= emallocz(sizeof initags
);
1077 m
= &monitors
[c
->monitor
];
1079 c
->x
= wa
->x
+ m
->sx
;
1080 c
->y
= wa
->y
+ m
->sy
;
1083 c
->oldborder
= wa
->border_width
;
1085 if(c
->w
== m
->sw
&& c
->h
== m
->sh
) {
1088 c
->border
= wa
->border_width
;
1091 if(c
->x
+ c
->w
+ 2 * c
->border
> m
->wax
+ m
->waw
)
1092 c
->x
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1093 if(c
->y
+ c
->h
+ 2 * c
->border
> m
->way
+ m
->wah
)
1094 c
->y
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1099 c
->border
= BORDERPX
;
1101 wc
.border_width
= c
->border
;
1102 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1103 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1104 configure(c
); /* propagates border_width, if size doesn't change */
1106 XSelectInput(dpy
, w
, EnterWindowMask
| FocusChangeMask
| PropertyChangeMask
| StructureNotifyMask
);
1107 grabbuttons(c
, False
);
1109 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
1110 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
1112 memcpy(c
->tags
, t
->tags
, sizeof initags
);
1114 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
1117 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1119 XMapWindow(dpy
, c
->win
);
1120 setclientstate(c
, NormalState
);
1125 mappingnotify(XEvent
*e
) {
1126 XMappingEvent
*ev
= &e
->xmapping
;
1128 XRefreshKeyboardMapping(ev
);
1129 if(ev
->request
== MappingKeyboard
)
1134 maprequest(XEvent
*e
) {
1135 static XWindowAttributes wa
;
1136 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1138 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1140 if(wa
.override_redirect
)
1142 if(!getclient(ev
->window
))
1143 manage(ev
->window
, &wa
);
1152 XQueryPointer(dpy
, root
, &win
, &win
, &x
, &y
, &i
, &i
, &mask
);
1153 for(i
= 0; i
< mcount
; i
++) {
1154 if((x
>= monitors
[i
].sx
&& x
< monitors
[i
].sx
+ monitors
[i
].sw
)
1155 && (y
>= monitors
[i
].sy
&& y
< monitors
[i
].sy
+ monitors
[i
].sh
)) {
1163 movemouse(Client
*c
) {
1164 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1172 m
= &monitors
[c
->monitor
];
1173 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1174 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1176 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1178 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1181 XUngrabPointer(dpy
, CurrentTime
);
1183 case ConfigureRequest
:
1186 handler
[ev
.type
](&ev
);
1190 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1191 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1192 if(abs(m
->wax
- nx
) < SNAP
)
1194 else if(abs((m
->wax
+ m
->waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
1195 nx
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1196 if(abs(m
->way
- ny
) < SNAP
)
1198 else if(abs((m
->way
+ m
->wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
1199 ny
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1200 if((m
->layout
->arrange
!= floating
) && (abs(nx
- c
->x
) > SNAP
|| abs(ny
- c
->y
) > SNAP
))
1201 togglefloating(NULL
);
1202 if((m
->layout
->arrange
== floating
) || c
->isfloating
)
1203 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1210 nexttiled(Client
*c
, int monitor
) {
1211 for(; c
&& (c
->isfloating
|| !isvisible(c
, monitor
)); c
= c
->next
);
1216 propertynotify(XEvent
*e
) {
1219 XPropertyEvent
*ev
= &e
->xproperty
;
1221 if(ev
->state
== PropertyDelete
)
1222 return; /* ignore */
1223 if((c
= getclient(ev
->window
))) {
1226 case XA_WM_TRANSIENT_FOR
:
1227 XGetTransientForHint(dpy
, c
->win
, &trans
);
1228 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1231 case XA_WM_NORMAL_HINTS
:
1239 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1248 quit(const char *arg
) {
1249 readin
= running
= False
;
1253 reapply(const char *arg
) {
1254 static Bool zerotags
[LENGTH(tags
)] = { 0 };
1257 for(c
= clients
; c
; c
= c
->next
) {
1258 memcpy(c
->tags
, zerotags
, sizeof zerotags
);
1265 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1269 m
= &monitors
[c
->monitor
];
1272 /* set minimum possible */
1278 /* temporarily remove base dimensions */
1282 /* adjust for aspect limits */
1283 if (c
->minay
> 0 && c
->maxay
> 0 && c
->minax
> 0 && c
->maxax
> 0) {
1284 if (w
* c
->maxay
> h
* c
->maxax
)
1285 w
= h
* c
->maxax
/ c
->maxay
;
1286 else if (w
* c
->minay
< h
* c
->minax
)
1287 h
= w
* c
->minay
/ c
->minax
;
1290 /* adjust for increment value */
1296 /* restore base dimensions */
1300 if(c
->minw
> 0 && w
< c
->minw
)
1302 if(c
->minh
> 0 && h
< c
->minh
)
1304 if(c
->maxw
> 0 && w
> c
->maxw
)
1306 if(c
->maxh
> 0 && h
> c
->maxh
)
1309 if(w
<= 0 || h
<= 0)
1312 x
= m
->sw
- w
- 2 * c
->border
;
1314 y
= m
->sh
- h
- 2 * c
->border
;
1315 if(x
+ w
+ 2 * c
->border
< m
->sx
)
1317 if(y
+ h
+ 2 * c
->border
< m
->sy
)
1319 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1322 c
->w
= wc
.width
= w
;
1323 c
->h
= wc
.height
= h
;
1324 wc
.border_width
= c
->border
;
1325 XConfigureWindow(dpy
, c
->win
, CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
, &wc
);
1332 resizemouse(Client
*c
) {
1340 m
= &monitors
[c
->monitor
];
1341 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1342 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1344 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1346 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1349 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1350 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1351 XUngrabPointer(dpy
, CurrentTime
);
1352 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1354 case ConfigureRequest
:
1357 handler
[ev
.type
](&ev
);
1361 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
1363 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
1365 if((m
->layout
->arrange
!= floating
) && (abs(nw
- c
->w
) > SNAP
|| abs(nh
- c
->h
) > SNAP
))
1366 togglefloating(NULL
);
1367 if((m
->layout
->arrange
== floating
) || c
->isfloating
)
1368 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1384 if(sel
->isfloating
|| (monitors
[selmonitor
].layout
->arrange
== floating
))
1385 XRaiseWindow(dpy
, sel
->win
);
1386 if(monitors
[selmonitor
].layout
->arrange
!= floating
) {
1387 wc
.stack_mode
= Below
;
1388 wc
.sibling
= monitors
[selmonitor
].barwin
;
1389 if(!sel
->isfloating
) {
1390 XConfigureWindow(dpy
, sel
->win
, CWSibling
| CWStackMode
, &wc
);
1391 wc
.sibling
= sel
->win
;
1393 for(i
= 0; i
< mcount
; i
++) {
1394 for(c
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
)) {
1397 XConfigureWindow(dpy
, c
->win
, CWSibling
| CWStackMode
, &wc
);
1398 wc
.sibling
= c
->win
;
1403 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1409 char buf
[sizeof stext
];
1412 unsigned int len
, offset
;
1415 /* main event loop, also reads status text from stdin */
1417 xfd
= ConnectionNumber(dpy
);
1420 len
= sizeof stext
- 1;
1421 buf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1425 FD_SET(STDIN_FILENO
, &rd
);
1427 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1430 eprint("select failed\n");
1432 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1433 switch((r
= read(STDIN_FILENO
, buf
+ offset
, len
- offset
))) {
1435 strncpy(stext
, strerror(errno
), len
);
1439 strncpy(stext
, "EOF", 4);
1443 for(p
= buf
+ offset
; r
> 0; p
++, r
--, offset
++)
1444 if(*p
== '\n' || *p
== '\0') {
1446 strncpy(stext
, buf
, len
);
1447 p
+= r
- 1; /* p is buf + offset + r - 1 */
1448 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1451 memmove(buf
, p
- r
+ 1, r
);
1458 while(XPending(dpy
)) {
1459 XNextEvent(dpy
, &ev
);
1460 if(handler
[ev
.type
])
1461 (handler
[ev
.type
])(&ev
); /* call handler */
1468 unsigned int i
, num
;
1469 Window
*wins
, d1
, d2
;
1470 XWindowAttributes wa
;
1473 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1474 for(i
= 0; i
< num
; i
++) {
1475 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1476 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1478 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1479 manage(wins
[i
], &wa
);
1481 for(i
= 0; i
< num
; i
++) { /* now the transients */
1482 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1484 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1485 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1486 manage(wins
[i
], &wa
);
1494 setclientstate(Client
*c
, long state
) {
1495 long data
[] = {state
, None
};
1497 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1498 PropModeReplace
, (unsigned char *)data
, 2);
1502 setlayout(const char *arg
) {
1504 Monitor
*m
= &monitors
[monitorat()];
1508 if(m
->layout
== &layouts
[LENGTH(layouts
)])
1509 m
->layout
= &layouts
[0];
1512 for(i
= 0; i
< LENGTH(layouts
); i
++)
1513 if(!strcmp(arg
, layouts
[i
].symbol
))
1515 if(i
== LENGTH(layouts
))
1517 m
->layout
= &layouts
[i
];
1526 setmwfact(const char *arg
) {
1529 Monitor
*m
= &monitors
[monitorat()];
1533 /* arg handling, manipulate mwfact */
1536 else if(sscanf(arg
, "%lf", &delta
) == 1) {
1537 if(arg
[0] == '+' || arg
[0] == '-')
1543 else if(m
->mwfact
> 0.9)
1553 XSetWindowAttributes wa
;
1554 XineramaScreenInfo
*info
= NULL
;
1557 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1558 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1559 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1560 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1561 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1562 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1565 wa
.cursor
= cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1566 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1567 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1569 // init screens/monitors first
1571 if((isxinerama
= XineramaIsActive(dpy
)))
1572 info
= XineramaQueryScreens(dpy
, &mcount
);
1573 monitors
= emallocz(mcount
* sizeof(Monitor
));
1575 screen
= DefaultScreen(dpy
);
1576 root
= RootWindow(dpy
, screen
);
1578 /* init appearance */
1579 dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
);
1580 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
1581 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
1582 dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
);
1583 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
1584 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
1586 dc
.h
= bh
= dc
.font
.height
+ 2;
1587 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1588 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1589 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1591 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1593 for(blw
= i
= 0; i
< LENGTH(layouts
); i
++) {
1594 i
= textw(layouts
[i
].symbol
);
1598 for(i
= 0; i
< mcount
; i
++) {
1602 if (mcount
!= 1 && isxinerama
) {
1603 m
->sx
= info
[i
].x_org
;
1604 m
->sy
= info
[i
].y_org
;
1605 m
->sw
= info
[i
].width
;
1606 m
->sh
= info
[i
].height
;
1607 fprintf(stderr
, "monitor[%d]: %d,%d,%d,%d\n", i
, m
->sx
, m
->sy
, m
->sw
, m
->sh
);
1612 m
->sw
= DisplayWidth(dpy
, screen
);
1613 m
->sh
= DisplayHeight(dpy
, screen
);
1616 m
->seltags
= emallocz(sizeof initags
);
1617 m
->prevtags
= emallocz(sizeof initags
);
1619 memcpy(m
->seltags
, initags
, sizeof initags
);
1620 memcpy(m
->prevtags
, initags
, sizeof initags
);
1624 m
->layout
= &layouts
[0];
1626 // TODO: bpos per screen?
1628 wa
.override_redirect
= 1;
1629 wa
.background_pixmap
= ParentRelative
;
1630 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
1633 m
->barwin
= XCreateWindow(dpy
, root
, m
->sx
, m
->sy
, m
->sw
, bh
, 0,
1634 DefaultDepth(dpy
, screen
), CopyFromParent
, DefaultVisual(dpy
, screen
),
1635 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
1636 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1638 XMapRaised(dpy
, m
->barwin
);
1639 strcpy(stext
, "dwm-"VERSION
);
1641 /* EWMH support per monitor */
1642 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1643 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1645 /* select for events */
1646 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
1647 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
1648 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
1649 XSelectInput(dpy
, root
, wa
.event_mask
);
1660 selmonitor
= monitorat();
1661 fprintf(stderr
, "selmonitor == %d\n", selmonitor
);
1665 spawn(const char *arg
) {
1666 static char *shell
= NULL
;
1668 if(!shell
&& !(shell
= getenv("SHELL")))
1672 /* The double-fork construct avoids zombie processes and keeps the code
1673 * clean from stupid signal handlers. */
1677 close(ConnectionNumber(dpy
));
1679 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1680 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1689 tag(const char *arg
) {
1694 for(i
= 0; i
< LENGTH(tags
); i
++)
1695 sel
->tags
[i
] = (NULL
== arg
);
1696 sel
->tags
[idxoftag(arg
)] = True
;
1701 textnw(const char *text
, unsigned int len
) {
1705 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1708 return XTextWidth(dc
.font
.xfont
, text
, len
);
1712 textw(const char *text
) {
1713 return textnw(text
, strlen(text
)) + dc
.font
.height
;
1718 unsigned int i
, j
, n
, nx
, ny
, nw
, nh
, mw
, th
;
1721 domwfact
= dozoom
= True
;
1723 nx
= ny
= nw
= 0; /* gcc stupidity requires this */
1725 for (i
= 0; i
< mcount
; i
++) {
1726 Monitor
*m
= &monitors
[i
];
1728 for(n
= 0, c
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
))
1732 mw
= (n
== 1) ? m
->waw
: m
->mwfact
* m
->waw
;
1733 th
= (n
> 1) ? m
->wah
/ (n
- 1) : 0;
1734 if(n
> 1 && th
< bh
)
1737 for(j
= 0, c
= mc
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
)) {
1738 if(j
== 0) { /* master */
1741 nw
= mw
- 2 * c
->border
;
1742 nh
= m
->wah
- 2 * c
->border
;
1744 else { /* tile window */
1747 nx
+= mc
->w
+ 2 * mc
->border
;
1748 nw
= m
->waw
- mw
- 2 * c
->border
;
1750 if(j
+ 1 == n
) /* remainder */
1751 nh
= (m
->way
+ m
->wah
) - ny
- 2 * c
->border
;
1753 nh
= th
- 2 * c
->border
;
1755 fprintf(stderr
, "tile(%d, %d, %d, %d)\n", nx
, ny
, nw
, nh
);
1756 resize(c
, nx
, ny
, nw
, nh
, RESIZEHINTS
);
1757 if((RESIZEHINTS
) && ((c
->h
< bh
) || (c
->h
> nh
) || (c
->w
< bh
) || (c
->w
> nw
)))
1758 /* client doesn't accept size constraints */
1759 resize(c
, nx
, ny
, nw
, nh
, False
);
1760 if(n
> 1 && th
!= m
->wah
)
1761 ny
= c
->y
+ c
->h
+ 2 * c
->border
;
1766 fprintf(stderr
, "done\n");
1769 togglebar(const char *arg
) {
1771 bpos
= (BARPOS
== BarOff
) ? BarTop
: BARPOS
;
1774 updatebarpos(&monitors
[monitorat()]);
1779 togglefloating(const char *arg
) {
1782 sel
->isfloating
= !sel
->isfloating
;
1784 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1789 toggletag(const char *arg
) {
1795 sel
->tags
[i
] = !sel
->tags
[i
];
1796 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1797 if(j
== LENGTH(tags
))
1798 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1803 toggleview(const char *arg
) {
1806 Monitor
*m
= &monitors
[monitorat()];
1809 m
->seltags
[i
] = !m
->seltags
[i
];
1810 for(j
= 0; j
< LENGTH(tags
) && !m
->seltags
[j
]; j
++);
1811 if(j
== LENGTH(tags
))
1812 m
->seltags
[i
] = True
; /* at least one tag must be viewed */
1820 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1821 c
->isbanned
= False
;
1825 unmanage(Client
*c
) {
1828 wc
.border_width
= c
->oldborder
;
1829 /* The server grab construct avoids race conditions. */
1831 XSetErrorHandler(xerrordummy
);
1832 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1837 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1838 setclientstate(c
, WithdrawnState
);
1842 XSetErrorHandler(xerror
);
1848 unmapnotify(XEvent
*e
) {
1850 XUnmapEvent
*ev
= &e
->xunmap
;
1852 if((c
= getclient(ev
->window
)))
1857 updatebarpos(Monitor
*m
) {
1868 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
);
1872 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
+ m
->wah
);
1875 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
- bh
);
1879 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1883 updatesizehints(Client
*c
) {
1887 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1889 c
->flags
= size
.flags
;
1890 if(c
->flags
& PBaseSize
) {
1891 c
->basew
= size
.base_width
;
1892 c
->baseh
= size
.base_height
;
1894 else if(c
->flags
& PMinSize
) {
1895 c
->basew
= size
.min_width
;
1896 c
->baseh
= size
.min_height
;
1899 c
->basew
= c
->baseh
= 0;
1900 if(c
->flags
& PResizeInc
) {
1901 c
->incw
= size
.width_inc
;
1902 c
->inch
= size
.height_inc
;
1905 c
->incw
= c
->inch
= 0;
1906 if(c
->flags
& PMaxSize
) {
1907 c
->maxw
= size
.max_width
;
1908 c
->maxh
= size
.max_height
;
1911 c
->maxw
= c
->maxh
= 0;
1912 if(c
->flags
& PMinSize
) {
1913 c
->minw
= size
.min_width
;
1914 c
->minh
= size
.min_height
;
1916 else if(c
->flags
& PBaseSize
) {
1917 c
->minw
= size
.base_width
;
1918 c
->minh
= size
.base_height
;
1921 c
->minw
= c
->minh
= 0;
1922 if(c
->flags
& PAspect
) {
1923 c
->minax
= size
.min_aspect
.x
;
1924 c
->maxax
= size
.max_aspect
.x
;
1925 c
->minay
= size
.min_aspect
.y
;
1926 c
->maxay
= size
.max_aspect
.y
;
1929 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1930 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1931 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1935 updatetitle(Client
*c
) {
1936 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1937 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1941 updatewmhints(Client
*c
) {
1944 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1945 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1950 /* There's no way to check accesses to destroyed windows, thus those cases are
1951 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1952 * default error handler, which may call exit. */
1954 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1955 if(ee
->error_code
== BadWindow
1956 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1957 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1958 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1959 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1960 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1961 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1962 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1964 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1965 ee
->request_code
, ee
->error_code
);
1966 return xerrorxlib(dpy
, ee
); /* may call exit */
1970 xerrordummy(Display
*dsply
, XErrorEvent
*ee
) {
1974 /* Startup Error handler to check if another window manager
1975 * is already running. */
1977 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
1983 view(const char *arg
) {
1985 Bool tmp
[LENGTH(tags
)];
1986 Monitor
*m
= &monitors
[monitorat()];
1988 for(i
= 0; i
< LENGTH(tags
); i
++)
1989 tmp
[i
] = (NULL
== arg
);
1990 tmp
[idxoftag(arg
)] = True
;
1991 if(memcmp(m
->seltags
, tmp
, sizeof initags
) != 0) {
1992 memcpy(m
->prevtags
, m
->seltags
, sizeof initags
);
1993 memcpy(m
->seltags
, tmp
, sizeof initags
);
1999 viewprevtag(const char *arg
) {
2000 static Bool tmp
[LENGTH(tags
)];
2002 Monitor
*m
= &monitors
[monitorat()];
2004 memcpy(tmp
, m
->seltags
, sizeof initags
);
2005 memcpy(m
->seltags
, m
->prevtags
, sizeof initags
);
2006 memcpy(m
->prevtags
, tmp
, sizeof initags
);
2011 zoom(const char *arg
) {
2014 if(!sel
|| !dozoom
|| sel
->isfloating
)
2016 if((c
= sel
) == nexttiled(clients
, c
->monitor
))
2017 if(!(c
= nexttiled(c
->next
, c
->monitor
)))
2026 movetomonitor(const char *arg
) {
2028 sel
->monitor
= arg
? atoi(arg
) : (sel
->monitor
+1) % mcount
;
2030 memcpy(sel
->tags
, monitors
[sel
->monitor
].seltags
, sizeof initags
);
2031 resize(sel
, monitors
[sel
->monitor
].wax
, monitors
[sel
->monitor
].way
, sel
->w
, sel
->h
, True
);
2037 selectmonitor(const char *arg
) {
2038 Monitor
*m
= &monitors
[arg
? atoi(arg
) : (monitorat()+1) % mcount
];
2040 XWarpPointer(dpy
, None
, root
, 0, 0, 0, 0, m
->wax
+m
->waw
/2, m
->way
+m
->wah
/2);
2046 main(int argc
, char *argv
[]) {
2047 if(argc
== 2 && !strcmp("-v", argv
[1]))
2048 eprint("dwm-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
2049 "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
2051 eprint("usage: dwm [-v]\n");
2053 setlocale(LC_CTYPE
, "");
2054 if(!(dpy
= XOpenDisplay(0)))
2055 eprint("dwm: cannot open display\n");