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 Monitor Monitor
;
64 typedef struct Client Client
;
68 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
69 int minax
, maxax
, minay
, maxay
;
71 unsigned int border
, oldborder
;
72 Bool isbanned
, isfixed
, isfloating
, isurgent
;
83 unsigned long norm
[ColLast
];
84 unsigned long sel
[ColLast
];
94 } DC
; /* draw context */
99 void (*func
)(const char *arg
);
105 void (*arrange
)(Monitor
*);
122 int sx
, sy
, sw
, sh
, wax
, way
, wah
, waw
;
131 /* function declarations */
132 void applyrules(Client
*c
);
133 void arrange(Monitor
*m
);
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
);
147 void drawbar(Monitor
*m
);
148 void drawsquare(Monitor
*m
, Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
149 void drawtext(Monitor
*m
, 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(Monitor
*m
); /* 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
);
161 Monitor
*getmonitor(Window barwin
);
162 long getstate(Window w
);
163 Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
164 void grabbuttons(Client
*c
, Bool focused
);
166 unsigned int idxoftag(const char *tag
);
167 void initfont(const char *fontstr
);
168 Bool
isoccupied(Monitor
*monitor
, unsigned int t
);
169 Bool
isprotodel(Client
*c
);
170 Bool
isurgent(Monitor
*monitor
, unsigned int t
);
171 Bool
isvisible(Client
*c
, Monitor
*m
);
172 void keypress(XEvent
*e
);
173 void killclient(const char *arg
);
174 void manage(Window w
, XWindowAttributes
*wa
);
175 void mappingnotify(XEvent
*e
);
176 void maprequest(XEvent
*e
);
177 Monitor
*monitorat(void);
178 void movemouse(Client
*c
);
179 Client
*nexttiled(Client
*c
, Monitor
*monitor
);
180 void propertynotify(XEvent
*e
);
181 void quit(const char *arg
);
182 void reapply(const char *arg
);
183 void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
);
184 void resizemouse(Client
*c
);
185 void restack(Monitor
*m
);
188 void setclientstate(Client
*c
, long state
);
189 void setlayout(const char *arg
);
190 void setmwfact(const char *arg
);
192 void spawn(const char *arg
);
193 void tag(const char *arg
);
194 unsigned int textnw(const char *text
, unsigned int len
);
195 unsigned int textw(const char *text
);
196 void tile(Monitor
*m
);
197 void togglebar(const char *arg
);
198 void togglefloating(const char *arg
);
199 void toggletag(const char *arg
);
200 void toggleview(const char *arg
);
201 void unban(Client
*c
);
202 void unmanage(Client
*c
);
203 void unmapnotify(XEvent
*e
);
204 void updatebarpos(Monitor
*m
);
205 void updatesizehints(Client
*c
);
206 void updatetitle(Client
*c
);
207 void updatewmhints(Client
*c
);
208 void view(const char *arg
);
209 void viewprevtag(const char *arg
); /* views previous selected tags */
210 int xerror(Display
*dpy
, XErrorEvent
*ee
);
211 int xerrordummy(Display
*dsply
, XErrorEvent
*ee
);
212 int xerrorstart(Display
*dsply
, XErrorEvent
*ee
);
213 void zoom(const char *arg
);
214 void movetomonitor(const char *arg
);
215 void selectmonitor(const char *arg
);
222 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
223 unsigned int bh
, bpos
;
224 unsigned int blw
= 0;
225 unsigned int numlockmask
= 0;
226 void (*handler
[LASTEvent
]) (XEvent
*) = {
227 [ButtonPress
] = buttonpress
,
228 [ConfigureRequest
] = configurerequest
,
229 [ConfigureNotify
] = configurenotify
,
230 [DestroyNotify
] = destroynotify
,
231 [EnterNotify
] = enternotify
,
234 [KeyPress
] = keypress
,
235 [MappingNotify
] = mappingnotify
,
236 [MapRequest
] = maprequest
,
237 [PropertyNotify
] = propertynotify
,
238 [UnmapNotify
] = unmapnotify
240 Atom wmatom
[WMLast
], netatom
[NetLast
];
241 Bool isxinerama
= False
;
242 Bool domwfact
= True
;
244 Bool otherwm
, readin
;
246 Client
*clients
= NULL
;
248 Client
*stack
= NULL
;
249 Cursor cursor
[CurLast
];
256 /* configuration, allows nested code to access above variables */
259 //Bool prevtags[LENGTH(tags)];
261 /* function implementations */
263 applyrules(Client
*c
) {
264 static char buf
[512];
267 Bool matched_tag
= False
;
268 Bool matched_monitor
= False
;
269 XClassHint ch
= { 0 };
272 XGetClassHint(dpy
, c
->win
, &ch
);
273 snprintf(buf
, sizeof buf
, "%s:%s:%s",
274 ch
.res_class
? ch
.res_class
: "",
275 ch
.res_name
? ch
.res_name
: "", c
->name
);
276 for(i
= 0; i
< LENGTH(rules
); i
++)
277 if(regs
[i
].propregex
&& !regexec(regs
[i
].propregex
, buf
, 1, &tmp
, 0)) {
278 if (rules
[i
].monitor
>= 0 && rules
[i
].monitor
< mcount
) {
279 matched_monitor
= True
;
280 c
->monitor
= &monitors
[rules
[i
].monitor
];
283 c
->isfloating
= rules
[i
].isfloating
;
284 for(j
= 0; regs
[i
].tagregex
&& j
< LENGTH(tags
); j
++) {
285 if(!regexec(regs
[i
].tagregex
, tags
[j
], 1, &tmp
, 0)) {
296 memcpy(c
->tags
, monitorat()->seltags
, sizeof initags
);
297 if (!matched_monitor
)
298 c
->monitor
= monitorat();
302 arrange(Monitor
*m
) {
306 for(c
= clients
; c
; c
= c
->next
)
307 if(isvisible(c
, c
->monitor
))
313 m
->layout
->arrange(m
);
315 for(i
= 0; i
< mcount
; i
++)
316 m
->layout
->arrange(&monitors
[i
]);
330 attachstack(Client
*c
) {
339 XMoveWindow(dpy
, c
->win
, c
->x
+ 3 * c
->monitor
->sw
, c
->y
);
344 buttonpress(XEvent
*e
) {
347 XButtonPressedEvent
*ev
= &e
->xbutton
;
349 Monitor
*m
= monitorat();
351 if(ev
->window
== m
->barwin
) {
353 for(i
= 0; i
< LENGTH(tags
); i
++) {
356 if(ev
->button
== Button1
) {
357 if(ev
->state
& MODKEY
)
362 else if(ev
->button
== Button3
) {
363 if(ev
->state
& MODKEY
)
371 if((ev
->x
< x
+ blw
) && ev
->button
== Button1
)
374 else if((c
= getclient(ev
->window
))) {
376 if(CLEANMASK(ev
->state
) != MODKEY
)
378 if(ev
->button
== Button1
) {
382 else if(ev
->button
== Button2
) {
383 if((floating
!= m
->layout
->arrange
) && c
->isfloating
)
384 togglefloating(NULL
);
388 else if(ev
->button
== Button3
&& !c
->isfixed
) {
398 XSetErrorHandler(xerrorstart
);
400 /* this causes an error if some other window manager is running */
401 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
404 eprint("dwm: another window manager is already running\n");
406 XSetErrorHandler(NULL
);
407 xerrorxlib
= XSetErrorHandler(xerror
);
420 XFreeFontSet(dpy
, dc
.font
.set
);
422 XFreeFont(dpy
, dc
.font
.xfont
);
424 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
425 XFreePixmap(dpy
, dc
.drawable
);
427 XFreeCursor(dpy
, cursor
[CurNormal
]);
428 XFreeCursor(dpy
, cursor
[CurResize
]);
429 XFreeCursor(dpy
, cursor
[CurMove
]);
430 for(i
= 0; i
< mcount
; i
++)
431 XDestroyWindow(dpy
, monitors
[i
].barwin
);
433 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
443 regs
= emallocz(LENGTH(rules
) * sizeof(Regs
));
444 for(i
= 0; i
< LENGTH(rules
); i
++) {
446 reg
= emallocz(sizeof(regex_t
));
447 if(regcomp(reg
, rules
[i
].prop
, REG_EXTENDED
))
450 regs
[i
].propregex
= reg
;
453 reg
= emallocz(sizeof(regex_t
));
454 if(regcomp(reg
, rules
[i
].tags
, REG_EXTENDED
))
457 regs
[i
].tagregex
= reg
;
463 configure(Client
*c
) {
466 ce
.type
= ConfigureNotify
;
474 ce
.border_width
= c
->border
;
476 ce
.override_redirect
= False
;
477 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
481 configurenotify(XEvent
*e
) {
482 XConfigureEvent
*ev
= &e
->xconfigure
;
483 Monitor
*m
= selmonitor
;
485 if(ev
->window
== root
&& (ev
->width
!= m
->sw
|| ev
->height
!= m
->sh
)) {
486 /* TODO -- update Xinerama dimensions here */
489 XFreePixmap(dpy
, dc
.drawable
);
490 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(root
, screen
), bh
, DefaultDepth(dpy
, screen
));
491 XResizeWindow(dpy
, m
->barwin
, m
->sw
, bh
);
498 configurerequest(XEvent
*e
) {
500 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
503 if((c
= getclient(ev
->window
))) {
504 Monitor
*m
= c
->monitor
;
505 if(ev
->value_mask
& CWBorderWidth
)
506 c
->border
= ev
->border_width
;
507 if(c
->isfixed
|| c
->isfloating
|| (floating
== m
->layout
->arrange
)) {
508 if(ev
->value_mask
& CWX
)
510 if(ev
->value_mask
& CWY
)
512 if(ev
->value_mask
& CWWidth
)
514 if(ev
->value_mask
& CWHeight
)
516 if((c
->x
- m
->sx
+ c
->w
) > m
->sw
&& c
->isfloating
)
517 c
->x
= m
->sx
+ (m
->sw
/ 2 - c
->w
/ 2); /* center in x direction */
518 if((c
->y
- m
->sy
+ c
->h
) > m
->sh
&& c
->isfloating
)
519 c
->y
= m
->sy
+ (m
->sh
/ 2 - c
->h
/ 2); /* center in y direction */
520 if((ev
->value_mask
& (CWX
| CWY
))
521 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
523 if(isvisible(c
, monitorat()))
524 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
532 wc
.width
= ev
->width
;
533 wc
.height
= ev
->height
;
534 wc
.border_width
= ev
->border_width
;
535 wc
.sibling
= ev
->above
;
536 wc
.stack_mode
= ev
->detail
;
537 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
543 destroynotify(XEvent
*e
) {
545 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
547 if((c
= getclient(ev
->window
)))
554 c
->prev
->next
= c
->next
;
556 c
->next
->prev
= c
->prev
;
559 c
->next
= c
->prev
= NULL
;
563 detachstack(Client
*c
) {
566 for(tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
571 drawbar(Monitor
*m
) {
576 for(c
= stack
; c
&& !isvisible(c
, m
); c
= c
->snext
);
577 for(j
= 0; j
< LENGTH(tags
); j
++) {
578 dc
.w
= textw(tags
[j
]);
580 drawtext(m
, tags
[j
], dc
.sel
, isurgent(m
, j
));
581 drawsquare(m
, c
&& c
->tags
[j
] && c
->monitor
== m
,
582 isoccupied(m
, j
), isurgent(m
, j
), dc
.sel
);
585 drawtext(m
, tags
[j
], dc
.norm
, isurgent(m
, j
));
586 drawsquare(m
, c
&& c
->tags
[j
] && c
->monitor
== m
,
587 isoccupied(m
, j
), isurgent(m
, j
), dc
.norm
);
592 drawtext(m
, m
->layout
->symbol
, dc
.norm
, False
);
594 if(m
== selmonitor
) {
601 drawtext(m
, stext
, dc
.norm
, False
);
605 if((dc
.w
= dc
.x
- x
) > bh
) {
608 drawtext(m
, c
->name
, dc
.sel
, False
);
609 drawsquare(m
, False
, c
->isfloating
, False
, dc
.sel
);
612 drawtext(m
, NULL
, dc
.norm
, False
);
614 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->sw
, bh
, 0, 0);
619 drawsquare(Monitor
*m
, Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
622 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
624 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
625 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
626 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
630 r
.width
= r
.height
= x
+ 1;
631 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
634 r
.width
= r
.height
= x
;
635 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
640 drawtext(Monitor
*m
, const char *text
, unsigned long col
[ColLast
], Bool invert
) {
642 static char buf
[256];
643 unsigned int len
, olen
;
644 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
646 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
647 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
651 olen
= len
= strlen(text
);
652 if(len
>= sizeof buf
)
653 len
= sizeof buf
- 1;
654 memcpy(buf
, text
, len
);
656 h
= dc
.font
.ascent
+ dc
.font
.descent
;
657 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
659 /* shorten text if necessary */
660 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
671 return; /* too long */
672 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
674 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
676 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
680 emallocz(unsigned int size
) {
681 void *res
= calloc(1, size
);
684 eprint("fatal: could not malloc() %u bytes\n", size
);
689 enternotify(XEvent
*e
) {
691 XCrossingEvent
*ev
= &e
->xcrossing
;
693 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) {
694 if(!isxinerama
|| ev
->window
!= root
)
697 if((c
= getclient(ev
->window
)))
700 selmonitor
= monitorat();
701 fprintf(stderr
, "updating selmonitor %d\n", selmonitor
- monitors
);
707 eprint(const char *errstr
, ...) {
710 va_start(ap
, errstr
);
711 vfprintf(stderr
, errstr
, ap
);
719 XExposeEvent
*ev
= &e
->xexpose
;
721 if(ev
->count
== 0 && (m
= getmonitor(ev
->window
)))
726 floating(Monitor
*m
) { /* default floating layout */
729 domwfact
= dozoom
= False
;
730 for(c
= clients
; c
; c
= c
->next
)
732 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
738 selmonitor
= c
->monitor
;
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
);
752 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
753 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
754 selmonitor
= c
->monitor
;
757 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
762 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
763 XFocusChangeEvent
*ev
= &e
->xfocus
;
765 if(sel
&& ev
->window
!= sel
->win
)
766 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
770 focusnext(const char *arg
) {
775 for(c
= sel
->next
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
777 for(c
= clients
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
785 focusprev(const char *arg
) {
790 for(c
= sel
->prev
; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
792 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
793 for(; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
802 getclient(Window w
) {
805 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
810 getcolor(const char *colstr
) {
811 Colormap cmap
= DefaultColormap(dpy
, screen
);
814 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
815 eprint("error, cannot allocate color '%s'\n", colstr
);
820 getmonitor(Window barwin
) {
823 for(i
= 0; i
< mcount
; i
++)
824 if(monitors
[i
].barwin
== barwin
)
833 unsigned char *p
= NULL
;
834 unsigned long n
, extra
;
837 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
838 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
839 if(status
!= Success
)
848 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
853 if(!text
|| size
== 0)
856 XGetTextProperty(dpy
, w
, &name
, atom
);
859 if(name
.encoding
== XA_STRING
)
860 strncpy(text
, (char *)name
.value
, size
- 1);
862 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
864 strncpy(text
, *list
, size
- 1);
865 XFreeStringList(list
);
868 text
[size
- 1] = '\0';
874 grabbuttons(Client
*c
, Bool focused
) {
875 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
878 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
879 GrabModeAsync
, GrabModeSync
, None
, None
);
880 XGrabButton(dpy
, Button1
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
881 GrabModeAsync
, GrabModeSync
, None
, None
);
882 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
883 GrabModeAsync
, GrabModeSync
, None
, None
);
884 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
885 GrabModeAsync
, GrabModeSync
, None
, None
);
887 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
888 GrabModeAsync
, GrabModeSync
, None
, None
);
889 XGrabButton(dpy
, Button2
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
890 GrabModeAsync
, GrabModeSync
, None
, None
);
891 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
892 GrabModeAsync
, GrabModeSync
, None
, None
);
893 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
894 GrabModeAsync
, GrabModeSync
, None
, None
);
896 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
897 GrabModeAsync
, GrabModeSync
, None
, None
);
898 XGrabButton(dpy
, Button3
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
899 GrabModeAsync
, GrabModeSync
, None
, None
);
900 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
901 GrabModeAsync
, GrabModeSync
, None
, None
);
902 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
903 GrabModeAsync
, GrabModeSync
, None
, None
);
906 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
907 GrabModeAsync
, GrabModeSync
, None
, None
);
914 XModifierKeymap
*modmap
;
916 /* init modifier map */
917 modmap
= XGetModifierMapping(dpy
);
918 for(i
= 0; i
< 8; i
++)
919 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
920 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
921 numlockmask
= (1 << i
);
923 XFreeModifiermap(modmap
);
925 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
926 for(i
= 0; i
< LENGTH(keys
); i
++) {
927 code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
);
928 XGrabKey(dpy
, code
, keys
[i
].mod
, root
, True
,
929 GrabModeAsync
, GrabModeAsync
);
930 XGrabKey(dpy
, code
, keys
[i
].mod
| LockMask
, root
, True
,
931 GrabModeAsync
, GrabModeAsync
);
932 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
, root
, True
,
933 GrabModeAsync
, GrabModeAsync
);
934 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
| LockMask
, root
, True
,
935 GrabModeAsync
, GrabModeAsync
);
940 idxoftag(const char *tag
) {
943 for(i
= 0; (i
< LENGTH(tags
)) && (tags
[i
] != tag
); i
++);
944 return (i
< LENGTH(tags
)) ? i
: 0;
948 initfont(const char *fontstr
) {
949 char *def
, **missing
;
954 XFreeFontSet(dpy
, dc
.font
.set
);
955 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
958 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
959 XFreeStringList(missing
);
962 XFontSetExtents
*font_extents
;
963 XFontStruct
**xfonts
;
965 dc
.font
.ascent
= dc
.font
.descent
= 0;
966 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
967 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
968 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
969 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
970 dc
.font
.ascent
= (*xfonts
)->ascent
;
971 if(dc
.font
.descent
< (*xfonts
)->descent
)
972 dc
.font
.descent
= (*xfonts
)->descent
;
978 XFreeFont(dpy
, dc
.font
.xfont
);
979 dc
.font
.xfont
= NULL
;
980 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
981 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
982 eprint("error, cannot load font: '%s'\n", fontstr
);
983 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
984 dc
.font
.descent
= dc
.font
.xfont
->descent
;
986 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
990 isoccupied(Monitor
*monitor
, unsigned int t
) {
993 for(c
= clients
; c
; c
= c
->next
)
994 if(c
->tags
[t
] && c
->monitor
== monitor
)
1000 isprotodel(Client
*c
) {
1005 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1006 for(i
= 0; !ret
&& i
< n
; i
++)
1007 if(protocols
[i
] == wmatom
[WMDelete
])
1015 isurgent(Monitor
*monitor
, unsigned int t
) {
1018 for(c
= clients
; c
; c
= c
->next
)
1019 if(c
->monitor
== monitor
&& c
->isurgent
&& c
->tags
[t
])
1025 isvisible(Client
*c
, Monitor
*m
) {
1030 for(i
= 0; i
< LENGTH(tags
); i
++)
1031 if(c
->tags
[i
] && c
->monitor
->seltags
[i
])
1037 keypress(XEvent
*e
) {
1043 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1044 for(i
= 0; i
< LENGTH(keys
); i
++)
1045 if(keysym
== keys
[i
].keysym
1046 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
1049 keys
[i
].func(keys
[i
].arg
);
1054 killclient(const char *arg
) {
1059 if(isprotodel(sel
)) {
1060 ev
.type
= ClientMessage
;
1061 ev
.xclient
.window
= sel
->win
;
1062 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1063 ev
.xclient
.format
= 32;
1064 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1065 ev
.xclient
.data
.l
[1] = CurrentTime
;
1066 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
1069 XKillClient(dpy
, sel
->win
);
1073 manage(Window w
, XWindowAttributes
*wa
) {
1074 Client
*c
, *t
= NULL
;
1080 c
= emallocz(sizeof(Client
));
1081 c
->tags
= emallocz(sizeof initags
);
1088 c
->x
= wa
->x
+ m
->sx
;
1089 c
->y
= wa
->y
+ m
->sy
;
1092 c
->oldborder
= wa
->border_width
;
1094 if(c
->w
== m
->sw
&& c
->h
== m
->sh
) {
1097 c
->border
= wa
->border_width
;
1100 if(c
->x
+ c
->w
+ 2 * c
->border
> m
->wax
+ m
->waw
)
1101 c
->x
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1102 if(c
->y
+ c
->h
+ 2 * c
->border
> m
->way
+ m
->wah
)
1103 c
->y
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1108 c
->border
= BORDERPX
;
1110 wc
.border_width
= c
->border
;
1111 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1112 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1113 configure(c
); /* propagates border_width, if size doesn't change */
1115 XSelectInput(dpy
, w
, EnterWindowMask
| FocusChangeMask
| PropertyChangeMask
| StructureNotifyMask
);
1116 grabbuttons(c
, False
);
1118 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
1119 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
1121 memcpy(c
->tags
, t
->tags
, sizeof initags
);
1123 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
1126 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1128 XMapWindow(dpy
, c
->win
);
1129 setclientstate(c
, NormalState
);
1134 mappingnotify(XEvent
*e
) {
1135 XMappingEvent
*ev
= &e
->xmapping
;
1137 XRefreshKeyboardMapping(ev
);
1138 if(ev
->request
== MappingKeyboard
)
1143 maprequest(XEvent
*e
) {
1144 static XWindowAttributes wa
;
1145 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1147 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1149 if(wa
.override_redirect
)
1151 if(!getclient(ev
->window
))
1152 manage(ev
->window
, &wa
);
1161 XQueryPointer(dpy
, root
, &win
, &win
, &x
, &y
, &i
, &i
, &mask
);
1162 for(i
= 0; i
< mcount
; i
++) {
1163 if((x
>= monitors
[i
].sx
&& x
< monitors
[i
].sx
+ monitors
[i
].sw
)
1164 && (y
>= monitors
[i
].sy
&& y
< monitors
[i
].sy
+ monitors
[i
].sh
)) {
1165 return &monitors
[i
];
1172 movemouse(Client
*c
) {
1173 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1182 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1183 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1185 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1187 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1190 XUngrabPointer(dpy
, CurrentTime
);
1192 case ConfigureRequest
:
1195 handler
[ev
.type
](&ev
);
1199 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1200 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1201 if(abs(m
->wax
- nx
) < SNAP
)
1203 else if(abs((m
->wax
+ m
->waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
1204 nx
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1205 if(abs(m
->way
- ny
) < SNAP
)
1207 else if(abs((m
->way
+ m
->wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
1208 ny
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1209 if(!c
->isfloating
&& (m
->layout
->arrange
!= floating
) && (abs(nx
- c
->x
) > SNAP
|| abs(ny
- c
->y
) > SNAP
))
1210 togglefloating(NULL
);
1211 if((m
->layout
->arrange
== floating
) || c
->isfloating
)
1212 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1219 nexttiled(Client
*c
, Monitor
*monitor
) {
1220 for(; c
&& (c
->isfloating
|| !isvisible(c
, monitor
)); c
= c
->next
);
1225 propertynotify(XEvent
*e
) {
1228 XPropertyEvent
*ev
= &e
->xproperty
;
1230 if(ev
->state
== PropertyDelete
)
1231 return; /* ignore */
1232 if((c
= getclient(ev
->window
))) {
1235 case XA_WM_TRANSIENT_FOR
:
1236 XGetTransientForHint(dpy
, c
->win
, &trans
);
1237 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1238 arrange(c
->monitor
);
1240 case XA_WM_NORMAL_HINTS
:
1245 drawbar(c
->monitor
);
1248 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1251 drawbar(c
->monitor
);
1257 quit(const char *arg
) {
1258 readin
= running
= False
;
1262 reapply(const char *arg
) {
1263 static Bool zerotags
[LENGTH(tags
)] = { 0 };
1266 for(c
= clients
; c
; c
= c
->next
) {
1267 memcpy(c
->tags
, zerotags
, sizeof zerotags
);
1274 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1281 /* set minimum possible */
1287 /* temporarily remove base dimensions */
1291 /* adjust for aspect limits */
1292 if (c
->minay
> 0 && c
->maxay
> 0 && c
->minax
> 0 && c
->maxax
> 0) {
1293 if (w
* c
->maxay
> h
* c
->maxax
)
1294 w
= h
* c
->maxax
/ c
->maxay
;
1295 else if (w
* c
->minay
< h
* c
->minax
)
1296 h
= w
* c
->minay
/ c
->minax
;
1299 /* adjust for increment value */
1305 /* restore base dimensions */
1309 if(c
->minw
> 0 && w
< c
->minw
)
1311 if(c
->minh
> 0 && h
< c
->minh
)
1313 if(c
->maxw
> 0 && w
> c
->maxw
)
1315 if(c
->maxh
> 0 && h
> c
->maxh
)
1318 if(w
<= 0 || h
<= 0)
1321 x
= m
->sw
- w
- 2 * c
->border
;
1323 y
= m
->sh
- h
- 2 * c
->border
;
1324 if(x
+ w
+ 2 * c
->border
< m
->sx
)
1326 if(y
+ h
+ 2 * c
->border
< m
->sy
)
1328 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1331 c
->w
= wc
.width
= w
;
1332 c
->h
= wc
.height
= h
;
1333 wc
.border_width
= c
->border
;
1334 XConfigureWindow(dpy
, c
->win
, CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
, &wc
);
1341 resizemouse(Client
*c
) {
1350 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1351 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1353 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1355 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1358 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1359 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1360 XUngrabPointer(dpy
, CurrentTime
);
1361 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1363 case ConfigureRequest
:
1366 handler
[ev
.type
](&ev
);
1370 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
1372 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
1374 if(!c
->isfloating
&& (m
->layout
->arrange
!= floating
) && (abs(nw
- c
->w
) > SNAP
|| abs(nh
- c
->h
) > SNAP
))
1375 togglefloating(NULL
);
1376 if((m
->layout
->arrange
== floating
) || c
->isfloating
)
1377 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1384 restack(Monitor
*m
) {
1392 if(sel
->isfloating
|| (m
->layout
->arrange
== floating
))
1393 XRaiseWindow(dpy
, sel
->win
);
1394 if(m
->layout
->arrange
!= floating
) {
1395 wc
.stack_mode
= Below
;
1396 wc
.sibling
= m
->barwin
;
1397 if(!sel
->isfloating
) {
1398 XConfigureWindow(dpy
, sel
->win
, CWSibling
| CWStackMode
, &wc
);
1399 wc
.sibling
= sel
->win
;
1401 for(c
= nexttiled(clients
, m
); c
; c
= nexttiled(c
->next
, m
)) {
1404 XConfigureWindow(dpy
, c
->win
, CWSibling
| CWStackMode
, &wc
);
1405 wc
.sibling
= c
->win
;
1409 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1415 char buf
[sizeof stext
];
1418 unsigned int len
, offset
;
1421 /* main event loop, also reads status text from stdin */
1423 xfd
= ConnectionNumber(dpy
);
1426 len
= sizeof stext
- 1;
1427 buf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1431 FD_SET(STDIN_FILENO
, &rd
);
1433 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1436 eprint("select failed\n");
1438 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1439 switch((r
= read(STDIN_FILENO
, buf
+ offset
, len
- offset
))) {
1441 strncpy(stext
, strerror(errno
), len
);
1445 strncpy(stext
, "EOF", 4);
1449 for(p
= buf
+ offset
; r
> 0; p
++, r
--, offset
++)
1450 if(*p
== '\n' || *p
== '\0') {
1452 strncpy(stext
, buf
, len
);
1453 p
+= r
- 1; /* p is buf + offset + r - 1 */
1454 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1457 memmove(buf
, p
- r
+ 1, r
);
1462 drawbar(selmonitor
);
1464 while(XPending(dpy
)) {
1465 XNextEvent(dpy
, &ev
);
1466 if(handler
[ev
.type
])
1467 (handler
[ev
.type
])(&ev
); /* call handler */
1474 unsigned int i
, num
;
1475 Window
*wins
, d1
, d2
;
1476 XWindowAttributes wa
;
1479 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1480 for(i
= 0; i
< num
; i
++) {
1481 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1482 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1484 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1485 manage(wins
[i
], &wa
);
1487 for(i
= 0; i
< num
; i
++) { /* now the transients */
1488 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1490 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1491 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1492 manage(wins
[i
], &wa
);
1500 setclientstate(Client
*c
, long state
) {
1501 long data
[] = {state
, None
};
1503 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1504 PropModeReplace
, (unsigned char *)data
, 2);
1508 setlayout(const char *arg
) {
1510 Monitor
*m
= monitorat();
1514 if(m
->layout
== &layouts
[LENGTH(layouts
)])
1515 m
->layout
= &layouts
[0];
1518 for(i
= 0; i
< LENGTH(layouts
); i
++)
1519 if(!strcmp(arg
, layouts
[i
].symbol
))
1521 if(i
== LENGTH(layouts
))
1523 m
->layout
= &layouts
[i
];
1532 setmwfact(const char *arg
) {
1535 Monitor
*m
= monitorat();
1539 /* arg handling, manipulate mwfact */
1542 else if(sscanf(arg
, "%lf", &delta
) == 1) {
1543 if(arg
[0] == '+' || arg
[0] == '-')
1549 else if(m
->mwfact
> 0.9)
1559 XSetWindowAttributes wa
;
1560 XineramaScreenInfo
*info
= NULL
;
1563 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1564 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1565 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1566 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1567 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1568 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1571 wa
.cursor
= cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1572 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1573 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1575 // init screens/monitors first
1576 if((isxinerama
= XineramaIsActive(dpy
)))
1577 info
= XineramaQueryScreens(dpy
, &mcount
);
1578 selmonitor
= monitors
= emallocz(mcount
* sizeof(Monitor
));
1580 screen
= DefaultScreen(dpy
);
1581 root
= RootWindow(dpy
, screen
);
1583 /* init appearance */
1584 dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
);
1585 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
1586 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
1587 dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
);
1588 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
1589 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
1591 dc
.h
= bh
= dc
.font
.height
+ 2;
1592 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1593 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1594 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1596 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1598 for(blw
= i
= 0; i
< LENGTH(layouts
); i
++) {
1599 i
= textw(layouts
[i
].symbol
);
1603 for(i
= 0; i
< mcount
; i
++) {
1608 if (mcount
!= 1 && isxinerama
) {
1609 m
->sx
= info
[i
].x_org
;
1610 m
->sy
= info
[i
].y_org
;
1611 m
->sw
= info
[i
].width
;
1612 m
->sh
= info
[i
].height
;
1613 fprintf(stderr
, "monitor[%d]: %d,%d,%d,%d\n", i
, m
->sx
, m
->sy
, m
->sw
, m
->sh
);
1618 m
->sw
= DisplayWidth(dpy
, screen
);
1619 m
->sh
= DisplayHeight(dpy
, screen
);
1622 m
->seltags
= emallocz(sizeof initags
);
1623 m
->prevtags
= emallocz(sizeof initags
);
1625 memcpy(m
->seltags
, initags
, sizeof initags
);
1626 memcpy(m
->prevtags
, initags
, sizeof initags
);
1630 m
->layout
= &layouts
[0];
1632 // TODO: bpos per screen?
1634 wa
.override_redirect
= 1;
1635 wa
.background_pixmap
= ParentRelative
;
1636 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
1639 m
->barwin
= XCreateWindow(dpy
, root
, m
->sx
, m
->sy
, m
->sw
, bh
, 0,
1640 DefaultDepth(dpy
, screen
), CopyFromParent
, DefaultVisual(dpy
, screen
),
1641 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
1642 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1644 XMapRaised(dpy
, m
->barwin
);
1645 strcpy(stext
, "dwm-"VERSION
);
1647 /* EWMH support per monitor */
1648 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1649 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1651 /* select for events */
1652 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
1653 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
1654 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
1655 XSelectInput(dpy
, root
, wa
.event_mask
);
1668 selmonitor
= monitorat();
1669 fprintf(stderr
, "selmonitor == %d\n", selmonitor
- monitors
);
1673 spawn(const char *arg
) {
1674 static char *shell
= NULL
;
1676 if(!shell
&& !(shell
= getenv("SHELL")))
1680 /* The double-fork construct avoids zombie processes and keeps the code
1681 * clean from stupid signal handlers. */
1685 close(ConnectionNumber(dpy
));
1687 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1688 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1697 tag(const char *arg
) {
1702 for(i
= 0; i
< LENGTH(tags
); i
++)
1703 sel
->tags
[i
] = (NULL
== arg
);
1704 sel
->tags
[idxoftag(arg
)] = True
;
1705 arrange(sel
->monitor
);
1709 textnw(const char *text
, unsigned int len
) {
1713 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1716 return XTextWidth(dc
.font
.xfont
, text
, len
);
1720 textw(const char *text
) {
1721 return textnw(text
, strlen(text
)) + dc
.font
.height
;
1726 unsigned int i
, n
, nx
, ny
, nw
, nh
, mw
, th
;
1729 domwfact
= dozoom
= True
;
1731 nx
= ny
= nw
= 0; /* gcc stupidity requires this */
1733 for(n
= 0, c
= nexttiled(clients
, m
); c
; c
= nexttiled(c
->next
, m
))
1737 mw
= (n
== 1) ? m
->waw
: m
->mwfact
* m
->waw
;
1738 th
= (n
> 1) ? m
->wah
/ (n
- 1) : 0;
1739 if(n
> 1 && th
< bh
)
1742 for(i
= 0, c
= mc
= nexttiled(clients
, m
); c
; c
= nexttiled(c
->next
, m
)) {
1743 if(i
== 0) { /* master */
1746 nw
= mw
- 2 * c
->border
;
1747 nh
= m
->wah
- 2 * c
->border
;
1749 else { /* tile window */
1752 nx
+= mc
->w
+ 2 * mc
->border
;
1753 nw
= m
->waw
- mw
- 2 * c
->border
;
1755 if(i
+ 1 == n
) /* remainder */
1756 nh
= (m
->way
+ m
->wah
) - ny
- 2 * c
->border
;
1758 nh
= th
- 2 * c
->border
;
1760 resize(c
, nx
, ny
, nw
, nh
, RESIZEHINTS
);
1761 if((RESIZEHINTS
) && ((c
->h
< bh
) || (c
->h
> nh
) || (c
->w
< bh
) || (c
->w
> nw
)))
1762 /* client doesn't accept size constraints */
1763 resize(c
, nx
, ny
, nw
, nh
, False
);
1764 if(n
> 1 && th
!= m
->wah
)
1765 ny
= c
->y
+ c
->h
+ 2 * c
->border
;
1771 togglebar(const char *arg
) {
1773 bpos
= (BARPOS
== BarOff
) ? BarTop
: BARPOS
;
1776 updatebarpos(monitorat());
1777 arrange(monitorat());
1781 togglefloating(const char *arg
) {
1784 sel
->isfloating
= !sel
->isfloating
;
1786 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1787 arrange(sel
->monitor
);
1791 toggletag(const char *arg
) {
1797 sel
->tags
[i
] = !sel
->tags
[i
];
1798 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1799 if(j
== LENGTH(tags
))
1800 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1801 arrange(sel
->monitor
);
1805 toggleview(const char *arg
) {
1807 Monitor
*m
= monitorat();
1810 m
->seltags
[i
] = !m
->seltags
[i
];
1811 for(j
= 0; j
< LENGTH(tags
) && !m
->seltags
[j
]; j
++);
1812 if(j
== LENGTH(tags
))
1813 m
->seltags
[i
] = True
; /* at least one tag must be viewed */
1821 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1822 c
->isbanned
= False
;
1826 unmanage(Client
*c
) {
1829 wc
.border_width
= c
->oldborder
;
1830 /* The server grab construct avoids race conditions. */
1832 XSetErrorHandler(xerrordummy
);
1833 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1838 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1839 setclientstate(c
, WithdrawnState
);
1843 XSetErrorHandler(xerror
);
1849 unmapnotify(XEvent
*e
) {
1851 XUnmapEvent
*ev
= &e
->xunmap
;
1853 if((c
= getclient(ev
->window
)))
1858 updatebarpos(Monitor
*m
) {
1869 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
);
1873 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
+ m
->wah
);
1876 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
- bh
);
1880 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1884 updatesizehints(Client
*c
) {
1888 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1890 c
->flags
= size
.flags
;
1891 if(c
->flags
& PBaseSize
) {
1892 c
->basew
= size
.base_width
;
1893 c
->baseh
= size
.base_height
;
1895 else if(c
->flags
& PMinSize
) {
1896 c
->basew
= size
.min_width
;
1897 c
->baseh
= size
.min_height
;
1900 c
->basew
= c
->baseh
= 0;
1901 if(c
->flags
& PResizeInc
) {
1902 c
->incw
= size
.width_inc
;
1903 c
->inch
= size
.height_inc
;
1906 c
->incw
= c
->inch
= 0;
1907 if(c
->flags
& PMaxSize
) {
1908 c
->maxw
= size
.max_width
;
1909 c
->maxh
= size
.max_height
;
1912 c
->maxw
= c
->maxh
= 0;
1913 if(c
->flags
& PMinSize
) {
1914 c
->minw
= size
.min_width
;
1915 c
->minh
= size
.min_height
;
1917 else if(c
->flags
& PBaseSize
) {
1918 c
->minw
= size
.base_width
;
1919 c
->minh
= size
.base_height
;
1922 c
->minw
= c
->minh
= 0;
1923 if(c
->flags
& PAspect
) {
1924 c
->minax
= size
.min_aspect
.x
;
1925 c
->maxax
= size
.max_aspect
.x
;
1926 c
->minay
= size
.min_aspect
.y
;
1927 c
->maxay
= size
.max_aspect
.y
;
1930 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1931 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1932 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1936 updatetitle(Client
*c
) {
1937 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1938 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1942 updatewmhints(Client
*c
) {
1945 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1946 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1951 /* There's no way to check accesses to destroyed windows, thus those cases are
1952 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1953 * default error handler, which may call exit. */
1955 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1956 if(ee
->error_code
== BadWindow
1957 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1958 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1959 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1960 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1961 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1962 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1963 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1965 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1966 ee
->request_code
, ee
->error_code
);
1967 return xerrorxlib(dpy
, ee
); /* may call exit */
1971 xerrordummy(Display
*dsply
, XErrorEvent
*ee
) {
1975 /* Startup Error handler to check if another window manager
1976 * is already running. */
1978 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
1984 view(const char *arg
) {
1986 Bool tmp
[LENGTH(tags
)];
1987 Monitor
*m
= monitorat();
1989 for(i
= 0; i
< LENGTH(tags
); i
++)
1990 tmp
[i
] = (NULL
== arg
);
1991 tmp
[idxoftag(arg
)] = True
;
1992 if(memcmp(m
->seltags
, tmp
, sizeof initags
) != 0) {
1993 memcpy(m
->prevtags
, m
->seltags
, sizeof initags
);
1994 memcpy(m
->seltags
, tmp
, sizeof initags
);
2000 viewprevtag(const char *arg
) {
2001 static Bool tmp
[LENGTH(tags
)];
2003 Monitor
*m
= monitorat();
2005 memcpy(tmp
, m
->seltags
, sizeof initags
);
2006 memcpy(m
->seltags
, m
->prevtags
, sizeof initags
);
2007 memcpy(m
->prevtags
, tmp
, sizeof initags
);
2012 zoom(const char *arg
) {
2015 if(!sel
|| !dozoom
|| sel
->isfloating
)
2017 if(c
== nexttiled(clients
, c
->monitor
))
2018 if(!(c
= nexttiled(c
->next
, c
->monitor
)))
2023 arrange(c
->monitor
);
2027 movetomonitor(const char *arg
) {
2035 for(i
= 0; &monitors
[i
] != sel
->monitor
&& i
< mcount
; i
++);
2038 sel
->monitor
= &monitors
[i
% mcount
];
2040 memcpy(sel
->tags
, sel
->monitor
->seltags
, sizeof initags
);
2041 resize(sel
, sel
->monitor
->wax
, sel
->monitor
->way
, sel
->w
, sel
->h
, True
);
2042 arrange(sel
->monitor
);
2046 selectmonitor(const char *arg
) {
2053 for(i
= 0; &monitors
[i
] != sel
->monitor
&& i
< mcount
; i
++);
2056 m
= &monitors
[i
% mcount
];
2057 XWarpPointer(dpy
, None
, root
, 0, 0, 0, 0, m
->wax
+m
->waw
/2, m
->way
+m
->wah
/2);
2063 main(int argc
, char *argv
[]) {
2064 if(argc
== 2 && !strcmp("-v", argv
[1]))
2065 eprint("dwm-"VERSION
", © 2006-2008 Anselm R. Garbe, Sander van Dijk, "
2066 "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
2068 eprint("usage: dwm [-v]\n");
2070 setlocale(LC_CTYPE
, "");
2071 if(!(dpy
= XOpenDisplay(0)))
2072 eprint("dwm: cannot open display\n");