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);
122 int sx
, sy
, sw
, sh
, wax
, way
, wah
, waw
;
129 /* function declarations */
130 void applyrules(Client
*c
);
132 void attach(Client
*c
);
133 void attachstack(Client
*c
);
135 void buttonpress(XEvent
*e
);
136 void checkotherwm(void);
138 void compileregs(void);
139 void configure(Client
*c
);
140 void configurenotify(XEvent
*e
);
141 void configurerequest(XEvent
*e
);
142 void destroynotify(XEvent
*e
);
143 void detach(Client
*c
);
144 void detachstack(Client
*c
);
146 void drawsquare(Monitor
*, Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
147 void drawtext(Monitor
*, const char *text
, unsigned long col
[ColLast
], Bool invert
);
148 void *emallocz(unsigned int size
);
149 void enternotify(XEvent
*e
);
150 void eprint(const char *errstr
, ...);
151 void expose(XEvent
*e
);
152 void floating(void); /* default floating layout */
153 void focus(Client
*c
);
154 void focusin(XEvent
*e
);
155 void focusnext(const char *arg
);
156 void focusprev(const char *arg
);
157 Client
*getclient(Window w
);
158 unsigned long getcolor(const char *colstr
);
159 long getstate(Window w
);
160 Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
161 void grabbuttons(Client
*c
, Bool focused
);
163 unsigned int idxoftag(const char *tag
);
164 void initfont(const char *fontstr
);
165 Bool
isoccupied(unsigned int monitor
, unsigned int t
);
166 Bool
isprotodel(Client
*c
);
167 Bool
isurgent(unsigned int monitor
, unsigned int t
);
168 Bool
isvisible(Client
*c
, int monitor
);
169 void keypress(XEvent
*e
);
170 void killclient(const char *arg
);
171 void manage(Window w
, XWindowAttributes
*wa
);
172 void mappingnotify(XEvent
*e
);
173 void maprequest(XEvent
*e
);
174 void movemouse(Client
*c
);
175 Client
*nexttiled(Client
*c
, int monitor
);
176 void propertynotify(XEvent
*e
);
177 void quit(const char *arg
);
178 void reapply(const char *arg
);
179 void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
);
180 void resizemouse(Client
*c
);
184 void setclientstate(Client
*c
, long state
);
185 void setlayout(const char *arg
);
186 void setmwfact(const char *arg
);
188 void spawn(const char *arg
);
189 void tag(const char *arg
);
190 unsigned int textnw(const char *text
, unsigned int len
);
191 unsigned int textw(const char *text
);
193 void togglebar(const char *arg
);
194 void togglefloating(const char *arg
);
195 void toggletag(const char *arg
);
196 void toggleview(const char *arg
);
197 void unban(Client
*c
);
198 void unmanage(Client
*c
);
199 void unmapnotify(XEvent
*e
);
200 void updatebarpos(Monitor
*m
);
201 void updatesizehints(Client
*c
);
202 void updatetitle(Client
*c
);
203 void updatewmhints(Client
*c
);
204 void view(const char *arg
);
205 void viewprevtag(const char *arg
); /* views previous selected tags */
206 int xerror(Display
*dpy
, XErrorEvent
*ee
);
207 int xerrordummy(Display
*dsply
, XErrorEvent
*ee
);
208 int xerrorstart(Display
*dsply
, XErrorEvent
*ee
);
209 void zoom(const char *arg
);
211 void movetomonitor(const char *arg
);
212 void selectmonitor(const char *arg
);
219 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
220 unsigned int bh
, bpos
;
221 unsigned int blw
= 0;
222 unsigned int numlockmask
= 0;
223 void (*handler
[LASTEvent
]) (XEvent
*) = {
224 [ButtonPress
] = buttonpress
,
225 [ConfigureRequest
] = configurerequest
,
226 [ConfigureNotify
] = configurenotify
,
227 [DestroyNotify
] = destroynotify
,
228 [EnterNotify
] = enternotify
,
231 [KeyPress
] = keypress
,
232 [MappingNotify
] = mappingnotify
,
233 [MapRequest
] = maprequest
,
234 [PropertyNotify
] = propertynotify
,
235 [UnmapNotify
] = unmapnotify
237 Atom wmatom
[WMLast
], netatom
[NetLast
];
238 Bool isxinerama
= False
;
239 Bool domwfact
= True
;
241 Bool otherwm
, readin
;
243 Client
*clients
= NULL
;
245 Client
*stack
= NULL
;
246 Cursor cursor
[CurLast
];
253 /* configuration, allows nested code to access above variables */
256 //Bool prevtags[LENGTH(tags)];
258 /* function implementations */
260 applyrules(Client
*c
) {
261 static char buf
[512];
264 Bool matched_tag
= False
;
265 Bool matched_monitor
= False
;
266 XClassHint ch
= { 0 };
269 XGetClassHint(dpy
, c
->win
, &ch
);
270 snprintf(buf
, sizeof buf
, "%s:%s:%s",
271 ch
.res_class
? ch
.res_class
: "",
272 ch
.res_name
? ch
.res_name
: "", c
->name
);
273 for(i
= 0; i
< LENGTH(rules
); i
++)
274 if(regs
[i
].propregex
&& !regexec(regs
[i
].propregex
, buf
, 1, &tmp
, 0)) {
275 if (rules
[i
].monitor
>= 0 && rules
[i
].monitor
< mcount
) {
276 matched_monitor
= True
;
277 c
->monitor
= rules
[i
].monitor
;
280 c
->isfloating
= rules
[i
].isfloating
;
281 for(j
= 0; regs
[i
].tagregex
&& j
< LENGTH(tags
); j
++) {
282 if(!regexec(regs
[i
].tagregex
, tags
[j
], 1, &tmp
, 0)) {
293 memcpy(c
->tags
, monitors
[monitorat()].seltags
, sizeof initags
);
294 if (!matched_monitor
)
295 c
->monitor
= monitorat();
302 for(c
= clients
; c
; c
= c
->next
)
303 if(isvisible(c
, c
->monitor
))
308 monitors
[selmonitor
].layout
->arrange();
322 attachstack(Client
*c
) {
331 XMoveWindow(dpy
, c
->win
, c
->x
+ 3 * monitors
[c
->monitor
].sw
, c
->y
);
336 buttonpress(XEvent
*e
) {
339 XButtonPressedEvent
*ev
= &e
->xbutton
;
341 Monitor
*m
= &monitors
[monitorat()];
343 if(ev
->window
== m
->barwin
) {
345 for(i
= 0; i
< LENGTH(tags
); i
++) {
348 if(ev
->button
== Button1
) {
349 if(ev
->state
& MODKEY
)
354 else if(ev
->button
== Button3
) {
355 if(ev
->state
& MODKEY
)
363 if((ev
->x
< x
+ blw
) && ev
->button
== Button1
)
366 else if((c
= getclient(ev
->window
))) {
368 if(CLEANMASK(ev
->state
) != MODKEY
)
370 if(ev
->button
== Button1
) {
374 else if(ev
->button
== Button2
) {
375 if((floating
!= m
->layout
->arrange
) && c
->isfloating
)
376 togglefloating(NULL
);
380 else if(ev
->button
== Button3
&& !c
->isfixed
) {
390 XSetErrorHandler(xerrorstart
);
392 /* this causes an error if some other window manager is running */
393 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
396 eprint("dwm: another window manager is already running\n");
398 XSetErrorHandler(NULL
);
399 xerrorxlib
= XSetErrorHandler(xerror
);
412 XFreeFontSet(dpy
, dc
.font
.set
);
414 XFreeFont(dpy
, dc
.font
.xfont
);
416 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
417 XFreePixmap(dpy
, dc
.drawable
);
419 XFreeCursor(dpy
, cursor
[CurNormal
]);
420 XFreeCursor(dpy
, cursor
[CurResize
]);
421 XFreeCursor(dpy
, cursor
[CurMove
]);
422 for(i
= 0; i
< mcount
; i
++)
423 XDestroyWindow(dpy
, monitors
[i
].barwin
);
425 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
435 regs
= emallocz(LENGTH(rules
) * sizeof(Regs
));
436 for(i
= 0; i
< LENGTH(rules
); i
++) {
438 reg
= emallocz(sizeof(regex_t
));
439 if(regcomp(reg
, rules
[i
].prop
, REG_EXTENDED
))
442 regs
[i
].propregex
= reg
;
445 reg
= emallocz(sizeof(regex_t
));
446 if(regcomp(reg
, rules
[i
].tags
, REG_EXTENDED
))
449 regs
[i
].tagregex
= reg
;
455 configure(Client
*c
) {
458 ce
.type
= ConfigureNotify
;
466 ce
.border_width
= c
->border
;
468 ce
.override_redirect
= False
;
469 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
473 configurenotify(XEvent
*e
) {
474 XConfigureEvent
*ev
= &e
->xconfigure
;
475 Monitor
*m
= &monitors
[selmonitor
];
477 if(ev
->window
== root
&& (ev
->width
!= m
->sw
|| ev
->height
!= m
->sh
)) {
478 /* TODO -- update Xinerama dimensions here */
481 XFreePixmap(dpy
, dc
.drawable
);
482 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(root
, screen
), bh
, DefaultDepth(dpy
, screen
));
483 XResizeWindow(dpy
, m
->barwin
, m
->sw
, bh
);
490 configurerequest(XEvent
*e
) {
492 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
495 if((c
= getclient(ev
->window
))) {
496 Monitor
*m
= &monitors
[c
->monitor
];
497 if(ev
->value_mask
& CWBorderWidth
)
498 c
->border
= ev
->border_width
;
499 if(c
->isfixed
|| c
->isfloating
|| (floating
== m
->layout
->arrange
)) {
500 if(ev
->value_mask
& CWX
)
502 if(ev
->value_mask
& CWY
)
504 if(ev
->value_mask
& CWWidth
)
506 if(ev
->value_mask
& CWHeight
)
508 if((c
->x
- m
->sx
+ c
->w
) > m
->sw
&& c
->isfloating
)
509 c
->x
= m
->sx
+ (m
->sw
/ 2 - c
->w
/ 2); /* center in x direction */
510 if((c
->y
- m
->sy
+ c
->h
) > m
->sh
&& c
->isfloating
)
511 c
->y
= m
->sy
+ (m
->sh
/ 2 - c
->h
/ 2); /* center in y direction */
512 if((ev
->value_mask
& (CWX
| CWY
))
513 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
515 if(isvisible(c
, monitorat()))
516 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
524 wc
.width
= ev
->width
;
525 wc
.height
= ev
->height
;
526 wc
.border_width
= ev
->border_width
;
527 wc
.sibling
= ev
->above
;
528 wc
.stack_mode
= ev
->detail
;
529 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
535 destroynotify(XEvent
*e
) {
537 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
539 if((c
= getclient(ev
->window
)))
546 c
->prev
->next
= c
->next
;
548 c
->next
->prev
= c
->prev
;
551 c
->next
= c
->prev
= NULL
;
555 detachstack(Client
*c
) {
558 for(tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
567 for(i
= 0; i
< mcount
; i
++) {
568 Monitor
*m
= &monitors
[i
];
570 for(c
= stack
; c
&& !isvisible(c
, i
); c
= c
->snext
);
571 fprintf(stderr
, "m%d %s\n", i
, c
? c
->name
: "NIL");
572 for(j
= 0; j
< LENGTH(tags
); j
++) {
573 dc
.w
= textw(tags
[j
]);
575 drawtext(m
, tags
[j
], dc
.sel
, isurgent(i
, j
));
576 drawsquare(m
, c
&& c
->tags
[j
] && c
->monitor
== i
,
577 isoccupied(i
, j
), isurgent(i
, j
), dc
.sel
);
580 drawtext(m
, tags
[j
], dc
.norm
, isurgent(i
, j
));
581 drawsquare(m
, c
&& c
->tags
[j
] && c
->monitor
== i
,
582 isoccupied(i
, j
), isurgent(i
, j
), dc
.norm
);
587 drawtext(m
, m
->layout
->symbol
, dc
.norm
, False
);
589 if(i
== selmonitor
) {
596 drawtext(m
, stext
, dc
.norm
, False
);
600 if((dc
.w
= dc
.x
- x
) > bh
) {
603 drawtext(m
, c
->name
, dc
.sel
, False
);
604 drawsquare(m
, False
, c
->isfloating
, False
, dc
.sel
);
607 drawtext(m
, NULL
, dc
.norm
, False
);
609 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->sw
, bh
, 0, 0);
615 drawsquare(Monitor
*m
, Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
618 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
620 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
621 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
622 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
626 r
.width
= r
.height
= x
+ 1;
627 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
630 r
.width
= r
.height
= x
;
631 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
636 drawtext(Monitor
*m
, const char *text
, unsigned long col
[ColLast
], Bool invert
) {
638 static char buf
[256];
639 unsigned int len
, olen
;
640 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
642 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
643 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
647 olen
= len
= strlen(text
);
648 if(len
>= sizeof buf
)
649 len
= sizeof buf
- 1;
650 memcpy(buf
, text
, len
);
652 h
= dc
.font
.ascent
+ dc
.font
.descent
;
653 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
655 /* shorten text if necessary */
656 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
667 return; /* too long */
668 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
670 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
672 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
676 emallocz(unsigned int size
) {
677 void *res
= calloc(1, size
);
680 eprint("fatal: could not malloc() %u bytes\n", size
);
685 enternotify(XEvent
*e
) {
687 XCrossingEvent
*ev
= &e
->xcrossing
;
689 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) {
690 if(!isxinerama
|| ev
->window
!= root
)
693 if((c
= getclient(ev
->window
)))
696 selmonitor
= monitorat();
697 fprintf(stderr
, "updating selmonitor %d\n", selmonitor
);
703 eprint(const char *errstr
, ...) {
706 va_start(ap
, errstr
);
707 vfprintf(stderr
, errstr
, ap
);
714 XExposeEvent
*ev
= &e
->xexpose
;
717 if(ev
->window
== monitors
[selmonitor
].barwin
)
723 floating(void) { /* default floating layout */
726 domwfact
= dozoom
= False
;
727 for(c
= clients
; c
; c
= c
->next
)
728 if(isvisible(c
, selmonitor
))
729 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
737 selmonitor
= c
->monitor
;
738 m
= &monitors
[selmonitor
];
739 if(!c
|| (c
&& !isvisible(c
, selmonitor
)))
740 for(c
= stack
; c
&& !isvisible(c
, c
->monitor
); c
= c
->snext
);
741 if(sel
&& sel
!= c
) {
742 grabbuttons(sel
, False
);
743 XSetWindowBorder(dpy
, sel
->win
, dc
.norm
[ColBorder
]);
748 grabbuttons(c
, True
);
753 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
754 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
755 selmonitor
= c
->monitor
;
758 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
764 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
765 XFocusChangeEvent
*ev
= &e
->xfocus
;
767 if(sel
&& ev
->window
!= sel
->win
)
768 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
772 focusnext(const char *arg
) {
777 for(c
= sel
->next
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
779 for(c
= clients
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
787 focusprev(const char *arg
) {
792 for(c
= sel
->prev
; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
794 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
795 for(; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
804 getclient(Window w
) {
807 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
812 getcolor(const char *colstr
) {
813 Colormap cmap
= DefaultColormap(dpy
, screen
);
816 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
817 eprint("error, cannot allocate color '%s'\n", colstr
);
825 unsigned char *p
= NULL
;
826 unsigned long n
, extra
;
829 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
830 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
831 if(status
!= Success
)
840 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
845 if(!text
|| size
== 0)
848 XGetTextProperty(dpy
, w
, &name
, atom
);
851 if(name
.encoding
== XA_STRING
)
852 strncpy(text
, (char *)name
.value
, size
- 1);
854 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
856 strncpy(text
, *list
, size
- 1);
857 XFreeStringList(list
);
860 text
[size
- 1] = '\0';
866 grabbuttons(Client
*c
, Bool focused
) {
867 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
870 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
871 GrabModeAsync
, GrabModeSync
, None
, None
);
872 XGrabButton(dpy
, Button1
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
873 GrabModeAsync
, GrabModeSync
, None
, None
);
874 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
875 GrabModeAsync
, GrabModeSync
, None
, None
);
876 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
877 GrabModeAsync
, GrabModeSync
, None
, None
);
879 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
880 GrabModeAsync
, GrabModeSync
, None
, None
);
881 XGrabButton(dpy
, Button2
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
882 GrabModeAsync
, GrabModeSync
, None
, None
);
883 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
884 GrabModeAsync
, GrabModeSync
, None
, None
);
885 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
886 GrabModeAsync
, GrabModeSync
, None
, None
);
888 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
889 GrabModeAsync
, GrabModeSync
, None
, None
);
890 XGrabButton(dpy
, Button3
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
891 GrabModeAsync
, GrabModeSync
, None
, None
);
892 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
893 GrabModeAsync
, GrabModeSync
, None
, None
);
894 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
895 GrabModeAsync
, GrabModeSync
, None
, None
);
898 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
899 GrabModeAsync
, GrabModeSync
, None
, None
);
906 XModifierKeymap
*modmap
;
908 /* init modifier map */
909 modmap
= XGetModifierMapping(dpy
);
910 for(i
= 0; i
< 8; i
++)
911 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
912 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
913 numlockmask
= (1 << i
);
915 XFreeModifiermap(modmap
);
917 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
918 for(i
= 0; i
< LENGTH(keys
); i
++) {
919 code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
);
920 XGrabKey(dpy
, code
, keys
[i
].mod
, root
, True
,
921 GrabModeAsync
, GrabModeAsync
);
922 XGrabKey(dpy
, code
, keys
[i
].mod
| LockMask
, root
, True
,
923 GrabModeAsync
, GrabModeAsync
);
924 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
, root
, True
,
925 GrabModeAsync
, GrabModeAsync
);
926 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
| LockMask
, root
, True
,
927 GrabModeAsync
, GrabModeAsync
);
932 idxoftag(const char *tag
) {
935 for(i
= 0; (i
< LENGTH(tags
)) && (tags
[i
] != tag
); i
++);
936 return (i
< LENGTH(tags
)) ? i
: 0;
940 initfont(const char *fontstr
) {
941 char *def
, **missing
;
946 XFreeFontSet(dpy
, dc
.font
.set
);
947 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
950 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
951 XFreeStringList(missing
);
954 XFontSetExtents
*font_extents
;
955 XFontStruct
**xfonts
;
957 dc
.font
.ascent
= dc
.font
.descent
= 0;
958 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
959 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
960 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
961 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
962 dc
.font
.ascent
= (*xfonts
)->ascent
;
963 if(dc
.font
.descent
< (*xfonts
)->descent
)
964 dc
.font
.descent
= (*xfonts
)->descent
;
970 XFreeFont(dpy
, dc
.font
.xfont
);
971 dc
.font
.xfont
= NULL
;
972 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
973 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
974 eprint("error, cannot load font: '%s'\n", fontstr
);
975 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
976 dc
.font
.descent
= dc
.font
.xfont
->descent
;
978 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
982 isoccupied(unsigned int monitor
, unsigned int t
) {
985 for(c
= clients
; c
; c
= c
->next
)
986 if(c
->tags
[t
] && c
->monitor
== monitor
)
992 isprotodel(Client
*c
) {
997 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
998 for(i
= 0; !ret
&& i
< n
; i
++)
999 if(protocols
[i
] == wmatom
[WMDelete
])
1007 isurgent(unsigned int monitor
, unsigned int t
) {
1010 for(c
= clients
; c
; c
= c
->next
)
1011 if(c
->monitor
== monitor
&& c
->isurgent
&& c
->tags
[t
])
1017 isvisible(Client
*c
, int monitor
) {
1020 if(c
->monitor
!= monitor
)
1022 for(i
= 0; i
< LENGTH(tags
); i
++)
1023 if(c
->tags
[i
] && monitors
[c
->monitor
].seltags
[i
])
1029 keypress(XEvent
*e
) {
1035 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1036 for(i
= 0; i
< LENGTH(keys
); i
++)
1037 if(keysym
== keys
[i
].keysym
1038 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
1041 keys
[i
].func(keys
[i
].arg
);
1046 killclient(const char *arg
) {
1051 if(isprotodel(sel
)) {
1052 ev
.type
= ClientMessage
;
1053 ev
.xclient
.window
= sel
->win
;
1054 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1055 ev
.xclient
.format
= 32;
1056 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1057 ev
.xclient
.data
.l
[1] = CurrentTime
;
1058 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
1061 XKillClient(dpy
, sel
->win
);
1065 manage(Window w
, XWindowAttributes
*wa
) {
1066 Client
*c
, *t
= NULL
;
1072 c
= emallocz(sizeof(Client
));
1073 c
->tags
= emallocz(sizeof initags
);
1078 m
= &monitors
[c
->monitor
];
1080 c
->x
= wa
->x
+ m
->sx
;
1081 c
->y
= wa
->y
+ m
->sy
;
1084 c
->oldborder
= wa
->border_width
;
1086 if(c
->w
== m
->sw
&& c
->h
== m
->sh
) {
1089 c
->border
= wa
->border_width
;
1092 if(c
->x
+ c
->w
+ 2 * c
->border
> m
->wax
+ m
->waw
)
1093 c
->x
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1094 if(c
->y
+ c
->h
+ 2 * c
->border
> m
->way
+ m
->wah
)
1095 c
->y
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1100 c
->border
= BORDERPX
;
1102 wc
.border_width
= c
->border
;
1103 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1104 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1105 configure(c
); /* propagates border_width, if size doesn't change */
1107 XSelectInput(dpy
, w
, EnterWindowMask
| FocusChangeMask
| PropertyChangeMask
| StructureNotifyMask
);
1108 grabbuttons(c
, False
);
1110 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
1111 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
1113 memcpy(c
->tags
, t
->tags
, sizeof initags
);
1115 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
1118 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1120 XMapWindow(dpy
, c
->win
);
1121 setclientstate(c
, NormalState
);
1126 mappingnotify(XEvent
*e
) {
1127 XMappingEvent
*ev
= &e
->xmapping
;
1129 XRefreshKeyboardMapping(ev
);
1130 if(ev
->request
== MappingKeyboard
)
1135 maprequest(XEvent
*e
) {
1136 static XWindowAttributes wa
;
1137 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1139 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1141 if(wa
.override_redirect
)
1143 if(!getclient(ev
->window
))
1144 manage(ev
->window
, &wa
);
1153 XQueryPointer(dpy
, root
, &win
, &win
, &x
, &y
, &i
, &i
, &mask
);
1154 for(i
= 0; i
< mcount
; i
++) {
1155 if((x
>= monitors
[i
].sx
&& x
< monitors
[i
].sx
+ monitors
[i
].sw
)
1156 && (y
>= monitors
[i
].sy
&& y
< monitors
[i
].sy
+ monitors
[i
].sh
)) {
1164 movemouse(Client
*c
) {
1165 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1172 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1173 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1175 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1177 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1180 XUngrabPointer(dpy
, CurrentTime
);
1182 case ConfigureRequest
:
1185 handler
[ev
.type
](&ev
);
1189 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1190 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1191 Monitor
*m
= &monitors
[monitorat()];
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((monitors
[selmonitor
].layout
->arrange
!= floating
) && (abs(nx
- c
->x
) > SNAP
|| abs(ny
- c
->y
) > SNAP
))
1201 togglefloating(NULL
);
1202 if((monitors
[selmonitor
].layout
->arrange
== floating
) || c
->isfloating
)
1203 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1204 memcpy(c
->tags
, monitors
[monitorat()].seltags
, sizeof initags
);
1211 nexttiled(Client
*c
, int monitor
) {
1212 for(; c
&& (c
->isfloating
|| !isvisible(c
, monitor
)); c
= c
->next
);
1217 propertynotify(XEvent
*e
) {
1220 XPropertyEvent
*ev
= &e
->xproperty
;
1222 if(ev
->state
== PropertyDelete
)
1223 return; /* ignore */
1224 if((c
= getclient(ev
->window
))) {
1227 case XA_WM_TRANSIENT_FOR
:
1228 XGetTransientForHint(dpy
, c
->win
, &trans
);
1229 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1232 case XA_WM_NORMAL_HINTS
:
1240 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1249 quit(const char *arg
) {
1250 readin
= running
= False
;
1254 reapply(const char *arg
) {
1255 static Bool zerotags
[LENGTH(tags
)] = { 0 };
1258 for(c
= clients
; c
; c
= c
->next
) {
1259 memcpy(c
->tags
, zerotags
, sizeof zerotags
);
1266 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1268 //Monitor scr = monitors[monitorat()];
1269 // c->monitor = monitorat();
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)
1311 /* TODO: offscreen appearance fixes */
1314 x = scr.sw - w - 2 * c->border;
1316 y = scr.sh - h - 2 * c->border;
1317 if(x + w + 2 * c->border < scr.sx)
1319 if(y + h + 2 * c->border < scr.sy)
1322 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1325 c
->w
= wc
.width
= w
;
1326 c
->h
= wc
.height
= h
;
1327 wc
.border_width
= c
->border
;
1328 XConfigureWindow(dpy
, c
->win
, CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
, &wc
);
1335 resizemouse(Client
*c
) {
1342 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1343 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1345 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1347 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1350 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1351 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1352 XUngrabPointer(dpy
, CurrentTime
);
1353 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1355 case ConfigureRequest
:
1358 handler
[ev
.type
](&ev
);
1362 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
1364 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
1366 if((monitors
[selmonitor
].layout
->arrange
!= floating
) && (abs(nw
- c
->w
) > SNAP
|| abs(nh
- c
->h
) > SNAP
))
1367 togglefloating(NULL
);
1368 if((monitors
[selmonitor
].layout
->arrange
== floating
) || c
->isfloating
)
1369 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1385 if(sel
->isfloating
|| (monitors
[selmonitor
].layout
->arrange
== floating
))
1386 XRaiseWindow(dpy
, sel
->win
);
1387 if(monitors
[selmonitor
].layout
->arrange
!= floating
) {
1388 wc
.stack_mode
= Below
;
1389 wc
.sibling
= monitors
[selmonitor
].barwin
;
1390 if(!sel
->isfloating
) {
1391 XConfigureWindow(dpy
, sel
->win
, CWSibling
| CWStackMode
, &wc
);
1392 wc
.sibling
= sel
->win
;
1394 for(i
= 0; i
< mcount
; i
++) {
1395 for(c
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
)) {
1398 XConfigureWindow(dpy
, c
->win
, CWSibling
| CWStackMode
, &wc
);
1399 wc
.sibling
= c
->win
;
1404 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1410 char buf
[sizeof stext
];
1413 unsigned int len
, offset
;
1416 /* main event loop, also reads status text from stdin */
1418 xfd
= ConnectionNumber(dpy
);
1421 len
= sizeof stext
- 1;
1422 buf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1426 FD_SET(STDIN_FILENO
, &rd
);
1428 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1431 eprint("select failed\n");
1433 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1434 switch((r
= read(STDIN_FILENO
, buf
+ offset
, len
- offset
))) {
1436 strncpy(stext
, strerror(errno
), len
);
1440 strncpy(stext
, "EOF", 4);
1444 for(p
= buf
+ offset
; r
> 0; p
++, r
--, offset
++)
1445 if(*p
== '\n' || *p
== '\0') {
1447 strncpy(stext
, buf
, len
);
1448 p
+= r
- 1; /* p is buf + offset + r - 1 */
1449 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1452 memmove(buf
, p
- r
+ 1, r
);
1459 while(XPending(dpy
)) {
1460 XNextEvent(dpy
, &ev
);
1461 if(handler
[ev
.type
])
1462 (handler
[ev
.type
])(&ev
); /* call handler */
1469 unsigned int i
, num
;
1470 Window
*wins
, d1
, d2
;
1471 XWindowAttributes wa
;
1474 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1475 for(i
= 0; i
< num
; i
++) {
1476 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1477 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1479 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1480 manage(wins
[i
], &wa
);
1482 for(i
= 0; i
< num
; i
++) { /* now the transients */
1483 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1485 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1486 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1487 manage(wins
[i
], &wa
);
1495 setclientstate(Client
*c
, long state
) {
1496 long data
[] = {state
, None
};
1498 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1499 PropModeReplace
, (unsigned char *)data
, 2);
1503 setlayout(const char *arg
) {
1505 Monitor
*m
= &monitors
[monitorat()];
1509 if(m
->layout
== &layouts
[LENGTH(layouts
)])
1510 m
->layout
= &layouts
[0];
1513 for(i
= 0; i
< LENGTH(layouts
); i
++)
1514 if(!strcmp(arg
, layouts
[i
].symbol
))
1516 if(i
== LENGTH(layouts
))
1518 m
->layout
= &layouts
[i
];
1527 setmwfact(const char *arg
) {
1530 Monitor
*m
= &monitors
[monitorat()];
1534 /* arg handling, manipulate mwfact */
1537 else if(sscanf(arg
, "%lf", &delta
) == 1) {
1538 if(arg
[0] == '+' || arg
[0] == '-')
1544 else if(m
->mwfact
> 0.9)
1554 XSetWindowAttributes wa
;
1555 XineramaScreenInfo
*info
= NULL
;
1558 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1559 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1560 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1561 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1562 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1563 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1566 wa
.cursor
= cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1567 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1568 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1570 // init screens/monitors first
1572 if((isxinerama
= XineramaIsActive(dpy
)))
1573 info
= XineramaQueryScreens(dpy
, &mcount
);
1574 monitors
= emallocz(mcount
* sizeof(Monitor
));
1576 screen
= DefaultScreen(dpy
);
1577 root
= RootWindow(dpy
, screen
);
1579 /* init appearance */
1580 dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
);
1581 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
1582 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
1583 dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
);
1584 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
1585 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
1587 dc
.h
= bh
= dc
.font
.height
+ 2;
1588 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1589 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1590 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1592 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1594 for(blw
= i
= 0; i
< LENGTH(layouts
); i
++) {
1595 i
= textw(layouts
[i
].symbol
);
1599 for(i
= 0; i
< mcount
; i
++) {
1605 if (mcount
!= 1 && isxinerama
) {
1606 m
->sx
= info
[i
].x_org
;
1607 m
->sy
= info
[i
].y_org
;
1608 m
->sw
= info
[i
].width
;
1609 m
->sh
= info
[i
].height
;
1610 fprintf(stderr
, "monitor[%d]: %d,%d,%d,%d\n", i
, m
->sx
, m
->sy
, m
->sw
, m
->sh
);
1615 m
->sw
= DisplayWidth(dpy
, screen
);
1616 m
->sh
= DisplayHeight(dpy
, screen
);
1619 m
->seltags
= emallocz(sizeof initags
);
1620 m
->prevtags
= emallocz(sizeof initags
);
1622 memcpy(m
->seltags
, initags
, sizeof initags
);
1623 memcpy(m
->prevtags
, initags
, sizeof initags
);
1627 m
->layout
= &layouts
[0];
1629 // TODO: bpos per screen?
1631 wa
.override_redirect
= 1;
1632 wa
.background_pixmap
= ParentRelative
;
1633 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
1636 m
->barwin
= XCreateWindow(dpy
, root
, m
->sx
, m
->sy
, m
->sw
, bh
, 0,
1637 DefaultDepth(dpy
, screen
), CopyFromParent
, DefaultVisual(dpy
, screen
),
1638 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
1639 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1641 XMapRaised(dpy
, m
->barwin
);
1642 strcpy(stext
, "dwm-"VERSION
);
1644 /* EWMH support per monitor */
1645 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1646 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1648 /* select for events */
1649 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
1650 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
1651 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
1652 XSelectInput(dpy
, root
, wa
.event_mask
);
1663 selmonitor
= monitorat();
1664 fprintf(stderr
, "selmonitor == %d\n", selmonitor
);
1668 spawn(const char *arg
) {
1669 static char *shell
= NULL
;
1671 if(!shell
&& !(shell
= getenv("SHELL")))
1675 /* The double-fork construct avoids zombie processes and keeps the code
1676 * clean from stupid signal handlers. */
1680 close(ConnectionNumber(dpy
));
1682 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1683 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1692 tag(const char *arg
) {
1697 for(i
= 0; i
< LENGTH(tags
); i
++)
1698 sel
->tags
[i
] = (NULL
== arg
);
1699 sel
->tags
[idxoftag(arg
)] = True
;
1704 textnw(const char *text
, unsigned int len
) {
1708 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1711 return XTextWidth(dc
.font
.xfont
, text
, len
);
1715 textw(const char *text
) {
1716 return textnw(text
, strlen(text
)) + dc
.font
.height
;
1721 unsigned int i
, j
, n
, nx
, ny
, nw
, nh
, mw
, th
;
1724 domwfact
= dozoom
= True
;
1726 nx
= ny
= nw
= 0; /* gcc stupidity requires this */
1728 for (i
= 0; i
< mcount
; i
++) {
1729 Monitor
*m
= &monitors
[i
];
1731 for(n
= 0, c
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
))
1735 mw
= (n
== 1) ? m
->waw
: m
->mwfact
* m
->waw
;
1736 th
= (n
> 1) ? m
->wah
/ (n
- 1) : 0;
1737 if(n
> 1 && th
< bh
)
1740 for(j
= 0, c
= mc
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
)) {
1741 if(j
== 0) { /* master */
1744 nw
= mw
- 2 * c
->border
;
1745 nh
= m
->wah
- 2 * c
->border
;
1747 else { /* tile window */
1750 nx
+= mc
->w
+ 2 * mc
->border
;
1751 nw
= m
->waw
- mw
- 2 * c
->border
;
1753 if(j
+ 1 == n
) /* remainder */
1754 nh
= (m
->way
+ m
->wah
) - ny
- 2 * c
->border
;
1756 nh
= th
- 2 * c
->border
;
1758 fprintf(stderr
, "tile(%d, %d, %d, %d)\n", nx
, ny
, nw
, nh
);
1759 resize(c
, nx
, ny
, nw
, nh
, RESIZEHINTS
);
1760 if((RESIZEHINTS
) && ((c
->h
< bh
) || (c
->h
> nh
) || (c
->w
< bh
) || (c
->w
> nw
)))
1761 /* client doesn't accept size constraints */
1762 resize(c
, nx
, ny
, nw
, nh
, False
);
1763 if(n
> 1 && th
!= m
->wah
)
1764 ny
= c
->y
+ c
->h
+ 2 * c
->border
;
1769 fprintf(stderr
, "done\n");
1772 togglebar(const char *arg
) {
1774 bpos
= (BARPOS
== BarOff
) ? BarTop
: BARPOS
;
1777 updatebarpos(&monitors
[monitorat()]);
1782 togglefloating(const char *arg
) {
1785 sel
->isfloating
= !sel
->isfloating
;
1787 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1792 toggletag(const char *arg
) {
1798 sel
->tags
[i
] = !sel
->tags
[i
];
1799 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1800 if(j
== LENGTH(tags
))
1801 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1806 toggleview(const char *arg
) {
1809 Monitor
*m
= &monitors
[monitorat()];
1812 m
->seltags
[i
] = !m
->seltags
[i
];
1813 for(j
= 0; j
< LENGTH(tags
) && !m
->seltags
[j
]; j
++);
1814 if(j
== LENGTH(tags
))
1815 m
->seltags
[i
] = True
; /* at least one tag must be viewed */
1823 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1824 c
->isbanned
= False
;
1828 unmanage(Client
*c
) {
1831 wc
.border_width
= c
->oldborder
;
1832 /* The server grab construct avoids race conditions. */
1834 XSetErrorHandler(xerrordummy
);
1835 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1840 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1841 setclientstate(c
, WithdrawnState
);
1845 XSetErrorHandler(xerror
);
1851 unmapnotify(XEvent
*e
) {
1853 XUnmapEvent
*ev
= &e
->xunmap
;
1855 if((c
= getclient(ev
->window
)))
1860 updatebarpos(Monitor
*m
) {
1871 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
);
1875 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
+ m
->wah
);
1878 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
- bh
);
1882 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1886 updatesizehints(Client
*c
) {
1890 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1892 c
->flags
= size
.flags
;
1893 if(c
->flags
& PBaseSize
) {
1894 c
->basew
= size
.base_width
;
1895 c
->baseh
= size
.base_height
;
1897 else if(c
->flags
& PMinSize
) {
1898 c
->basew
= size
.min_width
;
1899 c
->baseh
= size
.min_height
;
1902 c
->basew
= c
->baseh
= 0;
1903 if(c
->flags
& PResizeInc
) {
1904 c
->incw
= size
.width_inc
;
1905 c
->inch
= size
.height_inc
;
1908 c
->incw
= c
->inch
= 0;
1909 if(c
->flags
& PMaxSize
) {
1910 c
->maxw
= size
.max_width
;
1911 c
->maxh
= size
.max_height
;
1914 c
->maxw
= c
->maxh
= 0;
1915 if(c
->flags
& PMinSize
) {
1916 c
->minw
= size
.min_width
;
1917 c
->minh
= size
.min_height
;
1919 else if(c
->flags
& PBaseSize
) {
1920 c
->minw
= size
.base_width
;
1921 c
->minh
= size
.base_height
;
1924 c
->minw
= c
->minh
= 0;
1925 if(c
->flags
& PAspect
) {
1926 c
->minax
= size
.min_aspect
.x
;
1927 c
->maxax
= size
.max_aspect
.x
;
1928 c
->minay
= size
.min_aspect
.y
;
1929 c
->maxay
= size
.max_aspect
.y
;
1932 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1933 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1934 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1938 updatetitle(Client
*c
) {
1939 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1940 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1944 updatewmhints(Client
*c
) {
1947 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1948 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1953 /* There's no way to check accesses to destroyed windows, thus those cases are
1954 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1955 * default error handler, which may call exit. */
1957 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1958 if(ee
->error_code
== BadWindow
1959 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1960 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1961 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1962 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1963 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1964 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1965 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1967 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1968 ee
->request_code
, ee
->error_code
);
1969 return xerrorxlib(dpy
, ee
); /* may call exit */
1973 xerrordummy(Display
*dsply
, XErrorEvent
*ee
) {
1977 /* Startup Error handler to check if another window manager
1978 * is already running. */
1980 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
1986 view(const char *arg
) {
1988 Bool tmp
[LENGTH(tags
)];
1989 Monitor
*m
= &monitors
[monitorat()];
1991 for(i
= 0; i
< LENGTH(tags
); i
++)
1992 tmp
[i
] = (NULL
== arg
);
1993 tmp
[idxoftag(arg
)] = True
;
1994 if(memcmp(m
->seltags
, tmp
, sizeof initags
) != 0) {
1995 memcpy(m
->prevtags
, m
->seltags
, sizeof initags
);
1996 memcpy(m
->seltags
, tmp
, sizeof initags
);
2002 viewprevtag(const char *arg
) {
2003 static Bool tmp
[LENGTH(tags
)];
2005 Monitor
*m
= &monitors
[monitorat()];
2007 memcpy(tmp
, m
->seltags
, sizeof initags
);
2008 memcpy(m
->seltags
, m
->prevtags
, sizeof initags
);
2009 memcpy(m
->prevtags
, tmp
, sizeof initags
);
2014 zoom(const char *arg
) {
2017 if(!sel
|| !dozoom
|| sel
->isfloating
)
2019 if((c
= sel
) == nexttiled(clients
, c
->monitor
))
2020 if(!(c
= nexttiled(c
->next
, c
->monitor
)))
2029 movetomonitor(const char *arg
) {
2031 sel
->monitor
= arg
? atoi(arg
) : (sel
->monitor
+1) % mcount
;
2033 memcpy(sel
->tags
, monitors
[sel
->monitor
].seltags
, sizeof initags
);
2034 resize(sel
, monitors
[sel
->monitor
].wax
, monitors
[sel
->monitor
].way
, sel
->w
, sel
->h
, True
);
2040 selectmonitor(const char *arg
) {
2041 Monitor
*m
= &monitors
[arg
? atoi(arg
) : (monitorat()+1) % mcount
];
2043 XWarpPointer(dpy
, None
, root
, 0, 0, 0, 0, m
->wax
+m
->waw
/2, m
->way
+m
->wah
/2);
2049 main(int argc
, char *argv
[]) {
2050 if(argc
== 2 && !strcmp("-v", argv
[1]))
2051 eprint("dwm-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
2052 "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
2054 eprint("usage: dwm [-v]\n");
2056 setlocale(LC_CTYPE
, "");
2057 if(!(dpy
= XOpenDisplay(0)))
2058 eprint("dwm: cannot open display\n");