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
;
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
;
130 /* function declarations */
131 void applyrules(Client
*c
);
133 void attach(Client
*c
);
134 void attachstack(Client
*c
);
136 void buttonpress(XEvent
*e
);
137 void checkotherwm(void);
139 void compileregs(void);
140 void configure(Client
*c
);
141 void configurenotify(XEvent
*e
);
142 void configurerequest(XEvent
*e
);
143 void destroynotify(XEvent
*e
);
144 void detach(Client
*c
);
145 void detachstack(Client
*c
);
147 void drawsquare(Monitor
*, Bool filled
, Bool empty
, unsigned long col
[ColLast
]);
148 void drawtext(Monitor
*, const char *text
, unsigned long col
[ColLast
]);
149 void *emallocz(unsigned int size
);
150 void enternotify(XEvent
*e
);
151 void eprint(const char *errstr
, ...);
152 void expose(XEvent
*e
);
153 void floating(void); /* default floating layout */
154 void focus(Client
*c
);
155 void focusin(XEvent
*e
);
156 void focusnext(const char *arg
);
157 void focusprev(const char *arg
);
158 Client
*getclient(Window w
);
159 unsigned long getcolor(const char *colstr
);
160 long getstate(Window w
);
161 Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
162 void grabbuttons(Client
*c
, Bool focused
);
164 unsigned int idxoftag(const char *tag
);
165 void initfont(Monitor
*, const char *fontstr
);
166 Bool
isoccupied(Monitor
*m
, unsigned int t
);
167 Bool
isprotodel(Client
*c
);
168 Bool
isvisible(Client
*c
, Monitor
*m
);
169 void keypress(XEvent
*e
);
170 void killclient(const char *arg
);
171 void leavenotify(XEvent
*e
);
172 void manage(Window w
, XWindowAttributes
*wa
);
173 void mappingnotify(XEvent
*e
);
174 void maprequest(XEvent
*e
);
175 void maximize(const char *arg
);
176 void movemouse(Client
*c
);
177 Client
*nexttiled(Client
*c
, Monitor
*m
);
178 void propertynotify(XEvent
*e
);
179 void quit(const char *arg
);
180 void reapply(const char *arg
);
181 void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
);
182 void resizemouse(Client
*c
);
186 void setclientstate(Client
*c
, long state
);
187 void setlayout(const char *arg
);
188 void setmwfact(const char *arg
);
190 void spawn(const char *arg
);
191 void tag(const char *arg
);
192 unsigned int textnw(Monitor
*, const char *text
, unsigned int len
);
193 unsigned int textw(Monitor
*, const char *text
);
195 void togglebar(const char *arg
);
196 void togglefloating(const char *arg
);
197 void toggletag(const char *arg
);
198 void toggleview(const char *arg
);
199 void unban(Client
*c
);
200 void unmanage(Client
*c
);
201 void unmapnotify(XEvent
*e
);
202 void updatebarpos(Monitor
*s
);
203 void updatesizehints(Client
*c
);
204 void updatetitle(Client
*c
);
205 void view(const char *arg
);
206 void viewprevtag(const char *arg
); /* views previous selected tags */
207 int xerror(Display
*dpy
, XErrorEvent
*ee
);
208 int xerrordummy(Display
*dsply
, XErrorEvent
*ee
);
209 int xerrorstart(Display
*dsply
, XErrorEvent
*ee
);
210 void zoom(const char *arg
);
211 int monitorat(int, int);
212 void movetomonitor(const char *arg
);
213 void selectmonitor(const char *arg
);
219 //int screen, sx, sy, sw, sh, wax, way, waw, wah;
220 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
221 unsigned int bh
, bpos
;
222 unsigned int blw
= 0;
223 unsigned int numlockmask
= 0;
224 void (*handler
[LASTEvent
]) (XEvent
*) = {
225 [ButtonPress
] = buttonpress
,
226 [ConfigureRequest
] = configurerequest
,
227 [ConfigureNotify
] = configurenotify
,
228 [DestroyNotify
] = destroynotify
,
229 [EnterNotify
] = enternotify
,
232 [KeyPress
] = keypress
,
233 [LeaveNotify
] = leavenotify
,
234 [MappingNotify
] = mappingnotify
,
235 [MapRequest
] = maprequest
,
236 [PropertyNotify
] = propertynotify
,
237 [UnmapNotify
] = unmapnotify
239 Atom wmatom
[WMLast
], netatom
[NetLast
];
240 Bool domwfact
= True
;
242 Bool otherwm
, readin
;
244 //Bool selscreen = True;
245 Client
*clients
= NULL
;
247 Client
*stack
= NULL
;
248 Cursor cursor
[CurLast
];
252 //Layout *layout = NULL;
253 //Window barwin, root;
258 /* configuration, allows nested code to access above variables */
261 //Bool prevtags[LENGTH(tags)];
263 /* function implementations */
265 applyrules(Client
*c
) {
266 static char buf
[512];
269 Bool matched_tag
= False
;
270 Bool matched_monitor
= False
;
271 XClassHint ch
= { 0 };
274 XGetClassHint(dpy
, c
->win
, &ch
);
275 snprintf(buf
, sizeof buf
, "%s:%s:%s",
276 ch
.res_class
? ch
.res_class
: "",
277 ch
.res_name
? ch
.res_name
: "", c
->name
);
278 for(i
= 0; i
< LENGTH(rules
); i
++)
279 if(regs
[i
].propregex
&& !regexec(regs
[i
].propregex
, buf
, 1, &tmp
, 0)) {
280 if (rules
[i
].monitor
>= 0 && rules
[i
].monitor
< mcount
) {
281 matched_monitor
= True
;
282 c
->monitor
= rules
[i
].monitor
;
285 c
->isfloating
= rules
[i
].isfloating
;
286 for(j
= 0; regs
[i
].tagregex
&& j
< LENGTH(tags
); j
++) {
287 if(!regexec(regs
[i
].tagregex
, tags
[j
], 1, &tmp
, 0)) {
298 memcpy(c
->tags
, monitors
[monitorat(-1, -1)].seltags
, sizeof seltags
);
299 if (!matched_monitor
)
300 c
->monitor
= monitorat(-1, -1);
307 for(c
= clients
; c
; c
= c
->next
)
308 if(isvisible(c
, &monitors
[c
->monitor
]))
313 monitors
[selmonitor
].layout
->arrange();
327 attachstack(Client
*c
) {
336 XMoveWindow(dpy
, c
->win
, c
->x
+ 3 * monitors
[c
->monitor
].sw
, c
->y
);
341 buttonpress(XEvent
*e
) {
344 XButtonPressedEvent
*ev
= &e
->xbutton
;
346 Monitor s
= monitors
[monitorat(-1, -1)];
348 if(ev
->window
== s
.barwin
) {
350 for(i
= 0; i
< LENGTH(tags
); i
++) {
351 x
+= textw(&s
, tags
[i
]);
353 if(ev
->button
== Button1
) {
354 if(ev
->state
& MODKEY
)
359 else if(ev
->button
== Button3
) {
360 if(ev
->state
& MODKEY
)
368 if((ev
->x
< x
+ blw
) && ev
->button
== Button1
)
371 else if((c
= getclient(ev
->window
))) {
373 if(CLEANMASK(ev
->state
) != MODKEY
)
375 if(ev
->button
== Button1
) {
376 if((s
.layout
->arrange
== floating
) || c
->isfloating
)
379 togglefloating(NULL
);
382 else if(ev
->button
== Button2
) {
383 if((floating
!= s
.layout
->arrange
) && c
->isfloating
)
384 togglefloating(NULL
);
388 else if(ev
->button
== Button3
&& !c
->isfixed
) {
389 if((floating
== s
.layout
->arrange
) || c
->isfloating
)
392 togglefloating(NULL
);
401 XSetErrorHandler(xerrorstart
);
403 /* this causes an error if some other window manager is running */
404 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
407 eprint("dwm: another window manager is already running\n");
409 XSetErrorHandler(NULL
);
410 xerrorxlib
= XSetErrorHandler(xerror
);
422 for(i
= 0; i
< mcount
; i
++) {
423 Monitor
*m
= &monitors
[i
];
425 XFreeFontSet(dpy
, m
->dc
.font
.set
);
427 XFreeFont(dpy
, m
->dc
.font
.xfont
);
428 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
429 XFreePixmap(dpy
, m
->dc
.drawable
);
430 XFreeGC(dpy
, m
->dc
.gc
);
431 XDestroyWindow(dpy
, m
->barwin
);
432 XFreeCursor(dpy
, cursor
[CurNormal
]);
433 XFreeCursor(dpy
, cursor
[CurResize
]);
434 XFreeCursor(dpy
, cursor
[CurMove
]);
435 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
447 regs
= emallocz(LENGTH(rules
) * sizeof(Regs
));
448 for(i
= 0; i
< LENGTH(rules
); i
++) {
450 reg
= emallocz(sizeof(regex_t
));
451 if(regcomp(reg
, rules
[i
].prop
, REG_EXTENDED
))
454 regs
[i
].propregex
= reg
;
457 reg
= emallocz(sizeof(regex_t
));
458 if(regcomp(reg
, rules
[i
].tags
, REG_EXTENDED
))
461 regs
[i
].tagregex
= reg
;
467 configure(Client
*c
) {
470 ce
.type
= ConfigureNotify
;
478 ce
.border_width
= c
->border
;
480 ce
.override_redirect
= False
;
481 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
485 configurenotify(XEvent
*e
) {
486 XConfigureEvent
*ev
= &e
->xconfigure
;
487 Monitor
*m
= &monitors
[selmonitor
];
489 if(ev
->window
== root
&& (ev
->width
!= m
->sw
|| ev
->height
!= m
->sh
)) {
492 XFreePixmap(dpy
, dc
.drawable
);
493 dc
.drawable
= XCreatePixmap(dpy
, root
, m
->sw
, bh
, DefaultDepth(dpy
, screen
));
494 XResizeWindow(dpy
, m
->barwin
, m
->sw
, bh
);
501 configurerequest(XEvent
*e
) {
503 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
506 if((c
= getclient(ev
->window
))) {
507 Monitor
*m
= &monitors
[c
->monitor
];
508 if(ev
->value_mask
& CWBorderWidth
)
509 c
->border
= ev
->border_width
;
510 if(c
->isfixed
|| c
->isfloating
|| (floating
== m
->layout
->arrange
)) {
511 if(ev
->value_mask
& CWX
)
513 if(ev
->value_mask
& CWY
)
515 if(ev
->value_mask
& CWWidth
)
517 if(ev
->value_mask
& CWHeight
)
519 if((c
->x
- m
->sx
+ c
->w
) > m
->sw
&& c
->isfloating
)
520 c
->x
= m
->sx
+ (m
->sw
/ 2 - c
->w
/ 2); /* center in x direction */
521 if((c
->y
- m
->sy
+ c
->h
) > m
->sh
&& c
->isfloating
)
522 c
->y
= m
->sy
+ (m
->sh
/ 2 - c
->h
/ 2); /* center in y direction */
523 if((ev
->value_mask
& (CWX
| CWY
))
524 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
526 if(isvisible(c
, &monitors
[monitorat(-1,-1)]))
527 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
535 wc
.width
= ev
->width
;
536 wc
.height
= ev
->height
;
537 wc
.border_width
= ev
->border_width
;
538 wc
.sibling
= ev
->above
;
539 wc
.stack_mode
= ev
->detail
;
540 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
546 destroynotify(XEvent
*e
) {
548 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
550 if((c
= getclient(ev
->window
)))
557 c
->prev
->next
= c
->next
;
559 c
->next
->prev
= c
->prev
;
562 c
->next
= c
->prev
= NULL
;
566 detachstack(Client
*c
) {
569 for(tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
577 for(s
= 0; s
< mcount
; ++s
) {
578 Monitor
*m
= &monitors
[s
];
580 for(i
= 0; i
< LENGTH(tags
); i
++) {
581 m
->dc
.w
= textw(m
, tags
[i
]);
583 drawtext(m
, tags
[i
], m
->dc
.sel
);
584 drawsquare(m
, sel
&& sel
->tags
[i
] && sel
->monitor
== m
->id
, isoccupied(m
, i
), m
->dc
.sel
);
587 drawtext(m
, tags
[i
], m
->dc
.norm
);
588 drawsquare(m
, sel
&& sel
->tags
[i
] && sel
->monitor
== m
->id
, isoccupied(m
, i
), m
->dc
.norm
);
593 drawtext(m
, m
->layout
->symbol
, m
->dc
.norm
);
594 x
= m
->dc
.x
+ m
->dc
.w
;
595 m
->dc
.w
= textw(m
, stext
);
596 m
->dc
.x
= m
->sw
- m
->dc
.w
;
601 drawtext(m
, stext
, m
->dc
.norm
);
602 if((m
->dc
.w
= m
->dc
.x
- x
) > bh
) {
604 if(sel
&& sel
->monitor
== m
->id
) {
605 drawtext(m
, sel
->name
, m
->dc
.sel
);
606 drawsquare(m
, False
, sel
->isfloating
, m
->dc
.sel
);
609 drawtext(m
, NULL
, m
->dc
.norm
);
611 XCopyArea(dpy
, m
->dc
.drawable
, m
->barwin
, m
->dc
.gc
, 0, 0, m
->sw
, bh
, 0, 0);
617 drawsquare(Monitor
*m
, Bool filled
, Bool empty
, unsigned long col
[ColLast
]) {
620 XRectangle r
= { m
->dc
.x
, m
->dc
.y
, m
->dc
.w
, m
->dc
.h
};
622 gcv
.foreground
= col
[ColFG
];
623 XChangeGC(dpy
, m
->dc
.gc
, GCForeground
, &gcv
);
624 x
= (m
->dc
.font
.ascent
+ m
->dc
.font
.descent
+ 2) / 4;
628 r
.width
= r
.height
= x
+ 1;
629 XFillRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
632 r
.width
= r
.height
= x
;
633 XDrawRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
638 drawtext(Monitor
*m
, const char *text
, unsigned long col
[ColLast
]) {
640 static char buf
[256];
641 unsigned int len
, olen
;
642 XRectangle r
= { m
->dc
.x
, m
->dc
.y
, m
->dc
.w
, m
->dc
.h
};
644 XSetForeground(dpy
, m
->dc
.gc
, col
[ColBG
]);
645 XFillRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
649 olen
= len
= strlen(text
);
650 if(len
>= sizeof buf
)
651 len
= sizeof buf
- 1;
652 memcpy(buf
, text
, len
);
654 h
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
655 y
= m
->dc
.y
+ (m
->dc
.h
/ 2) - (h
/ 2) + m
->dc
.font
.ascent
;
656 x
= m
->dc
.x
+ (h
/ 2);
657 /* shorten text if necessary */
658 while(len
&& (w
= textnw(m
, buf
, len
)) > m
->dc
.w
- h
)
669 return; /* too long */
670 XSetForeground(dpy
, m
->dc
.gc
, col
[ColFG
]);
672 XmbDrawString(dpy
, m
->dc
.drawable
, m
->dc
.font
.set
, m
->dc
.gc
, x
, y
, buf
, len
);
674 XDrawString(dpy
, m
->dc
.drawable
, m
->dc
.gc
, x
, y
, buf
, len
);
678 emallocz(unsigned int size
) {
679 void *res
= calloc(1, size
);
682 eprint("fatal: could not malloc() %u bytes\n", size
);
687 enternotify(XEvent
*e
) {
689 XCrossingEvent
*ev
= &e
->xcrossing
;
691 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
693 if((c
= getclient(ev
->window
)))
695 else if(ev
->window
== root
) {
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
, &monitors
[selmonitor
]))
728 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
733 Monitor
*m
= &monitors
[monitorat(-1, -1)];
734 if(!c
|| (c
&& !isvisible(c
, m
)))
735 for(c
= stack
; c
&& !isvisible(c
, m
); c
= c
->snext
);
736 if(sel
&& sel
!= c
) {
737 grabbuttons(sel
, False
);
738 XSetWindowBorder(dpy
, sel
->win
, monitors
[sel
->monitor
].dc
.norm
[ColBorder
]);
743 grabbuttons(c
, True
);
748 XSetWindowBorder(dpy
, c
->win
, monitors
[c
->monitor
].dc
.sel
[ColBorder
]);
749 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
750 selmonitor
= monitorat(c
->x
, c
->y
);
753 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
754 selmonitor
= monitorat(-1, -1);
759 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
760 XFocusChangeEvent
*ev
= &e
->xfocus
;
762 if(sel
&& ev
->window
!= sel
->win
)
763 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
767 focusnext(const char *arg
) {
769 Monitor
*m
= &monitors
[selmonitor
];
773 for(c
= sel
->next
; c
&& !isvisible(c
, m
); c
= c
->next
);
775 for(c
= clients
; c
&& !isvisible(c
, m
); c
= c
->next
);
783 focusprev(const char *arg
) {
785 Monitor
*m
= &monitors
[selmonitor
];
789 for(c
= sel
->prev
; c
&& !isvisible(c
, m
); c
= c
->prev
);
791 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
792 for(; c
&& !isvisible(c
, m
); c
= c
->prev
);
801 getclient(Window w
) {
804 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
809 getcolor(const char *colstr
) {
810 Colormap cmap
= DefaultColormap(dpy
, screen
);
813 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
814 eprint("error, cannot allocate color '%s'\n", colstr
);
822 unsigned char *p
= NULL
;
823 unsigned long n
, extra
;
826 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
827 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
828 if(status
!= Success
)
837 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
842 if(!text
|| size
== 0)
845 XGetTextProperty(dpy
, w
, &name
, atom
);
848 if(name
.encoding
== XA_STRING
)
849 strncpy(text
, (char *)name
.value
, size
- 1);
851 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
853 strncpy(text
, *list
, size
- 1);
854 XFreeStringList(list
);
857 text
[size
- 1] = '\0';
863 grabbuttons(Client
*c
, Bool focused
) {
864 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
867 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
868 GrabModeAsync
, GrabModeSync
, None
, None
);
869 XGrabButton(dpy
, Button1
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
870 GrabModeAsync
, GrabModeSync
, None
, None
);
871 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
872 GrabModeAsync
, GrabModeSync
, None
, None
);
873 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
874 GrabModeAsync
, GrabModeSync
, None
, None
);
876 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
877 GrabModeAsync
, GrabModeSync
, None
, None
);
878 XGrabButton(dpy
, Button2
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
879 GrabModeAsync
, GrabModeSync
, None
, None
);
880 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
881 GrabModeAsync
, GrabModeSync
, None
, None
);
882 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
883 GrabModeAsync
, GrabModeSync
, None
, None
);
885 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
886 GrabModeAsync
, GrabModeSync
, None
, None
);
887 XGrabButton(dpy
, Button3
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
888 GrabModeAsync
, GrabModeSync
, None
, None
);
889 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
890 GrabModeAsync
, GrabModeSync
, None
, None
);
891 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
892 GrabModeAsync
, GrabModeSync
, None
, None
);
895 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
896 GrabModeAsync
, GrabModeSync
, None
, None
);
904 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
905 for(i
= 0; i
< LENGTH(keys
); i
++) {
906 code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
);
907 XGrabKey(dpy
, code
, keys
[i
].mod
, root
, True
,
908 GrabModeAsync
, GrabModeAsync
);
909 XGrabKey(dpy
, code
, keys
[i
].mod
| LockMask
, root
, True
,
910 GrabModeAsync
, GrabModeAsync
);
911 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
, root
, True
,
912 GrabModeAsync
, GrabModeAsync
);
913 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
| LockMask
, root
, True
,
914 GrabModeAsync
, GrabModeAsync
);
919 idxoftag(const char *tag
) {
922 for(i
= 0; (i
< LENGTH(tags
)) && (tags
[i
] != tag
); i
++);
923 return (i
< LENGTH(tags
)) ? i
: 0;
927 initfont(Monitor
*m
, const char *fontstr
) {
928 char *def
, **missing
;
933 XFreeFontSet(dpy
, m
->dc
.font
.set
);
934 m
->dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
937 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
938 XFreeStringList(missing
);
941 XFontSetExtents
*font_extents
;
942 XFontStruct
**xfonts
;
944 m
->dc
.font
.ascent
= m
->dc
.font
.descent
= 0;
945 font_extents
= XExtentsOfFontSet(m
->dc
.font
.set
);
946 n
= XFontsOfFontSet(m
->dc
.font
.set
, &xfonts
, &font_names
);
947 for(i
= 0, m
->dc
.font
.ascent
= 0, m
->dc
.font
.descent
= 0; i
< n
; i
++) {
948 if(m
->dc
.font
.ascent
< (*xfonts
)->ascent
)
949 m
->dc
.font
.ascent
= (*xfonts
)->ascent
;
950 if(m
->dc
.font
.descent
< (*xfonts
)->descent
)
951 m
->dc
.font
.descent
= (*xfonts
)->descent
;
957 XFreeFont(dpy
, m
->dc
.font
.xfont
);
958 m
->dc
.font
.xfont
= NULL
;
959 if(!(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
960 && !(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
961 eprint("error, cannot load font: '%s'\n", fontstr
);
962 m
->dc
.font
.ascent
= m
->dc
.font
.xfont
->ascent
;
963 m
->dc
.font
.descent
= m
->dc
.font
.xfont
->descent
;
965 m
->dc
.font
.height
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
969 isoccupied(Monitor
*m
, unsigned int t
) {
972 for(c
= clients
; c
; c
= c
->next
)
973 if(c
->tags
[t
] && c
->monitor
== m
->id
)
979 isprotodel(Client
*c
) {
984 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
985 for(i
= 0; !ret
&& i
< n
; i
++)
986 if(protocols
[i
] == wmatom
[WMDelete
])
994 isvisible(Client
*c
, Monitor
*m
) {
997 for(i
= 0; i
< LENGTH(tags
); i
++)
998 if(c
->tags
[i
] && monitors
[c
->monitor
].seltags
[i
] && m
->id
== c
->monitor
)
1004 keypress(XEvent
*e
) {
1010 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1011 for(i
= 0; i
< LENGTH(keys
); i
++)
1012 if(keysym
== keys
[i
].keysym
1013 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
1016 keys
[i
].func(keys
[i
].arg
);
1021 killclient(const char *arg
) {
1026 if(isprotodel(sel
)) {
1027 ev
.type
= ClientMessage
;
1028 ev
.xclient
.window
= sel
->win
;
1029 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1030 ev
.xclient
.format
= 32;
1031 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1032 ev
.xclient
.data
.l
[1] = CurrentTime
;
1033 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
1036 XKillClient(dpy
, sel
->win
);
1040 leavenotify(XEvent
*e
) {
1041 XCrossingEvent
*ev
= &e
->xcrossing
;
1043 if((ev
->window
== root
) && !ev
->same_screen
) {
1050 manage(Window w
, XWindowAttributes
*wa
) {
1051 Client
*c
, *t
= NULL
;
1056 c
= emallocz(sizeof(Client
));
1057 c
->tags
= emallocz(sizeof seltags
);
1061 Monitor
*m
= &monitors
[c
->monitor
];
1067 c
->oldborder
= wa
->border_width
;
1069 if (monitorat(c
->x
, c
->y
) != c
->monitor
) {
1074 if(c
->w
== m
->sw
&& c
->h
== m
->sh
) {
1077 c
->border
= wa
->border_width
;
1080 if(c
->x
+ c
->w
+ 2 * c
->border
> m
->wax
+ m
->waw
)
1081 c
->x
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1082 if(c
->y
+ c
->h
+ 2 * c
->border
> m
->way
+ m
->wah
)
1083 c
->y
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1088 c
->border
= BORDERPX
;
1090 wc
.border_width
= c
->border
;
1091 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1092 XSetWindowBorder(dpy
, w
, m
->dc
.norm
[ColBorder
]);
1093 configure(c
); /* propagates border_width, if size doesn't change */
1095 XSelectInput(dpy
, w
, EnterWindowMask
| FocusChangeMask
| PropertyChangeMask
| StructureNotifyMask
);
1096 grabbuttons(c
, False
);
1098 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
1099 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
1101 memcpy(c
->tags
, t
->tags
, sizeof seltags
);
1103 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
1106 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1108 XMapWindow(dpy
, c
->win
);
1109 setclientstate(c
, NormalState
);
1114 mappingnotify(XEvent
*e
) {
1115 XMappingEvent
*ev
= &e
->xmapping
;
1117 XRefreshKeyboardMapping(ev
);
1118 if(ev
->request
== MappingKeyboard
)
1123 maprequest(XEvent
*e
) {
1124 static XWindowAttributes wa
;
1125 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1127 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1129 if(wa
.override_redirect
)
1131 if(!getclient(ev
->window
))
1132 manage(ev
->window
, &wa
);
1136 maximize(const char *arg
) {
1138 if(!sel || (!sel->isfloating && layout->arrange != floating))
1140 resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
1145 movemouse(Client
*c
) {
1146 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1153 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1154 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1156 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1158 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1161 XUngrabPointer(dpy
, CurrentTime
);
1163 case ConfigureRequest
:
1166 handler
[ev
.type
](&ev
);
1170 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1171 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1172 Monitor
*m
= &monitors
[monitorat(nx
, ny
)];
1173 if(abs(m
->wax
- nx
) < SNAP
)
1175 else if(abs((m
->wax
+ m
->waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
1176 nx
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1177 if(abs(m
->way
- ny
) < SNAP
)
1179 else if(abs((m
->way
+ m
->wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
1180 ny
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1181 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1182 memcpy(c
->tags
, monitors
[monitorat(nx
, ny
)].seltags
, sizeof seltags
);
1189 nexttiled(Client
*c
, Monitor
*m
) {
1190 for(; c
&& (c
->isfloating
|| !isvisible(c
, m
)); c
= c
->next
);
1195 propertynotify(XEvent
*e
) {
1198 XPropertyEvent
*ev
= &e
->xproperty
;
1200 if(ev
->state
== PropertyDelete
)
1201 return; /* ignore */
1202 if((c
= getclient(ev
->window
))) {
1205 case XA_WM_TRANSIENT_FOR
:
1206 XGetTransientForHint(dpy
, c
->win
, &trans
);
1207 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1210 case XA_WM_NORMAL_HINTS
:
1214 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1223 quit(const char *arg
) {
1224 readin
= running
= False
;
1228 reapply(const char *arg
) {
1229 static Bool zerotags
[LENGTH(tags
)] = { 0 };
1232 for(c
= clients
; c
; c
= c
->next
) {
1233 memcpy(c
->tags
, zerotags
, sizeof zerotags
);
1240 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1242 Monitor scr
= monitors
[monitorat(x
, y
)];
1243 c
->monitor
= scr
.id
;
1246 /* set minimum possible */
1252 /* temporarily remove base dimensions */
1256 /* adjust for aspect limits */
1257 if (c
->minay
> 0 && c
->maxay
> 0 && c
->minax
> 0 && c
->maxax
> 0) {
1258 if (w
* c
->maxay
> h
* c
->maxax
)
1259 w
= h
* c
->maxax
/ c
->maxay
;
1260 else if (w
* c
->minay
< h
* c
->minax
)
1261 h
= w
* c
->minay
/ c
->minax
;
1264 /* adjust for increment value */
1270 /* restore base dimensions */
1274 if(c
->minw
> 0 && w
< c
->minw
)
1276 if(c
->minh
> 0 && h
< c
->minh
)
1278 if(c
->maxw
> 0 && w
> c
->maxw
)
1280 if(c
->maxh
> 0 && h
> c
->maxh
)
1283 if(w
<= 0 || h
<= 0)
1285 /* TODO: offscreen appearance fixes */
1288 x = scr.sw - w - 2 * c->border;
1290 y = scr.sh - h - 2 * c->border;
1291 if(x + w + 2 * c->border < scr.sx)
1293 if(y + h + 2 * c->border < scr.sy)
1296 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1299 c
->w
= wc
.width
= w
;
1300 c
->h
= wc
.height
= h
;
1301 wc
.border_width
= c
->border
;
1302 XConfigureWindow(dpy
, c
->win
, CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
, &wc
);
1309 resizemouse(Client
*c
) {
1316 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1317 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1319 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1321 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1324 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1325 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1326 XUngrabPointer(dpy
, CurrentTime
);
1327 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1329 case ConfigureRequest
:
1332 handler
[ev
.type
](&ev
);
1336 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
1338 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
1340 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1356 if(sel
->isfloating
|| (monitors
[selmonitor
].layout
->arrange
== floating
))
1357 XRaiseWindow(dpy
, sel
->win
);
1358 if(monitors
[selmonitor
].layout
->arrange
!= floating
) {
1359 wc
.stack_mode
= Below
;
1360 wc
.sibling
= monitors
[selmonitor
].barwin
;
1361 if(!sel
->isfloating
) {
1362 XConfigureWindow(dpy
, sel
->win
, CWSibling
| CWStackMode
, &wc
);
1363 wc
.sibling
= sel
->win
;
1365 for(s
= 0; s
< mcount
; s
++) {
1366 for(c
= nexttiled(clients
, &monitors
[s
]); c
; c
= nexttiled(c
->next
, &monitors
[s
])) {
1369 XConfigureWindow(dpy
, c
->win
, CWSibling
| CWStackMode
, &wc
);
1370 wc
.sibling
= c
->win
;
1375 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1381 char buf
[sizeof stext
];
1384 unsigned int len
, offset
;
1387 /* main event loop, also reads status text from stdin */
1389 xfd
= ConnectionNumber(dpy
);
1392 len
= sizeof stext
- 1;
1393 buf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1397 FD_SET(STDIN_FILENO
, &rd
);
1399 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1402 eprint("select failed\n");
1404 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1405 switch((r
= read(STDIN_FILENO
, buf
+ offset
, len
- offset
))) {
1407 strncpy(stext
, strerror(errno
), len
);
1411 strncpy(stext
, "EOF", 4);
1415 for(p
= buf
+ offset
; r
> 0; p
++, r
--, offset
++)
1416 if(*p
== '\n' || *p
== '\0') {
1418 strncpy(stext
, buf
, len
);
1419 p
+= r
- 1; /* p is buf + offset + r - 1 */
1420 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1423 memmove(buf
, p
- r
+ 1, r
);
1430 while(XPending(dpy
)) {
1431 XNextEvent(dpy
, &ev
);
1432 if(handler
[ev
.type
])
1433 (handler
[ev
.type
])(&ev
); /* call handler */
1440 unsigned int i
, num
;
1441 Window
*wins
, d1
, d2
;
1442 XWindowAttributes wa
;
1445 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1446 for(i
= 0; i
< num
; i
++) {
1447 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1448 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1450 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1451 manage(wins
[i
], &wa
);
1453 for(i
= 0; i
< num
; i
++) { /* now the transients */
1454 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1456 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1457 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1458 manage(wins
[i
], &wa
);
1466 setclientstate(Client
*c
, long state
) {
1467 long data
[] = {state
, None
};
1469 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1470 PropModeReplace
, (unsigned char *)data
, 2);
1474 setlayout(const char *arg
) {
1476 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1480 if(m
->layout
== &layouts
[LENGTH(layouts
)])
1481 m
->layout
= &layouts
[0];
1484 for(i
= 0; i
< LENGTH(layouts
); i
++)
1485 if(!strcmp(arg
, layouts
[i
].symbol
))
1487 if(i
== LENGTH(layouts
))
1489 m
->layout
= &layouts
[i
];
1498 setmwfact(const char *arg
) {
1501 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1505 /* arg handling, manipulate mwfact */
1508 else if(sscanf(arg
, "%lf", &delta
) == 1) {
1509 if(arg
[0] == '+' || arg
[0] == '-')
1515 else if(m
->mwfact
> 0.9)
1523 unsigned int i
, j
, k
;
1524 XModifierKeymap
*modmap
;
1525 XSetWindowAttributes wa
;
1528 XineramaScreenInfo
*info
= NULL
;
1531 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1532 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1533 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1534 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1535 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1536 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1537 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1538 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1541 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1542 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1543 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1546 /* init modifier map */
1547 modmap
= XGetModifierMapping(dpy
);
1548 for(i
= 0; i
< 8; i
++)
1549 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
1550 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1551 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1552 numlockmask
= (1 << i
);
1554 XFreeModifiermap(modmap
);
1556 /* select for events */
1557 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
1558 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
1559 wa
.cursor
= cursor
[CurNormal
];
1560 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
1561 XSelectInput(dpy
, root
, wa
.event_mask
);
1569 if (XineramaIsActive(dpy
)) {
1570 info
= XineramaQueryScreens(dpy
, &s
);
1573 monitors
= emallocz(s
*sizeof(Monitor
));
1576 for(i
= 0; i
< s
; i
++) {
1579 monitors
[i
].sx
= info
[i
].x_org
;
1580 monitors
[i
].sy
= info
[i
].y_org
;
1581 monitors
[i
].sw
= info
[i
].width
;
1582 monitors
[i
].sh
= info
[i
].height
;
1587 monitors
[i
].sw
= DisplayWidth(dpy
, screen
);
1588 monitors
[i
].sh
= DisplayHeight(dpy
, screen
);
1592 monitors
[i
].seltags
= emallocz(LENGTH(tags
)*sizeof(char*));
1593 monitors
[i
].prevtags
= emallocz(LENGTH(tags
)*sizeof(char*));
1595 memcpy(monitors
[i
].seltags
, seltags
, sizeof seltags
);
1596 memcpy(monitors
[i
].prevtags
, seltags
, sizeof seltags
);
1598 /* init appearance */
1599 monitors
[i
].dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
);
1600 monitors
[i
].dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
1601 monitors
[i
].dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
1602 monitors
[i
].dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
);
1603 monitors
[i
].dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
1604 monitors
[i
].dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
1605 initfont(&(monitors
[i
]), FONT
);
1606 monitors
[i
].dc
.h
= bh
= monitors
[i
].dc
.font
.height
+ 2;
1609 monitors
[i
].mwfact
= MWFACT
;
1610 monitors
[i
].layout
= &layouts
[0];
1611 for(blw
= k
= 0; k
< LENGTH(layouts
); k
++) {
1612 j
= textw(&monitors
[i
], layouts
[k
].symbol
);
1618 wa
.override_redirect
= 1;
1619 wa
.background_pixmap
= ParentRelative
;
1620 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
1623 monitors
[i
].barwin
= XCreateWindow(dpy
, root
, monitors
[i
].sx
, monitors
[i
].sy
, monitors
[i
].sw
, bh
, 0,
1624 DefaultDepth(dpy
, screen
), CopyFromParent
, DefaultVisual(dpy
, screen
),
1625 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
1626 XDefineCursor(dpy
, monitors
[i
].barwin
, cursor
[CurNormal
]);
1627 updatebarpos(&monitors
[i
]);
1628 XMapRaised(dpy
, monitors
[i
].barwin
);
1629 strcpy(stext
, "dwm-"VERSION
);
1630 monitors
[i
].dc
.drawable
= XCreatePixmap(dpy
, root
, monitors
[i
].sw
, bh
, DefaultDepth(dpy
, screen
));
1631 g
= XCreateGC(dpy
, root
, 0, 0);
1632 monitors
[i
].dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1633 XSetLineAttributes(dpy
, monitors
[i
].dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1634 if(!monitors
[i
].dc
.font
.set
)
1635 XSetFont(dpy
, monitors
[i
].dc
.gc
, monitors
[i
].dc
.font
.xfont
->fid
);
1640 spawn(const char *arg
) {
1641 static char *shell
= NULL
;
1643 if(!shell
&& !(shell
= getenv("SHELL")))
1647 /* The double-fork construct avoids zombie processes and keeps the code
1648 * clean from stupid signal handlers. */
1652 close(ConnectionNumber(dpy
));
1654 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1655 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1664 tag(const char *arg
) {
1669 for(i
= 0; i
< LENGTH(tags
); i
++)
1670 sel
->tags
[i
] = (NULL
== arg
);
1671 sel
->tags
[idxoftag(arg
)] = True
;
1676 textnw(Monitor
*m
, const char *text
, unsigned int len
) {
1679 if(m
->dc
.font
.set
) {
1680 XmbTextExtents(m
->dc
.font
.set
, text
, len
, NULL
, &r
);
1683 return XTextWidth(m
->dc
.font
.xfont
, text
, len
);
1687 textw(Monitor
*m
, const char *text
) {
1688 return textnw(m
, text
, strlen(text
)) + m
->dc
.font
.height
;
1693 unsigned int i
, n
, nx
, ny
, nw
, nh
, mw
, th
;
1697 domwfact
= dozoom
= True
;
1699 nw
= 0; /* gcc stupidity requires this */
1701 for (s
= 0; s
< mcount
; s
++) {
1702 Monitor
*m
= &monitors
[s
];
1704 for(n
= 0, c
= nexttiled(clients
, m
); c
; c
= nexttiled(c
->next
, m
))
1707 for(i
= 0, c
= mc
= nexttiled(clients
, m
); c
; c
= nexttiled(c
->next
, m
)) {
1709 mw
= (n
== 1) ? m
->waw
: m
->mwfact
* m
->waw
;
1710 th
= (n
> 1) ? m
->wah
/ (n
- 1) : 0;
1711 if(n
> 1 && th
< bh
)
1713 if(i
== 0) { /* master */
1716 nw
= mw
- 2 * c
->border
;
1717 nh
= m
->wah
- 2 * c
->border
;
1719 else { /* tile window */
1722 nx
+= mc
->w
+ 2 * mc
->border
;
1723 nw
= m
->waw
- mw
- 2 * c
->border
;
1725 if(i
+ 1 == n
) /* remainder */
1726 nh
= (m
->way
+ m
->wah
) - ny
- 2 * c
->border
;
1728 nh
= th
- 2 * c
->border
;
1730 resize(c
, nx
, ny
, nw
, nh
, RESIZEHINTS
);
1731 if((RESIZEHINTS
) && ((c
->h
< bh
) || (c
->h
> nh
) || (c
->w
< bh
) || (c
->w
> nw
)))
1732 /* client doesn't accept size constraints */
1733 resize(c
, nx
, ny
, nw
, nh
, False
);
1734 if(n
> 1 && th
!= m
->wah
)
1735 ny
= c
->y
+ c
->h
+ 2 * c
->border
;
1742 togglebar(const char *arg
) {
1744 bpos
= (BARPOS
== BarOff
) ? BarTop
: BARPOS
;
1747 updatebarpos(&monitors
[monitorat(-1,-1)]);
1752 togglefloating(const char *arg
) {
1755 sel
->isfloating
= !sel
->isfloating
;
1757 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1762 toggletag(const char *arg
) {
1768 sel
->tags
[i
] = !sel
->tags
[i
];
1769 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1770 if(j
== LENGTH(tags
))
1771 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1776 toggleview(const char *arg
) {
1779 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1782 m
->seltags
[i
] = !m
->seltags
[i
];
1783 for(j
= 0; j
< LENGTH(tags
) && !m
->seltags
[j
]; j
++);
1784 if(j
== LENGTH(tags
))
1785 m
->seltags
[i
] = True
; /* at least one tag must be viewed */
1793 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1794 c
->isbanned
= False
;
1798 unmanage(Client
*c
) {
1801 wc
.border_width
= c
->oldborder
;
1802 /* The server grab construct avoids race conditions. */
1804 XSetErrorHandler(xerrordummy
);
1805 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1810 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1811 setclientstate(c
, WithdrawnState
);
1815 XSetErrorHandler(xerror
);
1821 unmapnotify(XEvent
*e
) {
1823 XUnmapEvent
*ev
= &e
->xunmap
;
1825 if((c
= getclient(ev
->window
)))
1830 updatebarpos(Monitor
*s
) {
1841 XMoveWindow(dpy
, s
->barwin
, s
->sx
, s
->sy
);
1845 XMoveWindow(dpy
, s
->barwin
, s
->sx
, s
->sy
+ s
->wah
);
1848 XMoveWindow(dpy
, s
->barwin
, s
->sx
, s
->sy
- bh
);
1852 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1856 updatesizehints(Client
*c
) {
1860 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1862 c
->flags
= size
.flags
;
1863 if(c
->flags
& PBaseSize
) {
1864 c
->basew
= size
.base_width
;
1865 c
->baseh
= size
.base_height
;
1867 else if(c
->flags
& PMinSize
) {
1868 c
->basew
= size
.min_width
;
1869 c
->baseh
= size
.min_height
;
1872 c
->basew
= c
->baseh
= 0;
1873 if(c
->flags
& PResizeInc
) {
1874 c
->incw
= size
.width_inc
;
1875 c
->inch
= size
.height_inc
;
1878 c
->incw
= c
->inch
= 0;
1879 if(c
->flags
& PMaxSize
) {
1880 c
->maxw
= size
.max_width
;
1881 c
->maxh
= size
.max_height
;
1884 c
->maxw
= c
->maxh
= 0;
1885 if(c
->flags
& PMinSize
) {
1886 c
->minw
= size
.min_width
;
1887 c
->minh
= size
.min_height
;
1889 else if(c
->flags
& PBaseSize
) {
1890 c
->minw
= size
.base_width
;
1891 c
->minh
= size
.base_height
;
1894 c
->minw
= c
->minh
= 0;
1895 if(c
->flags
& PAspect
) {
1896 c
->minax
= size
.min_aspect
.x
;
1897 c
->maxax
= size
.max_aspect
.x
;
1898 c
->minay
= size
.min_aspect
.y
;
1899 c
->maxay
= size
.max_aspect
.y
;
1902 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1903 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1904 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1908 updatetitle(Client
*c
) {
1909 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1910 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1913 /* There's no way to check accesses to destroyed windows, thus those cases are
1914 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1915 * default error handler, which may call exit. */
1917 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1918 if(ee
->error_code
== BadWindow
1919 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1920 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1921 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1922 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1923 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1924 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1925 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1927 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1928 ee
->request_code
, ee
->error_code
);
1929 return xerrorxlib(dpy
, ee
); /* may call exit */
1933 xerrordummy(Display
*dsply
, XErrorEvent
*ee
) {
1937 /* Startup Error handler to check if another window manager
1938 * is already running. */
1940 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
1946 view(const char *arg
) {
1949 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1951 memcpy(m
->prevtags
, seltags
, sizeof seltags
);
1952 for(i
= 0; i
< LENGTH(tags
); i
++)
1953 m
->seltags
[i
] = (NULL
== arg
);
1954 m
->seltags
[idxoftag(arg
)] = True
;
1959 viewprevtag(const char *arg
) {
1960 static Bool tmp
[LENGTH(tags
)];
1962 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1964 memcpy(tmp
, m
->seltags
, sizeof seltags
);
1965 memcpy(m
->seltags
, m
->prevtags
, sizeof seltags
);
1966 memcpy(m
->prevtags
, tmp
, sizeof seltags
);
1971 zoom(const char *arg
) {
1974 if(!sel
|| !dozoom
|| sel
->isfloating
)
1976 if((c
= sel
) == nexttiled(clients
, &monitors
[c
->monitor
]))
1977 if(!(c
= nexttiled(c
->next
, &monitors
[c
->monitor
])))
1986 monitorat(int x
, int y
) {
1989 if(!XineramaIsActive(dpy
))
1992 if (x
< 0 || y
< 0) {
1995 XQueryPointer(dpy
, root
, &win
, &win
, &x
, &y
, &i
, &i
, &mask
);
1998 for(i
= 0; i
< mcount
; i
++)
1999 if((x
< 0 || (x
>= monitors
[i
].sx
&& x
< monitors
[i
].sx
+ monitors
[i
].sw
))
2000 && (y
< 0 || (y
>= monitors
[i
].sy
&& y
< monitors
[i
].sy
+ monitors
[i
].sh
)))
2008 movetomonitor(const char *arg
) {
2010 sel
->monitor
= arg
? atoi(arg
) : (sel
->monitor
+1) % mcount
;
2012 memcpy(sel
->tags
, monitors
[sel
->monitor
].seltags
, sizeof seltags
);
2013 resize(sel
, monitors
[sel
->monitor
].wax
, monitors
[sel
->monitor
].way
, sel
->w
, sel
->h
, True
);
2019 selectmonitor(const char *arg
) {
2020 Monitor
*m
= &monitors
[arg
? atoi(arg
) : (monitorat(-1, -1)+1) % mcount
];
2022 XWarpPointer(dpy
, None
, root
, 0, 0, 0, 0, m
->wax
+m
->waw
/2, m
->way
+m
->wah
/2);
2028 main(int argc
, char *argv
[]) {
2029 if(argc
== 2 && !strcmp("-v", argv
[1]))
2030 eprint("dwm-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
2031 "Jukka Salmi, Premysl Hruby, Szabolcs Nagy\n");
2033 eprint("usage: dwm [-v]\n");
2035 setlocale(LC_CTYPE
, "");
2036 if(!(dpy
= XOpenDisplay(0)))
2037 eprint("dwm: cannot open display\n");
2038 screen
= DefaultScreen(dpy
);
2039 root
= RootWindow(dpy
, screen
);