Xinqi Bao's Git
58148442bd191cfe1f72c753630fcb09252eecac
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);
123 int sx
, sy
, sw
, sh
, wax
, way
, wah
, waw
;
131 /* function declarations */
132 void applyrules(Client
*c
);
134 void attach(Client
*c
);
135 void attachstack(Client
*c
);
137 void buttonpress(XEvent
*e
);
138 void checkotherwm(void);
140 void compileregs(void);
141 void configure(Client
*c
);
142 void configurenotify(XEvent
*e
);
143 void configurerequest(XEvent
*e
);
144 void destroynotify(XEvent
*e
);
145 void detach(Client
*c
);
146 void detachstack(Client
*c
);
148 void drawsquare(Monitor
*, Bool filled
, Bool empty
, unsigned long col
[ColLast
]);
149 void drawtext(Monitor
*, const char *text
, unsigned long col
[ColLast
], Bool invert
);
150 void *emallocz(unsigned int size
);
151 void enternotify(XEvent
*e
);
152 void eprint(const char *errstr
, ...);
153 void expose(XEvent
*e
);
154 void floating(void); /* default floating layout */
155 void focus(Client
*c
);
156 void focusin(XEvent
*e
);
157 void focusnext(const char *arg
);
158 void focusprev(const char *arg
);
159 Client
*getclient(Window w
);
160 unsigned long getcolor(const char *colstr
, int screen
);
161 long getstate(Window w
);
162 Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
163 void grabbuttons(Client
*c
, Bool focused
);
165 unsigned int idxoftag(const char *tag
);
166 void initfont(Monitor
*, const char *fontstr
);
167 Bool
isoccupied(Monitor
*m
, unsigned int t
);
168 Bool
isprotodel(Client
*c
);
169 Bool
isurgent(int monitor
, unsigned int t
);
170 Bool
isvisible(Client
*c
, int monitor
);
171 void keypress(XEvent
*e
);
172 void killclient(const char *arg
);
173 void manage(Window w
, XWindowAttributes
*wa
);
174 void mappingnotify(XEvent
*e
);
175 void maprequest(XEvent
*e
);
176 void movemouse(Client
*c
);
177 Client
*nexttiled(Client
*c
, int monitor
);
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
*m
);
203 void updatesizehints(Client
*c
);
204 void updatetitle(Client
*c
);
205 void updatewmhints(Client
*c
);
206 void view(const char *arg
);
207 void viewprevtag(const char *arg
); /* views previous selected tags */
208 int xerror(Display
*dpy
, XErrorEvent
*ee
);
209 int xerrordummy(Display
*dsply
, XErrorEvent
*ee
);
210 int xerrorstart(Display
*dsply
, XErrorEvent
*ee
);
211 void zoom(const char *arg
);
213 void movetomonitor(const char *arg
);
214 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
++) {
346 x
+= textw(m
, 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
) {
371 if((m
->layout
->arrange
== floating
) || c
->isfloating
)
374 togglefloating(NULL
);
377 else if(ev
->button
== Button2
) {
378 if((floating
!= m
->layout
->arrange
) && c
->isfloating
)
379 togglefloating(NULL
);
383 else if(ev
->button
== Button3
&& !c
->isfixed
) {
384 if((floating
== m
->layout
->arrange
) || c
->isfloating
)
387 togglefloating(NULL
);
396 XSetErrorHandler(xerrorstart
);
398 /* this causes an error if some other window manager is running */
399 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
402 eprint("dwm: another window manager is already running\n");
404 XSetErrorHandler(NULL
);
405 xerrorxlib
= XSetErrorHandler(xerror
);
417 for(i
= 0; i
< mcount
; i
++) {
418 Monitor
*m
= &monitors
[i
];
420 XFreeFontSet(dpy
, m
->dc
.font
.set
);
422 XFreeFont(dpy
, m
->dc
.font
.xfont
);
423 XUngrabKey(dpy
, AnyKey
, AnyModifier
, m
->root
);
424 XFreePixmap(dpy
, m
->dc
.drawable
);
425 XFreeGC(dpy
, m
->dc
.gc
);
426 XDestroyWindow(dpy
, m
->barwin
);
427 XFreeCursor(dpy
, cursor
[CurNormal
]);
428 XFreeCursor(dpy
, cursor
[CurResize
]);
429 XFreeCursor(dpy
, cursor
[CurMove
]);
430 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
442 regs
= emallocz(LENGTH(rules
) * sizeof(Regs
));
443 for(i
= 0; i
< LENGTH(rules
); i
++) {
445 reg
= emallocz(sizeof(regex_t
));
446 if(regcomp(reg
, rules
[i
].prop
, REG_EXTENDED
))
449 regs
[i
].propregex
= reg
;
452 reg
= emallocz(sizeof(regex_t
));
453 if(regcomp(reg
, rules
[i
].tags
, REG_EXTENDED
))
456 regs
[i
].tagregex
= reg
;
462 configure(Client
*c
) {
465 ce
.type
= ConfigureNotify
;
473 ce
.border_width
= c
->border
;
475 ce
.override_redirect
= False
;
476 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
480 configurenotify(XEvent
*e
) {
481 XConfigureEvent
*ev
= &e
->xconfigure
;
482 Monitor
*m
= &monitors
[selmonitor
];
484 if(ev
->window
== m
->root
&& (ev
->width
!= m
->sw
|| ev
->height
!= m
->sh
)) {
487 XFreePixmap(dpy
, dc
.drawable
);
488 dc
.drawable
= XCreatePixmap(dpy
, m
->root
, m
->sw
, bh
, DefaultDepth(dpy
, m
->screen
));
489 XResizeWindow(dpy
, m
->barwin
, m
->sw
, bh
);
496 configurerequest(XEvent
*e
) {
498 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
501 if((c
= getclient(ev
->window
))) {
502 Monitor
*m
= &monitors
[c
->monitor
];
503 if(ev
->value_mask
& CWBorderWidth
)
504 c
->border
= ev
->border_width
;
505 if(c
->isfixed
|| c
->isfloating
|| (floating
== m
->layout
->arrange
)) {
506 if(ev
->value_mask
& CWX
)
508 if(ev
->value_mask
& CWY
)
510 if(ev
->value_mask
& CWWidth
)
512 if(ev
->value_mask
& CWHeight
)
514 if((c
->x
- m
->sx
+ c
->w
) > m
->sw
&& c
->isfloating
)
515 c
->x
= m
->sx
+ (m
->sw
/ 2 - c
->w
/ 2); /* center in x direction */
516 if((c
->y
- m
->sy
+ c
->h
) > m
->sh
&& c
->isfloating
)
517 c
->y
= m
->sy
+ (m
->sh
/ 2 - c
->h
/ 2); /* center in y direction */
518 if((ev
->value_mask
& (CWX
| CWY
))
519 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
521 if(isvisible(c
, monitorat()))
522 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
530 wc
.width
= ev
->width
;
531 wc
.height
= ev
->height
;
532 wc
.border_width
= ev
->border_width
;
533 wc
.sibling
= ev
->above
;
534 wc
.stack_mode
= ev
->detail
;
535 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
541 destroynotify(XEvent
*e
) {
543 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
545 if((c
= getclient(ev
->window
)))
552 c
->prev
->next
= c
->next
;
554 c
->next
->prev
= c
->prev
;
557 c
->next
= c
->prev
= NULL
;
561 detachstack(Client
*c
) {
564 for(tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
572 for(i
= 0; i
< mcount
; i
++) {
573 Monitor
*m
= &monitors
[i
];
575 for(j
= 0; j
< LENGTH(tags
); j
++) {
576 m
->dc
.w
= textw(m
, tags
[j
]);
578 drawtext(m
, tags
[j
], m
->dc
.sel
, isurgent(i
, j
));
579 drawsquare(m
, sel
&& sel
->tags
[j
] && sel
->monitor
== selmonitor
, isoccupied(m
, j
), m
->dc
.sel
);
582 drawtext(m
, tags
[j
], m
->dc
.norm
, isurgent(i
, j
));
583 drawsquare(m
, sel
&& sel
->tags
[j
] && sel
->monitor
== selmonitor
, isoccupied(m
, j
), m
->dc
.norm
);
588 drawtext(m
, m
->layout
->symbol
, m
->dc
.norm
, False
);
589 x
= m
->dc
.x
+ m
->dc
.w
;
590 m
->dc
.w
= textw(m
, stext
);
591 m
->dc
.x
= m
->sw
- m
->dc
.w
;
596 drawtext(m
, stext
, m
->dc
.norm
, False
);
597 if((m
->dc
.w
= m
->dc
.x
- x
) > bh
) {
599 if(sel
&& sel
->monitor
== selmonitor
) {
600 drawtext(m
, sel
->name
, m
->dc
.sel
, False
);
601 drawsquare(m
, False
, sel
->isfloating
, m
->dc
.sel
);
604 drawtext(m
, NULL
, m
->dc
.norm
, False
);
606 XCopyArea(dpy
, m
->dc
.drawable
, m
->barwin
, m
->dc
.gc
, 0, 0, m
->sw
, bh
, 0, 0);
612 drawsquare(Monitor
*m
, Bool filled
, Bool empty
, unsigned long col
[ColLast
]) {
615 XRectangle r
= { m
->dc
.x
, m
->dc
.y
, m
->dc
.w
, m
->dc
.h
};
617 gcv
.foreground
= col
[ColFG
];
618 XChangeGC(dpy
, m
->dc
.gc
, GCForeground
, &gcv
);
619 x
= (m
->dc
.font
.ascent
+ m
->dc
.font
.descent
+ 2) / 4;
623 r
.width
= r
.height
= x
+ 1;
624 XFillRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
627 r
.width
= r
.height
= x
;
628 XDrawRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
633 drawtext(Monitor
*m
, const char *text
, unsigned long col
[ColLast
], Bool invert
) {
635 static char buf
[256];
636 unsigned int len
, olen
;
637 XRectangle r
= { m
->dc
.x
, m
->dc
.y
, m
->dc
.w
, m
->dc
.h
};
639 XSetForeground(dpy
, m
->dc
.gc
, col
[invert
? ColFG
: ColBG
]);
640 XFillRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
644 olen
= len
= strlen(text
);
645 if(len
>= sizeof buf
)
646 len
= sizeof buf
- 1;
647 memcpy(buf
, text
, len
);
649 h
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
650 y
= m
->dc
.y
+ (m
->dc
.h
/ 2) - (h
/ 2) + m
->dc
.font
.ascent
;
651 x
= m
->dc
.x
+ (h
/ 2);
652 /* shorten text if necessary */
653 while(len
&& (w
= textnw(m
, buf
, len
)) > m
->dc
.w
- h
)
664 return; /* too long */
665 XSetForeground(dpy
, m
->dc
.gc
, col
[invert
? ColBG
: ColFG
]);
667 XmbDrawString(dpy
, m
->dc
.drawable
, m
->dc
.font
.set
, m
->dc
.gc
, x
, y
, buf
, len
);
669 XDrawString(dpy
, m
->dc
.drawable
, m
->dc
.gc
, x
, y
, buf
, len
);
673 emallocz(unsigned int size
) {
674 void *res
= calloc(1, size
);
677 eprint("fatal: could not malloc() %u bytes\n", size
);
682 enternotify(XEvent
*e
) {
684 XCrossingEvent
*ev
= &e
->xcrossing
;
686 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
);
688 if((c
= getclient(ev
->window
)))
691 selmonitor
= monitorat();
692 fprintf(stderr
, "updating selmonitor %d\n", selmonitor
);
698 eprint(const char *errstr
, ...) {
701 va_start(ap
, errstr
);
702 vfprintf(stderr
, errstr
, ap
);
709 XExposeEvent
*ev
= &e
->xexpose
;
712 if(ev
->window
== monitors
[selmonitor
].barwin
)
718 floating(void) { /* default floating layout */
721 domwfact
= dozoom
= False
;
722 for(c
= clients
; c
; c
= c
->next
)
723 if(isvisible(c
, selmonitor
))
724 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
732 selmonitor
= c
->monitor
;
733 m
= &monitors
[selmonitor
];
734 if(!c
|| (c
&& !isvisible(c
, selmonitor
)))
735 for(c
= stack
; c
&& !isvisible(c
, c
->monitor
); 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
, m
->dc
.sel
[ColBorder
]);
749 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
750 selmonitor
= c
->monitor
;
753 XSetInputFocus(dpy
, m
->root
, RevertToPointerRoot
, CurrentTime
);
758 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
759 XFocusChangeEvent
*ev
= &e
->xfocus
;
761 if(sel
&& ev
->window
!= sel
->win
)
762 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
766 focusnext(const char *arg
) {
771 for(c
= sel
->next
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
773 for(c
= clients
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
781 focusprev(const char *arg
) {
786 for(c
= sel
->prev
; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
788 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
789 for(; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
798 getclient(Window w
) {
801 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
806 getcolor(const char *colstr
, int screen
) {
807 Colormap cmap
= DefaultColormap(dpy
, screen
);
810 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
811 eprint("error, cannot allocate color '%s'\n", colstr
);
819 unsigned char *p
= NULL
;
820 unsigned long n
, extra
;
823 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
824 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
825 if(status
!= Success
)
834 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
839 if(!text
|| size
== 0)
842 XGetTextProperty(dpy
, w
, &name
, atom
);
845 if(name
.encoding
== XA_STRING
)
846 strncpy(text
, (char *)name
.value
, size
- 1);
848 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
850 strncpy(text
, *list
, size
- 1);
851 XFreeStringList(list
);
854 text
[size
- 1] = '\0';
860 grabbuttons(Client
*c
, Bool focused
) {
861 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
864 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
865 GrabModeAsync
, GrabModeSync
, None
, None
);
866 XGrabButton(dpy
, Button1
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
867 GrabModeAsync
, GrabModeSync
, None
, None
);
868 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
869 GrabModeAsync
, GrabModeSync
, None
, None
);
870 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
871 GrabModeAsync
, GrabModeSync
, None
, None
);
873 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
874 GrabModeAsync
, GrabModeSync
, None
, None
);
875 XGrabButton(dpy
, Button2
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
876 GrabModeAsync
, GrabModeSync
, None
, None
);
877 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
878 GrabModeAsync
, GrabModeSync
, None
, None
);
879 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
880 GrabModeAsync
, GrabModeSync
, None
, None
);
882 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
883 GrabModeAsync
, GrabModeSync
, None
, None
);
884 XGrabButton(dpy
, Button3
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
885 GrabModeAsync
, GrabModeSync
, None
, None
);
886 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
887 GrabModeAsync
, GrabModeSync
, None
, None
);
888 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
889 GrabModeAsync
, GrabModeSync
, None
, None
);
892 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
893 GrabModeAsync
, GrabModeSync
, None
, None
);
900 XModifierKeymap
*modmap
;
902 /* init modifier map */
903 modmap
= XGetModifierMapping(dpy
);
904 for(i
= 0; i
< 8; i
++)
905 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
906 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
907 numlockmask
= (1 << i
);
909 XFreeModifiermap(modmap
);
911 for(i
= 0; i
< mcount
; i
++) {
912 Monitor
*m
= &monitors
[i
];
913 XUngrabKey(dpy
, AnyKey
, AnyModifier
, m
->root
);
914 for(j
= 0; j
< LENGTH(keys
); j
++) {
915 code
= XKeysymToKeycode(dpy
, keys
[j
].keysym
);
916 XGrabKey(dpy
, code
, keys
[j
].mod
, m
->root
, True
,
917 GrabModeAsync
, GrabModeAsync
);
918 XGrabKey(dpy
, code
, keys
[j
].mod
| LockMask
, m
->root
, True
,
919 GrabModeAsync
, GrabModeAsync
);
920 XGrabKey(dpy
, code
, keys
[j
].mod
| numlockmask
, m
->root
, True
,
921 GrabModeAsync
, GrabModeAsync
);
922 XGrabKey(dpy
, code
, keys
[j
].mod
| numlockmask
| LockMask
, m
->root
, True
,
923 GrabModeAsync
, GrabModeAsync
);
929 idxoftag(const char *tag
) {
932 for(i
= 0; (i
< LENGTH(tags
)) && (tags
[i
] != tag
); i
++);
933 return (i
< LENGTH(tags
)) ? i
: 0;
937 initfont(Monitor
*m
, const char *fontstr
) {
938 char *def
, **missing
;
943 XFreeFontSet(dpy
, m
->dc
.font
.set
);
944 m
->dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
947 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
948 XFreeStringList(missing
);
951 XFontSetExtents
*font_extents
;
952 XFontStruct
**xfonts
;
954 m
->dc
.font
.ascent
= m
->dc
.font
.descent
= 0;
955 font_extents
= XExtentsOfFontSet(m
->dc
.font
.set
);
956 n
= XFontsOfFontSet(m
->dc
.font
.set
, &xfonts
, &font_names
);
957 for(i
= 0, m
->dc
.font
.ascent
= 0, m
->dc
.font
.descent
= 0; i
< n
; i
++) {
958 if(m
->dc
.font
.ascent
< (*xfonts
)->ascent
)
959 m
->dc
.font
.ascent
= (*xfonts
)->ascent
;
960 if(m
->dc
.font
.descent
< (*xfonts
)->descent
)
961 m
->dc
.font
.descent
= (*xfonts
)->descent
;
967 XFreeFont(dpy
, m
->dc
.font
.xfont
);
968 m
->dc
.font
.xfont
= NULL
;
969 if(!(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
970 && !(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
971 eprint("error, cannot load font: '%s'\n", fontstr
);
972 m
->dc
.font
.ascent
= m
->dc
.font
.xfont
->ascent
;
973 m
->dc
.font
.descent
= m
->dc
.font
.xfont
->descent
;
975 m
->dc
.font
.height
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
979 isoccupied(Monitor
*m
, unsigned int t
) {
982 for(c
= clients
; c
; c
= c
->next
)
983 if(c
->tags
[t
] && c
->monitor
== selmonitor
)
989 isprotodel(Client
*c
) {
994 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
995 for(i
= 0; !ret
&& i
< n
; i
++)
996 if(protocols
[i
] == wmatom
[WMDelete
])
1004 isurgent(int monitor
, unsigned int t
) {
1007 for(c
= clients
; c
; c
= c
->next
)
1008 if(c
->monitor
== monitor
&& c
->isurgent
&& c
->tags
[t
])
1014 isvisible(Client
*c
, int monitor
) {
1017 for(i
= 0; i
< LENGTH(tags
); i
++)
1018 if(c
->tags
[i
] && monitors
[c
->monitor
].seltags
[i
] && c
->monitor
== monitor
)
1024 keypress(XEvent
*e
) {
1030 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1031 for(i
= 0; i
< LENGTH(keys
); i
++)
1032 if(keysym
== keys
[i
].keysym
1033 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
1036 keys
[i
].func(keys
[i
].arg
);
1041 killclient(const char *arg
) {
1046 if(isprotodel(sel
)) {
1047 ev
.type
= ClientMessage
;
1048 ev
.xclient
.window
= sel
->win
;
1049 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1050 ev
.xclient
.format
= 32;
1051 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1052 ev
.xclient
.data
.l
[1] = CurrentTime
;
1053 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
1056 XKillClient(dpy
, sel
->win
);
1060 manage(Window w
, XWindowAttributes
*wa
) {
1061 Client
*c
, *t
= NULL
;
1067 c
= emallocz(sizeof(Client
));
1068 c
->tags
= emallocz(sizeof initags
);
1073 m
= &monitors
[c
->monitor
];
1075 c
->x
= wa
->x
+ m
->sx
;
1076 c
->y
= wa
->y
+ m
->sy
;
1079 c
->oldborder
= wa
->border_width
;
1081 if(c
->w
== m
->sw
&& c
->h
== m
->sh
) {
1084 c
->border
= wa
->border_width
;
1087 if(c
->x
+ c
->w
+ 2 * c
->border
> m
->wax
+ m
->waw
)
1088 c
->x
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1089 if(c
->y
+ c
->h
+ 2 * c
->border
> m
->way
+ m
->wah
)
1090 c
->y
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1095 c
->border
= BORDERPX
;
1097 wc
.border_width
= c
->border
;
1098 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1099 XSetWindowBorder(dpy
, w
, m
->dc
.norm
[ColBorder
]);
1100 configure(c
); /* propagates border_width, if size doesn't change */
1102 XSelectInput(dpy
, w
, EnterWindowMask
| FocusChangeMask
| PropertyChangeMask
| StructureNotifyMask
);
1103 grabbuttons(c
, False
);
1105 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
1106 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
1108 memcpy(c
->tags
, t
->tags
, sizeof initags
);
1110 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
1113 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1115 XMapWindow(dpy
, c
->win
);
1116 setclientstate(c
, NormalState
);
1121 mappingnotify(XEvent
*e
) {
1122 XMappingEvent
*ev
= &e
->xmapping
;
1124 XRefreshKeyboardMapping(ev
);
1125 if(ev
->request
== MappingKeyboard
)
1130 maprequest(XEvent
*e
) {
1131 static XWindowAttributes wa
;
1132 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1134 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1136 if(wa
.override_redirect
)
1138 if(!getclient(ev
->window
))
1139 manage(ev
->window
, &wa
);
1148 XQueryPointer(dpy
, monitors
[selmonitor
].root
, &win
, &win
, &x
, &y
, &i
, &i
, &mask
);
1149 for(i
= 0; i
< mcount
; i
++) {
1150 fprintf(stderr
, "checking monitor[%d]: %d %d %d %d\n", i
, monitors
[i
].sx
, monitors
[i
].sy
, monitors
[i
].sw
, monitors
[i
].sh
);
1151 if((x
>= monitors
[i
].sx
&& x
< monitors
[i
].sx
+ monitors
[i
].sw
)
1152 && (y
>= monitors
[i
].sy
&& y
< monitors
[i
].sy
+ monitors
[i
].sh
)) {
1153 fprintf(stderr
, "%d,%d -> %d\n", x
, y
, i
);
1157 fprintf(stderr
, "?,? -> 0\n");
1162 movemouse(Client
*c
) {
1163 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1170 if(XGrabPointer(dpy
, monitors
[selmonitor
].root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1171 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1173 XQueryPointer(dpy
, monitors
[selmonitor
].root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1175 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1178 XUngrabPointer(dpy
, CurrentTime
);
1180 case ConfigureRequest
:
1183 handler
[ev
.type
](&ev
);
1187 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1188 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1189 Monitor
*m
= &monitors
[monitorat()];
1190 if(abs(m
->wax
- nx
) < SNAP
)
1192 else if(abs((m
->wax
+ m
->waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
1193 nx
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1194 if(abs(m
->way
- ny
) < SNAP
)
1196 else if(abs((m
->way
+ m
->wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
1197 ny
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1198 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1199 memcpy(c
->tags
, monitors
[monitorat()].seltags
, sizeof initags
);
1206 nexttiled(Client
*c
, int monitor
) {
1207 for(; c
&& (c
->isfloating
|| !isvisible(c
, monitor
)); c
= c
->next
);
1212 propertynotify(XEvent
*e
) {
1215 XPropertyEvent
*ev
= &e
->xproperty
;
1217 if(ev
->state
== PropertyDelete
)
1218 return; /* ignore */
1219 if((c
= getclient(ev
->window
))) {
1222 case XA_WM_TRANSIENT_FOR
:
1223 XGetTransientForHint(dpy
, c
->win
, &trans
);
1224 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1227 case XA_WM_NORMAL_HINTS
:
1235 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1244 quit(const char *arg
) {
1245 readin
= running
= False
;
1249 reapply(const char *arg
) {
1250 static Bool zerotags
[LENGTH(tags
)] = { 0 };
1253 for(c
= clients
; c
; c
= c
->next
) {
1254 memcpy(c
->tags
, zerotags
, sizeof zerotags
);
1261 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1263 //Monitor scr = monitors[monitorat()];
1264 // c->monitor = monitorat();
1267 /* set minimum possible */
1273 /* temporarily remove base dimensions */
1277 /* adjust for aspect limits */
1278 if (c
->minay
> 0 && c
->maxay
> 0 && c
->minax
> 0 && c
->maxax
> 0) {
1279 if (w
* c
->maxay
> h
* c
->maxax
)
1280 w
= h
* c
->maxax
/ c
->maxay
;
1281 else if (w
* c
->minay
< h
* c
->minax
)
1282 h
= w
* c
->minay
/ c
->minax
;
1285 /* adjust for increment value */
1291 /* restore base dimensions */
1295 if(c
->minw
> 0 && w
< c
->minw
)
1297 if(c
->minh
> 0 && h
< c
->minh
)
1299 if(c
->maxw
> 0 && w
> c
->maxw
)
1301 if(c
->maxh
> 0 && h
> c
->maxh
)
1304 if(w
<= 0 || h
<= 0)
1306 /* TODO: offscreen appearance fixes */
1309 x = scr.sw - w - 2 * c->border;
1311 y = scr.sh - h - 2 * c->border;
1312 if(x + w + 2 * c->border < scr.sx)
1314 if(y + h + 2 * c->border < scr.sy)
1317 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1320 c
->w
= wc
.width
= w
;
1321 c
->h
= wc
.height
= h
;
1322 wc
.border_width
= c
->border
;
1323 XConfigureWindow(dpy
, c
->win
, CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
, &wc
);
1330 resizemouse(Client
*c
) {
1337 if(XGrabPointer(dpy
, monitors
[selmonitor
].root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1338 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1340 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1342 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1345 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1346 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1347 XUngrabPointer(dpy
, CurrentTime
);
1348 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1350 case ConfigureRequest
:
1353 handler
[ev
.type
](&ev
);
1357 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
1359 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
1361 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1377 if(sel
->isfloating
|| (monitors
[selmonitor
].layout
->arrange
== floating
))
1378 XRaiseWindow(dpy
, sel
->win
);
1379 if(monitors
[selmonitor
].layout
->arrange
!= floating
) {
1380 wc
.stack_mode
= Below
;
1381 wc
.sibling
= monitors
[selmonitor
].barwin
;
1382 if(!sel
->isfloating
) {
1383 XConfigureWindow(dpy
, sel
->win
, CWSibling
| CWStackMode
, &wc
);
1384 wc
.sibling
= sel
->win
;
1386 for(i
= 0; i
< mcount
; i
++) {
1387 for(c
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
)) {
1390 XConfigureWindow(dpy
, c
->win
, CWSibling
| CWStackMode
, &wc
);
1391 wc
.sibling
= c
->win
;
1396 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1402 char buf
[sizeof stext
];
1405 unsigned int len
, offset
;
1408 /* main event loop, also reads status text from stdin */
1410 xfd
= ConnectionNumber(dpy
);
1413 len
= sizeof stext
- 1;
1414 buf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1418 FD_SET(STDIN_FILENO
, &rd
);
1420 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1423 eprint("select failed\n");
1425 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1426 switch((r
= read(STDIN_FILENO
, buf
+ offset
, len
- offset
))) {
1428 strncpy(stext
, strerror(errno
), len
);
1432 strncpy(stext
, "EOF", 4);
1436 for(p
= buf
+ offset
; r
> 0; p
++, r
--, offset
++)
1437 if(*p
== '\n' || *p
== '\0') {
1439 strncpy(stext
, buf
, len
);
1440 p
+= r
- 1; /* p is buf + offset + r - 1 */
1441 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1444 memmove(buf
, p
- r
+ 1, r
);
1451 while(XPending(dpy
)) {
1452 XNextEvent(dpy
, &ev
);
1453 if(handler
[ev
.type
])
1454 (handler
[ev
.type
])(&ev
); /* call handler */
1461 unsigned int i
, j
, num
;
1462 Window
*wins
, d1
, d2
;
1463 XWindowAttributes wa
;
1465 for(i
= 0; i
< mcount
; i
++) {
1466 Monitor
*m
= &monitors
[i
];
1468 if(XQueryTree(dpy
, m
->root
, &d1
, &d2
, &wins
, &num
)) {
1469 for(j
= 0; j
< num
; j
++) {
1470 if(!XGetWindowAttributes(dpy
, wins
[j
], &wa
)
1471 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[j
], &d1
))
1473 if(wa
.map_state
== IsViewable
|| getstate(wins
[j
]) == IconicState
)
1474 manage(wins
[j
], &wa
);
1476 for(j
= 0; j
< num
; j
++) { /* now the transients */
1477 if(!XGetWindowAttributes(dpy
, wins
[j
], &wa
))
1479 if(XGetTransientForHint(dpy
, wins
[j
], &d1
)
1480 && (wa
.map_state
== IsViewable
|| getstate(wins
[j
]) == IconicState
))
1481 manage(wins
[j
], &wa
);
1490 setclientstate(Client
*c
, long state
) {
1491 long data
[] = {state
, None
};
1493 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1494 PropModeReplace
, (unsigned char *)data
, 2);
1498 setlayout(const char *arg
) {
1500 Monitor
*m
= &monitors
[monitorat()];
1504 if(m
->layout
== &layouts
[LENGTH(layouts
)])
1505 m
->layout
= &layouts
[0];
1508 for(i
= 0; i
< LENGTH(layouts
); i
++)
1509 if(!strcmp(arg
, layouts
[i
].symbol
))
1511 if(i
== LENGTH(layouts
))
1513 m
->layout
= &layouts
[i
];
1522 setmwfact(const char *arg
) {
1525 Monitor
*m
= &monitors
[monitorat()];
1529 /* arg handling, manipulate mwfact */
1532 else if(sscanf(arg
, "%lf", &delta
) == 1) {
1533 if(arg
[0] == '+' || arg
[0] == '-')
1539 else if(m
->mwfact
> 0.9)
1547 unsigned int i
, j
, k
;
1549 XSetWindowAttributes wa
;
1550 XineramaScreenInfo
*info
= NULL
;
1553 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1554 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1555 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1556 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1557 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1558 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1561 wa
.cursor
= cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1562 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1563 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1565 // init screens/monitors first
1567 if((isxinerama
= XineramaIsActive(dpy
)))
1568 info
= XineramaQueryScreens(dpy
, &mcount
);
1569 monitors
= emallocz(mcount
* sizeof(Monitor
));
1571 for(i
= 0; i
< mcount
; i
++) {
1575 m
->screen
= isxinerama
? 0 : i
;
1576 m
->root
= RootWindow(dpy
, m
->screen
);
1578 if (mcount
!= 1 && isxinerama
) {
1579 m
->sx
= info
[i
].x_org
;
1580 m
->sy
= info
[i
].y_org
;
1581 m
->sw
= info
[i
].width
;
1582 m
->sh
= info
[i
].height
;
1583 fprintf(stderr
, "monitor[%d]: %d,%d,%d,%d\n", i
, m
->sx
, m
->sy
, m
->sw
, m
->sh
);
1588 m
->sw
= DisplayWidth(dpy
, m
->screen
);
1589 m
->sh
= DisplayHeight(dpy
, m
->screen
);
1592 m
->seltags
= emallocz(sizeof initags
);
1593 m
->prevtags
= emallocz(sizeof initags
);
1595 memcpy(m
->seltags
, initags
, sizeof initags
);
1596 memcpy(m
->prevtags
, initags
, sizeof initags
);
1598 /* init appearance */
1599 m
->dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
, m
->screen
);
1600 m
->dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
, m
->screen
);
1601 m
->dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
, m
->screen
);
1602 m
->dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
, m
->screen
);
1603 m
->dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
, m
->screen
);
1604 m
->dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
, m
->screen
);
1606 m
->dc
.h
= bh
= m
->dc
.font
.height
+ 2;
1610 m
->layout
= &layouts
[0];
1611 for(blw
= k
= 0; k
< LENGTH(layouts
); k
++) {
1612 j
= textw(m
, layouts
[k
].symbol
);
1617 // TODO: bpos per screen?
1619 wa
.override_redirect
= 1;
1620 wa
.background_pixmap
= ParentRelative
;
1621 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
1624 m
->barwin
= XCreateWindow(dpy
, m
->root
, m
->sx
, m
->sy
, m
->sw
, bh
, 0,
1625 DefaultDepth(dpy
, m
->screen
), CopyFromParent
, DefaultVisual(dpy
, m
->screen
),
1626 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
1627 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1629 XMapRaised(dpy
, m
->barwin
);
1630 strcpy(stext
, "dwm-"VERSION
);
1631 m
->dc
.drawable
= XCreatePixmap(dpy
, m
->root
, m
->sw
, bh
, DefaultDepth(dpy
, m
->screen
));
1632 m
->dc
.gc
= XCreateGC(dpy
, m
->root
, 0, 0);
1633 XSetLineAttributes(dpy
, m
->dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1635 XSetFont(dpy
, m
->dc
.gc
, m
->dc
.font
.xfont
->fid
);
1637 /* EWMH support per monitor */
1638 XChangeProperty(dpy
, m
->root
, netatom
[NetSupported
], XA_ATOM
, 32,
1639 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1641 /* select for events */
1642 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
1643 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
1644 XChangeWindowAttributes(dpy
, m
->root
, CWEventMask
| CWCursor
, &wa
);
1645 XSelectInput(dpy
, m
->root
, wa
.event_mask
);
1656 selmonitor
= monitorat();
1657 fprintf(stderr
, "selmonitor == %d\n", selmonitor
);
1661 spawn(const char *arg
) {
1662 static char *shell
= NULL
;
1664 if(!shell
&& !(shell
= getenv("SHELL")))
1668 /* The double-fork construct avoids zombie processes and keeps the code
1669 * clean from stupid signal handlers. */
1673 close(ConnectionNumber(dpy
));
1675 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1676 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1685 tag(const char *arg
) {
1690 for(i
= 0; i
< LENGTH(tags
); i
++)
1691 sel
->tags
[i
] = (NULL
== arg
);
1692 sel
->tags
[idxoftag(arg
)] = True
;
1697 textnw(Monitor
*m
, const char *text
, unsigned int len
) {
1700 if(m
->dc
.font
.set
) {
1701 XmbTextExtents(m
->dc
.font
.set
, text
, len
, NULL
, &r
);
1704 return XTextWidth(m
->dc
.font
.xfont
, text
, len
);
1708 textw(Monitor
*m
, const char *text
) {
1709 return textnw(m
, text
, strlen(text
)) + m
->dc
.font
.height
;
1714 unsigned int i
, j
, n
, nx
, ny
, nw
, nh
, mw
, th
;
1717 domwfact
= dozoom
= True
;
1719 nx
= ny
= nw
= 0; /* gcc stupidity requires this */
1721 for (i
= 0; i
< mcount
; i
++) {
1722 Monitor
*m
= &monitors
[i
];
1724 for(n
= 0, c
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
))
1727 for(j
= 0, c
= mc
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
)) {
1729 mw
= (n
== 1) ? m
->waw
: m
->mwfact
* m
->waw
;
1730 th
= (n
> 1) ? m
->wah
/ (n
- 1) : 0;
1731 if(n
> 1 && th
< bh
)
1733 if(j
== 0) { /* master */
1736 nw
= mw
- 2 * c
->border
;
1737 nh
= m
->wah
- 2 * c
->border
;
1739 else { /* tile window */
1742 nx
+= mc
->w
+ 2 * mc
->border
;
1743 nw
= m
->waw
- mw
- 2 * c
->border
;
1745 if(j
+ 1 == n
) /* remainder */
1746 nh
= (m
->way
+ m
->wah
) - ny
- 2 * c
->border
;
1748 nh
= th
- 2 * c
->border
;
1750 fprintf(stderr
, "tile(%d, %d, %d, %d)\n", nx
, ny
, nw
, nh
);
1751 resize(c
, nx
, ny
, nw
, nh
, RESIZEHINTS
);
1752 if((RESIZEHINTS
) && ((c
->h
< bh
) || (c
->h
> nh
) || (c
->w
< bh
) || (c
->w
> nw
)))
1753 /* client doesn't accept size constraints */
1754 resize(c
, nx
, ny
, nw
, nh
, False
);
1755 if(n
> 1 && th
!= m
->wah
)
1756 ny
= c
->y
+ c
->h
+ 2 * c
->border
;
1761 fprintf(stderr
, "done\n");
1764 togglebar(const char *arg
) {
1766 bpos
= (BARPOS
== BarOff
) ? BarTop
: BARPOS
;
1769 updatebarpos(&monitors
[monitorat()]);
1774 togglefloating(const char *arg
) {
1777 sel
->isfloating
= !sel
->isfloating
;
1779 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1784 toggletag(const char *arg
) {
1790 sel
->tags
[i
] = !sel
->tags
[i
];
1791 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1792 if(j
== LENGTH(tags
))
1793 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1798 toggleview(const char *arg
) {
1801 Monitor
*m
= &monitors
[monitorat()];
1804 m
->seltags
[i
] = !m
->seltags
[i
];
1805 for(j
= 0; j
< LENGTH(tags
) && !m
->seltags
[j
]; j
++);
1806 if(j
== LENGTH(tags
))
1807 m
->seltags
[i
] = True
; /* at least one tag must be viewed */
1815 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1816 c
->isbanned
= False
;
1820 unmanage(Client
*c
) {
1823 wc
.border_width
= c
->oldborder
;
1824 /* The server grab construct avoids race conditions. */
1826 XSetErrorHandler(xerrordummy
);
1827 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1832 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1833 setclientstate(c
, WithdrawnState
);
1837 XSetErrorHandler(xerror
);
1843 unmapnotify(XEvent
*e
) {
1845 XUnmapEvent
*ev
= &e
->xunmap
;
1847 if((c
= getclient(ev
->window
)))
1852 updatebarpos(Monitor
*m
) {
1863 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
);
1867 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
+ m
->wah
);
1870 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
- bh
);
1874 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1878 updatesizehints(Client
*c
) {
1882 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1884 c
->flags
= size
.flags
;
1885 if(c
->flags
& PBaseSize
) {
1886 c
->basew
= size
.base_width
;
1887 c
->baseh
= size
.base_height
;
1889 else if(c
->flags
& PMinSize
) {
1890 c
->basew
= size
.min_width
;
1891 c
->baseh
= size
.min_height
;
1894 c
->basew
= c
->baseh
= 0;
1895 if(c
->flags
& PResizeInc
) {
1896 c
->incw
= size
.width_inc
;
1897 c
->inch
= size
.height_inc
;
1900 c
->incw
= c
->inch
= 0;
1901 if(c
->flags
& PMaxSize
) {
1902 c
->maxw
= size
.max_width
;
1903 c
->maxh
= size
.max_height
;
1906 c
->maxw
= c
->maxh
= 0;
1907 if(c
->flags
& PMinSize
) {
1908 c
->minw
= size
.min_width
;
1909 c
->minh
= size
.min_height
;
1911 else if(c
->flags
& PBaseSize
) {
1912 c
->minw
= size
.base_width
;
1913 c
->minh
= size
.base_height
;
1916 c
->minw
= c
->minh
= 0;
1917 if(c
->flags
& PAspect
) {
1918 c
->minax
= size
.min_aspect
.x
;
1919 c
->maxax
= size
.max_aspect
.x
;
1920 c
->minay
= size
.min_aspect
.y
;
1921 c
->maxay
= size
.max_aspect
.y
;
1924 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1925 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1926 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1930 updatetitle(Client
*c
) {
1931 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1932 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1936 updatewmhints(Client
*c
) {
1939 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1940 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1945 /* There's no way to check accesses to destroyed windows, thus those cases are
1946 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1947 * default error handler, which may call exit. */
1949 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1950 if(ee
->error_code
== BadWindow
1951 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1952 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1953 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1954 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1955 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1956 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1957 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1959 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1960 ee
->request_code
, ee
->error_code
);
1961 return xerrorxlib(dpy
, ee
); /* may call exit */
1965 xerrordummy(Display
*dsply
, XErrorEvent
*ee
) {
1969 /* Startup Error handler to check if another window manager
1970 * is already running. */
1972 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
1978 view(const char *arg
) {
1981 Monitor
*m
= &monitors
[monitorat()];
1983 memcpy(m
->prevtags
, m
->seltags
, sizeof initags
);
1984 for(i
= 0; i
< LENGTH(tags
); i
++)
1985 m
->seltags
[i
] = (NULL
== arg
);
1986 m
->seltags
[idxoftag(arg
)] = True
;
1991 viewprevtag(const char *arg
) {
1992 static Bool tmp
[LENGTH(tags
)];
1994 Monitor
*m
= &monitors
[monitorat()];
1996 memcpy(tmp
, m
->seltags
, sizeof initags
);
1997 memcpy(m
->seltags
, m
->prevtags
, sizeof initags
);
1998 memcpy(m
->prevtags
, tmp
, sizeof initags
);
2003 zoom(const char *arg
) {
2006 if(!sel
|| !dozoom
|| sel
->isfloating
)
2008 if((c
= sel
) == nexttiled(clients
, c
->monitor
))
2009 if(!(c
= nexttiled(c
->next
, c
->monitor
)))
2018 movetomonitor(const char *arg
) {
2020 sel
->monitor
= arg
? atoi(arg
) : (sel
->monitor
+1) % mcount
;
2022 memcpy(sel
->tags
, monitors
[sel
->monitor
].seltags
, sizeof initags
);
2023 resize(sel
, monitors
[sel
->monitor
].wax
, monitors
[sel
->monitor
].way
, sel
->w
, sel
->h
, True
);
2029 selectmonitor(const char *arg
) {
2030 Monitor
*m
= &monitors
[arg
? atoi(arg
) : (monitorat()+1) % mcount
];
2032 XWarpPointer(dpy
, None
, m
->root
, 0, 0, 0, 0, m
->wax
+m
->waw
/2, m
->way
+m
->wah
/2);
2038 main(int argc
, char *argv
[]) {
2039 if(argc
== 2 && !strcmp("-v", argv
[1]))
2040 eprint("dwm-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
2041 "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
2043 eprint("usage: dwm [-v]\n");
2045 setlocale(LC_CTYPE
, "");
2046 if(!(dpy
= XOpenDisplay(0)))
2047 eprint("dwm: cannot open display\n");