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);
124 int sx
, sy
, sw
, sh
, wax
, way
, wah
, waw
;
132 /* function declarations */
133 void applyrules(Client
*c
);
135 void attach(Client
*c
);
136 void attachstack(Client
*c
);
138 void buttonpress(XEvent
*e
);
139 void checkotherwm(void);
141 void compileregs(void);
142 void configure(Client
*c
);
143 void configurenotify(XEvent
*e
);
144 void configurerequest(XEvent
*e
);
145 void destroynotify(XEvent
*e
);
146 void detach(Client
*c
);
147 void detachstack(Client
*c
);
149 void drawsquare(Monitor
*, Bool filled
, Bool empty
, unsigned long col
[ColLast
]);
150 void drawtext(Monitor
*, const char *text
, unsigned long col
[ColLast
]);
151 void *emallocz(unsigned int size
);
152 void enternotify(XEvent
*e
);
153 void eprint(const char *errstr
, ...);
154 void expose(XEvent
*e
);
155 void floating(void); /* default floating layout */
156 void focus(Client
*c
);
157 void focusin(XEvent
*e
);
158 void focusnext(const char *arg
);
159 void focusprev(const char *arg
);
160 Client
*getclient(Window w
);
161 unsigned long getcolor(const char *colstr
);
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(Monitor
*, const char *fontstr
);
168 Bool
isoccupied(Monitor
*m
, unsigned int t
);
169 Bool
isprotodel(Client
*c
);
170 Bool
isvisible(Client
*c
, Monitor
*m
);
171 void keypress(XEvent
*e
);
172 void killclient(const char *arg
);
173 void leavenotify(XEvent
*e
);
174 void manage(Window w
, XWindowAttributes
*wa
);
175 void mappingnotify(XEvent
*e
);
176 void maprequest(XEvent
*e
);
177 void maximize(const char *arg
);
178 void movemouse(Client
*c
);
179 Client
*nexttiled(Client
*c
, Monitor
*m
);
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
);
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(Monitor
*, const char *text
, unsigned int len
);
195 unsigned int textw(Monitor
*, const char *text
);
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
*s
);
205 void updatesizehints(Client
*c
);
206 void updatetitle(Client
*c
);
207 void view(const char *arg
);
208 void viewprevtag(const char *arg
); /* views previous selected tags */
209 int xerror(Display
*dpy
, XErrorEvent
*ee
);
210 int xerrordummy(Display
*dsply
, XErrorEvent
*ee
);
211 int xerrorstart(Display
*dsply
, XErrorEvent
*ee
);
212 void zoom(const char *arg
);
213 int monitorat(int, int);
214 void movetomonitor(const char *arg
);
215 void selectmonitor(const char *arg
);
221 //int screen, sx, sy, sw, sh, wax, way, waw, wah;
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 [LeaveNotify
] = leavenotify
,
236 [MappingNotify
] = mappingnotify
,
237 [MapRequest
] = maprequest
,
238 [PropertyNotify
] = propertynotify
,
239 [UnmapNotify
] = unmapnotify
241 Atom wmatom
[WMLast
], netatom
[NetLast
];
242 Bool domwfact
= True
;
244 Bool otherwm
, readin
;
246 //Bool selscreen = True;
247 Client
*clients
= NULL
;
249 Client
*stack
= NULL
;
250 Cursor cursor
[CurLast
];
254 //Layout *layout = NULL;
255 //Window barwin, root;
260 /* configuration, allows nested code to access above variables */
263 //Bool prevtags[LENGTH(tags)];
265 /* function implementations */
267 applyrules(Client
*c
) {
268 static char buf
[512];
271 Bool matched_tag
= False
;
272 Bool matched_monitor
= False
;
273 XClassHint ch
= { 0 };
276 XGetClassHint(dpy
, c
->win
, &ch
);
277 snprintf(buf
, sizeof buf
, "%s:%s:%s",
278 ch
.res_class
? ch
.res_class
: "",
279 ch
.res_name
? ch
.res_name
: "", c
->name
);
280 for(i
= 0; i
< LENGTH(rules
); i
++)
281 if(regs
[i
].propregex
&& !regexec(regs
[i
].propregex
, buf
, 1, &tmp
, 0)) {
282 if (rules
[i
].monitor
>= 0 && rules
[i
].monitor
< mcount
) {
283 matched_monitor
= True
;
284 c
->monitor
= rules
[i
].monitor
;
287 c
->isfloating
= rules
[i
].isfloating
;
288 for(j
= 0; regs
[i
].tagregex
&& j
< LENGTH(tags
); j
++) {
289 if(!regexec(regs
[i
].tagregex
, tags
[j
], 1, &tmp
, 0)) {
300 memcpy(c
->tags
, monitors
[monitorat(-1, -1)].seltags
, sizeof initags
);
301 if (!matched_monitor
)
302 c
->monitor
= monitorat(-1, -1);
309 for(c
= clients
; c
; c
= c
->next
)
310 if(isvisible(c
, &monitors
[c
->monitor
]))
315 monitors
[selmonitor
].layout
->arrange();
329 attachstack(Client
*c
) {
338 XMoveWindow(dpy
, c
->win
, c
->x
+ 3 * monitors
[c
->monitor
].sw
, c
->y
);
343 buttonpress(XEvent
*e
) {
346 XButtonPressedEvent
*ev
= &e
->xbutton
;
348 Monitor s
= monitors
[monitorat(-1, -1)];
350 if(ev
->window
== s
.barwin
) {
352 for(i
= 0; i
< LENGTH(tags
); i
++) {
353 x
+= textw(&s
, tags
[i
]);
355 if(ev
->button
== Button1
) {
356 if(ev
->state
& MODKEY
)
361 else if(ev
->button
== Button3
) {
362 if(ev
->state
& MODKEY
)
370 if((ev
->x
< x
+ blw
) && ev
->button
== Button1
)
373 else if((c
= getclient(ev
->window
))) {
375 if(CLEANMASK(ev
->state
) != MODKEY
)
377 if(ev
->button
== Button1
) {
378 if((s
.layout
->arrange
== floating
) || c
->isfloating
)
381 togglefloating(NULL
);
384 else if(ev
->button
== Button2
) {
385 if((floating
!= s
.layout
->arrange
) && c
->isfloating
)
386 togglefloating(NULL
);
390 else if(ev
->button
== Button3
&& !c
->isfixed
) {
391 if((floating
== s
.layout
->arrange
) || c
->isfloating
)
394 togglefloating(NULL
);
403 XSetErrorHandler(xerrorstart
);
405 /* this causes an error if some other window manager is running */
406 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
409 eprint("dwm: another window manager is already running\n");
411 XSetErrorHandler(NULL
);
412 xerrorxlib
= XSetErrorHandler(xerror
);
424 for(i
= 0; i
< mcount
; i
++) {
425 Monitor
*m
= &monitors
[i
];
427 XFreeFontSet(dpy
, m
->dc
.font
.set
);
429 XFreeFont(dpy
, m
->dc
.font
.xfont
);
430 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
431 XFreePixmap(dpy
, m
->dc
.drawable
);
432 XFreeGC(dpy
, m
->dc
.gc
);
433 XDestroyWindow(dpy
, m
->barwin
);
434 XFreeCursor(dpy
, cursor
[CurNormal
]);
435 XFreeCursor(dpy
, cursor
[CurResize
]);
436 XFreeCursor(dpy
, cursor
[CurMove
]);
437 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
449 regs
= emallocz(LENGTH(rules
) * sizeof(Regs
));
450 for(i
= 0; i
< LENGTH(rules
); i
++) {
452 reg
= emallocz(sizeof(regex_t
));
453 if(regcomp(reg
, rules
[i
].prop
, REG_EXTENDED
))
456 regs
[i
].propregex
= reg
;
459 reg
= emallocz(sizeof(regex_t
));
460 if(regcomp(reg
, rules
[i
].tags
, REG_EXTENDED
))
463 regs
[i
].tagregex
= reg
;
469 configure(Client
*c
) {
472 ce
.type
= ConfigureNotify
;
480 ce
.border_width
= c
->border
;
482 ce
.override_redirect
= False
;
483 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
487 configurenotify(XEvent
*e
) {
488 XConfigureEvent
*ev
= &e
->xconfigure
;
489 Monitor
*m
= &monitors
[selmonitor
];
491 if(ev
->window
== root
&& (ev
->width
!= m
->sw
|| ev
->height
!= m
->sh
)) {
494 XFreePixmap(dpy
, dc
.drawable
);
495 dc
.drawable
= XCreatePixmap(dpy
, root
, m
->sw
, bh
, DefaultDepth(dpy
, screen
));
496 XResizeWindow(dpy
, m
->barwin
, m
->sw
, bh
);
503 configurerequest(XEvent
*e
) {
505 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
508 if((c
= getclient(ev
->window
))) {
509 Monitor
*m
= &monitors
[c
->monitor
];
510 if(ev
->value_mask
& CWBorderWidth
)
511 c
->border
= ev
->border_width
;
512 if(c
->isfixed
|| c
->isfloating
|| (floating
== m
->layout
->arrange
)) {
513 if(ev
->value_mask
& CWX
)
515 if(ev
->value_mask
& CWY
)
517 if(ev
->value_mask
& CWWidth
)
519 if(ev
->value_mask
& CWHeight
)
521 if((c
->x
- m
->sx
+ c
->w
) > m
->sw
&& c
->isfloating
)
522 c
->x
= m
->sx
+ (m
->sw
/ 2 - c
->w
/ 2); /* center in x direction */
523 if((c
->y
- m
->sy
+ c
->h
) > m
->sh
&& c
->isfloating
)
524 c
->y
= m
->sy
+ (m
->sh
/ 2 - c
->h
/ 2); /* center in y direction */
525 if((ev
->value_mask
& (CWX
| CWY
))
526 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
528 if(isvisible(c
, &monitors
[monitorat(-1,-1)]))
529 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
537 wc
.width
= ev
->width
;
538 wc
.height
= ev
->height
;
539 wc
.border_width
= ev
->border_width
;
540 wc
.sibling
= ev
->above
;
541 wc
.stack_mode
= ev
->detail
;
542 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
548 destroynotify(XEvent
*e
) {
550 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
552 if((c
= getclient(ev
->window
)))
559 c
->prev
->next
= c
->next
;
561 c
->next
->prev
= c
->prev
;
564 c
->next
= c
->prev
= NULL
;
568 detachstack(Client
*c
) {
571 for(tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
579 for(s
= 0; s
< mcount
; ++s
) {
580 Monitor
*m
= &monitors
[s
];
582 for(i
= 0; i
< LENGTH(tags
); i
++) {
583 m
->dc
.w
= textw(m
, tags
[i
]);
585 drawtext(m
, tags
[i
], m
->dc
.sel
);
586 drawsquare(m
, sel
&& sel
->tags
[i
] && sel
->monitor
== m
->id
, isoccupied(m
, i
), m
->dc
.sel
);
589 drawtext(m
, tags
[i
], m
->dc
.norm
);
590 drawsquare(m
, sel
&& sel
->tags
[i
] && sel
->monitor
== m
->id
, isoccupied(m
, i
), m
->dc
.norm
);
595 drawtext(m
, m
->layout
->symbol
, m
->dc
.norm
);
596 x
= m
->dc
.x
+ m
->dc
.w
;
597 m
->dc
.w
= textw(m
, stext
);
598 m
->dc
.x
= m
->sw
- m
->dc
.w
;
603 drawtext(m
, stext
, m
->dc
.norm
);
604 if((m
->dc
.w
= m
->dc
.x
- x
) > bh
) {
606 if(sel
&& sel
->monitor
== m
->id
) {
607 drawtext(m
, sel
->name
, m
->dc
.sel
);
608 drawsquare(m
, False
, sel
->isfloating
, m
->dc
.sel
);
611 drawtext(m
, NULL
, m
->dc
.norm
);
613 XCopyArea(dpy
, m
->dc
.drawable
, m
->barwin
, m
->dc
.gc
, 0, 0, m
->sw
, bh
, 0, 0);
619 drawsquare(Monitor
*m
, Bool filled
, Bool empty
, unsigned long col
[ColLast
]) {
622 XRectangle r
= { m
->dc
.x
, m
->dc
.y
, m
->dc
.w
, m
->dc
.h
};
624 gcv
.foreground
= col
[ColFG
];
625 XChangeGC(dpy
, m
->dc
.gc
, GCForeground
, &gcv
);
626 x
= (m
->dc
.font
.ascent
+ m
->dc
.font
.descent
+ 2) / 4;
630 r
.width
= r
.height
= x
+ 1;
631 XFillRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
634 r
.width
= r
.height
= x
;
635 XDrawRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
640 drawtext(Monitor
*m
, const char *text
, unsigned long col
[ColLast
]) {
642 static char buf
[256];
643 unsigned int len
, olen
;
644 XRectangle r
= { m
->dc
.x
, m
->dc
.y
, m
->dc
.w
, m
->dc
.h
};
646 XSetForeground(dpy
, m
->dc
.gc
, col
[ColBG
]);
647 XFillRectangles(dpy
, m
->dc
.drawable
, m
->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
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
657 y
= m
->dc
.y
+ (m
->dc
.h
/ 2) - (h
/ 2) + m
->dc
.font
.ascent
;
658 x
= m
->dc
.x
+ (h
/ 2);
659 /* shorten text if necessary */
660 while(len
&& (w
= textnw(m
, buf
, len
)) > m
->dc
.w
- h
)
671 return; /* too long */
672 XSetForeground(dpy
, m
->dc
.gc
, col
[ColFG
]);
674 XmbDrawString(dpy
, m
->dc
.drawable
, m
->dc
.font
.set
, m
->dc
.gc
, x
, y
, buf
, len
);
676 XDrawString(dpy
, m
->dc
.drawable
, m
->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
)
695 if((c
= getclient(ev
->window
)))
697 else if(ev
->window
== root
) {
704 eprint(const char *errstr
, ...) {
707 va_start(ap
, errstr
);
708 vfprintf(stderr
, errstr
, ap
);
715 XExposeEvent
*ev
= &e
->xexpose
;
718 if(ev
->window
== monitors
[selmonitor
].barwin
)
724 floating(void) { /* default floating layout */
727 domwfact
= dozoom
= False
;
728 for(c
= clients
; c
; c
= c
->next
)
729 if(isvisible(c
, &monitors
[selmonitor
]))
730 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
735 Monitor
*m
= &monitors
[monitorat(-1, -1)];
736 if(!c
|| (c
&& !isvisible(c
, m
)))
737 for(c
= stack
; c
&& !isvisible(c
, m
); c
= c
->snext
);
738 if(sel
&& sel
!= c
) {
739 grabbuttons(sel
, False
);
740 XSetWindowBorder(dpy
, sel
->win
, monitors
[sel
->monitor
].dc
.norm
[ColBorder
]);
745 grabbuttons(c
, True
);
750 XSetWindowBorder(dpy
, c
->win
, monitors
[c
->monitor
].dc
.sel
[ColBorder
]);
751 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
752 selmonitor
= monitorat(c
->x
, c
->y
);
755 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
756 selmonitor
= monitorat(-1, -1);
761 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
762 XFocusChangeEvent
*ev
= &e
->xfocus
;
764 if(sel
&& ev
->window
!= sel
->win
)
765 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
769 focusnext(const char *arg
) {
771 Monitor
*m
= &monitors
[selmonitor
];
775 for(c
= sel
->next
; c
&& !isvisible(c
, m
); c
= c
->next
);
777 for(c
= clients
; c
&& !isvisible(c
, m
); c
= c
->next
);
785 focusprev(const char *arg
) {
787 Monitor
*m
= &monitors
[selmonitor
];
791 for(c
= sel
->prev
; c
&& !isvisible(c
, m
); c
= c
->prev
);
793 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
794 for(; c
&& !isvisible(c
, m
); c
= c
->prev
);
803 getclient(Window w
) {
806 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
811 getcolor(const char *colstr
) {
812 Colormap cmap
= DefaultColormap(dpy
, screen
);
815 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
816 eprint("error, cannot allocate color '%s'\n", colstr
);
824 unsigned char *p
= NULL
;
825 unsigned long n
, extra
;
828 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
829 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
830 if(status
!= Success
)
839 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
844 if(!text
|| size
== 0)
847 XGetTextProperty(dpy
, w
, &name
, atom
);
850 if(name
.encoding
== XA_STRING
)
851 strncpy(text
, (char *)name
.value
, size
- 1);
853 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
855 strncpy(text
, *list
, size
- 1);
856 XFreeStringList(list
);
859 text
[size
- 1] = '\0';
865 grabbuttons(Client
*c
, Bool focused
) {
866 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
869 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
870 GrabModeAsync
, GrabModeSync
, None
, None
);
871 XGrabButton(dpy
, Button1
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
872 GrabModeAsync
, GrabModeSync
, None
, None
);
873 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
874 GrabModeAsync
, GrabModeSync
, None
, None
);
875 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
876 GrabModeAsync
, GrabModeSync
, None
, None
);
878 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
879 GrabModeAsync
, GrabModeSync
, None
, None
);
880 XGrabButton(dpy
, Button2
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
881 GrabModeAsync
, GrabModeSync
, None
, None
);
882 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
883 GrabModeAsync
, GrabModeSync
, None
, None
);
884 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
885 GrabModeAsync
, GrabModeSync
, None
, None
);
887 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
888 GrabModeAsync
, GrabModeSync
, None
, None
);
889 XGrabButton(dpy
, Button3
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
890 GrabModeAsync
, GrabModeSync
, None
, None
);
891 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
892 GrabModeAsync
, GrabModeSync
, None
, None
);
893 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
894 GrabModeAsync
, GrabModeSync
, None
, None
);
897 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
898 GrabModeAsync
, GrabModeSync
, None
, None
);
906 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
907 for(i
= 0; i
< LENGTH(keys
); i
++) {
908 code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
);
909 XGrabKey(dpy
, code
, keys
[i
].mod
, root
, True
,
910 GrabModeAsync
, GrabModeAsync
);
911 XGrabKey(dpy
, code
, keys
[i
].mod
| LockMask
, root
, True
,
912 GrabModeAsync
, GrabModeAsync
);
913 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
, root
, True
,
914 GrabModeAsync
, GrabModeAsync
);
915 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
| LockMask
, root
, True
,
916 GrabModeAsync
, GrabModeAsync
);
921 idxoftag(const char *tag
) {
924 for(i
= 0; (i
< LENGTH(tags
)) && (tags
[i
] != tag
); i
++);
925 return (i
< LENGTH(tags
)) ? i
: 0;
929 initfont(Monitor
*m
, const char *fontstr
) {
930 char *def
, **missing
;
935 XFreeFontSet(dpy
, m
->dc
.font
.set
);
936 m
->dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
939 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
940 XFreeStringList(missing
);
943 XFontSetExtents
*font_extents
;
944 XFontStruct
**xfonts
;
946 m
->dc
.font
.ascent
= m
->dc
.font
.descent
= 0;
947 font_extents
= XExtentsOfFontSet(m
->dc
.font
.set
);
948 n
= XFontsOfFontSet(m
->dc
.font
.set
, &xfonts
, &font_names
);
949 for(i
= 0, m
->dc
.font
.ascent
= 0, m
->dc
.font
.descent
= 0; i
< n
; i
++) {
950 if(m
->dc
.font
.ascent
< (*xfonts
)->ascent
)
951 m
->dc
.font
.ascent
= (*xfonts
)->ascent
;
952 if(m
->dc
.font
.descent
< (*xfonts
)->descent
)
953 m
->dc
.font
.descent
= (*xfonts
)->descent
;
959 XFreeFont(dpy
, m
->dc
.font
.xfont
);
960 m
->dc
.font
.xfont
= NULL
;
961 if(!(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
962 && !(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
963 eprint("error, cannot load font: '%s'\n", fontstr
);
964 m
->dc
.font
.ascent
= m
->dc
.font
.xfont
->ascent
;
965 m
->dc
.font
.descent
= m
->dc
.font
.xfont
->descent
;
967 m
->dc
.font
.height
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
971 isoccupied(Monitor
*m
, unsigned int t
) {
974 for(c
= clients
; c
; c
= c
->next
)
975 if(c
->tags
[t
] && c
->monitor
== m
->id
)
981 isprotodel(Client
*c
) {
986 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
987 for(i
= 0; !ret
&& i
< n
; i
++)
988 if(protocols
[i
] == wmatom
[WMDelete
])
996 isvisible(Client
*c
, Monitor
*m
) {
999 for(i
= 0; i
< LENGTH(tags
); i
++)
1000 if(c
->tags
[i
] && monitors
[c
->monitor
].seltags
[i
] && m
->id
== c
->monitor
)
1006 keypress(XEvent
*e
) {
1012 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1013 for(i
= 0; i
< LENGTH(keys
); i
++)
1014 if(keysym
== keys
[i
].keysym
1015 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
1018 keys
[i
].func(keys
[i
].arg
);
1023 killclient(const char *arg
) {
1028 if(isprotodel(sel
)) {
1029 ev
.type
= ClientMessage
;
1030 ev
.xclient
.window
= sel
->win
;
1031 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1032 ev
.xclient
.format
= 32;
1033 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1034 ev
.xclient
.data
.l
[1] = CurrentTime
;
1035 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
1038 XKillClient(dpy
, sel
->win
);
1042 leavenotify(XEvent
*e
) {
1043 XCrossingEvent
*ev
= &e
->xcrossing
;
1045 if((ev
->window
== root
) && !ev
->same_screen
) {
1052 manage(Window w
, XWindowAttributes
*wa
) {
1053 Client
*c
, *t
= NULL
;
1058 c
= emallocz(sizeof(Client
));
1059 c
->tags
= emallocz(sizeof initags
);
1063 Monitor
*m
= &monitors
[c
->monitor
];
1069 c
->oldborder
= wa
->border_width
;
1071 if (monitorat(c
->x
, c
->y
) != c
->monitor
) {
1076 if(c
->w
== m
->sw
&& c
->h
== m
->sh
) {
1079 c
->border
= wa
->border_width
;
1082 if(c
->x
+ c
->w
+ 2 * c
->border
> m
->wax
+ m
->waw
)
1083 c
->x
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1084 if(c
->y
+ c
->h
+ 2 * c
->border
> m
->way
+ m
->wah
)
1085 c
->y
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1090 c
->border
= BORDERPX
;
1092 wc
.border_width
= c
->border
;
1093 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1094 XSetWindowBorder(dpy
, w
, m
->dc
.norm
[ColBorder
]);
1095 configure(c
); /* propagates border_width, if size doesn't change */
1097 XSelectInput(dpy
, w
, EnterWindowMask
| FocusChangeMask
| PropertyChangeMask
| StructureNotifyMask
);
1098 grabbuttons(c
, False
);
1100 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
1101 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
1103 memcpy(c
->tags
, t
->tags
, sizeof initags
);
1105 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
1108 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1110 XMapWindow(dpy
, c
->win
);
1111 setclientstate(c
, NormalState
);
1116 mappingnotify(XEvent
*e
) {
1117 XMappingEvent
*ev
= &e
->xmapping
;
1119 XRefreshKeyboardMapping(ev
);
1120 if(ev
->request
== MappingKeyboard
)
1125 maprequest(XEvent
*e
) {
1126 static XWindowAttributes wa
;
1127 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1129 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1131 if(wa
.override_redirect
)
1133 if(!getclient(ev
->window
))
1134 manage(ev
->window
, &wa
);
1138 maximize(const char *arg
) {
1140 if(!sel || (!sel->isfloating && layout->arrange != floating))
1142 resize(sel, wax, way, waw - 2 * sel->border, wah - 2 * sel->border, True);
1147 movemouse(Client
*c
) {
1148 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1155 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1156 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1158 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1160 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1163 XUngrabPointer(dpy
, CurrentTime
);
1165 case ConfigureRequest
:
1168 handler
[ev
.type
](&ev
);
1172 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1173 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1174 Monitor
*m
= &monitors
[monitorat(nx
, ny
)];
1175 if(abs(m
->wax
- nx
) < SNAP
)
1177 else if(abs((m
->wax
+ m
->waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
1178 nx
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1179 if(abs(m
->way
- ny
) < SNAP
)
1181 else if(abs((m
->way
+ m
->wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
1182 ny
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1183 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1184 memcpy(c
->tags
, monitors
[monitorat(nx
, ny
)].seltags
, sizeof initags
);
1191 nexttiled(Client
*c
, Monitor
*m
) {
1192 for(; c
&& (c
->isfloating
|| !isvisible(c
, m
)); c
= c
->next
);
1197 propertynotify(XEvent
*e
) {
1200 XPropertyEvent
*ev
= &e
->xproperty
;
1202 if(ev
->state
== PropertyDelete
)
1203 return; /* ignore */
1204 if((c
= getclient(ev
->window
))) {
1207 case XA_WM_TRANSIENT_FOR
:
1208 XGetTransientForHint(dpy
, c
->win
, &trans
);
1209 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1212 case XA_WM_NORMAL_HINTS
:
1216 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1225 quit(const char *arg
) {
1226 readin
= running
= False
;
1230 reapply(const char *arg
) {
1231 static Bool zerotags
[LENGTH(tags
)] = { 0 };
1234 for(c
= clients
; c
; c
= c
->next
) {
1235 memcpy(c
->tags
, zerotags
, sizeof zerotags
);
1242 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1244 Monitor scr
= monitors
[monitorat(x
, y
)];
1245 c
->monitor
= scr
.id
;
1248 /* set minimum possible */
1254 /* temporarily remove base dimensions */
1258 /* adjust for aspect limits */
1259 if (c
->minay
> 0 && c
->maxay
> 0 && c
->minax
> 0 && c
->maxax
> 0) {
1260 if (w
* c
->maxay
> h
* c
->maxax
)
1261 w
= h
* c
->maxax
/ c
->maxay
;
1262 else if (w
* c
->minay
< h
* c
->minax
)
1263 h
= w
* c
->minay
/ c
->minax
;
1266 /* adjust for increment value */
1272 /* restore base dimensions */
1276 if(c
->minw
> 0 && w
< c
->minw
)
1278 if(c
->minh
> 0 && h
< c
->minh
)
1280 if(c
->maxw
> 0 && w
> c
->maxw
)
1282 if(c
->maxh
> 0 && h
> c
->maxh
)
1285 if(w
<= 0 || h
<= 0)
1287 /* TODO: offscreen appearance fixes */
1290 x = scr.sw - w - 2 * c->border;
1292 y = scr.sh - h - 2 * c->border;
1293 if(x + w + 2 * c->border < scr.sx)
1295 if(y + h + 2 * c->border < scr.sy)
1298 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1301 c
->w
= wc
.width
= w
;
1302 c
->h
= wc
.height
= h
;
1303 wc
.border_width
= c
->border
;
1304 XConfigureWindow(dpy
, c
->win
, CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
, &wc
);
1311 resizemouse(Client
*c
) {
1318 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1319 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1321 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1323 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1326 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1327 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1328 XUngrabPointer(dpy
, CurrentTime
);
1329 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1331 case ConfigureRequest
:
1334 handler
[ev
.type
](&ev
);
1338 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
1340 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
1342 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1358 if(sel
->isfloating
|| (monitors
[selmonitor
].layout
->arrange
== floating
))
1359 XRaiseWindow(dpy
, sel
->win
);
1360 if(monitors
[selmonitor
].layout
->arrange
!= floating
) {
1361 wc
.stack_mode
= Below
;
1362 wc
.sibling
= monitors
[selmonitor
].barwin
;
1363 if(!sel
->isfloating
) {
1364 XConfigureWindow(dpy
, sel
->win
, CWSibling
| CWStackMode
, &wc
);
1365 wc
.sibling
= sel
->win
;
1367 for(s
= 0; s
< mcount
; s
++) {
1368 for(c
= nexttiled(clients
, &monitors
[s
]); c
; c
= nexttiled(c
->next
, &monitors
[s
])) {
1371 XConfigureWindow(dpy
, c
->win
, CWSibling
| CWStackMode
, &wc
);
1372 wc
.sibling
= c
->win
;
1377 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1383 char buf
[sizeof stext
];
1386 unsigned int len
, offset
;
1389 /* main event loop, also reads status text from stdin */
1391 xfd
= ConnectionNumber(dpy
);
1394 len
= sizeof stext
- 1;
1395 buf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1399 FD_SET(STDIN_FILENO
, &rd
);
1401 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1404 eprint("select failed\n");
1406 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1407 switch((r
= read(STDIN_FILENO
, buf
+ offset
, len
- offset
))) {
1409 strncpy(stext
, strerror(errno
), len
);
1413 strncpy(stext
, "EOF", 4);
1417 for(p
= buf
+ offset
; r
> 0; p
++, r
--, offset
++)
1418 if(*p
== '\n' || *p
== '\0') {
1420 strncpy(stext
, buf
, len
);
1421 p
+= r
- 1; /* p is buf + offset + r - 1 */
1422 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1425 memmove(buf
, p
- r
+ 1, r
);
1432 while(XPending(dpy
)) {
1433 XNextEvent(dpy
, &ev
);
1434 if(handler
[ev
.type
])
1435 (handler
[ev
.type
])(&ev
); /* call handler */
1442 unsigned int i
, num
;
1443 Window
*wins
, d1
, d2
;
1444 XWindowAttributes wa
;
1447 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1448 for(i
= 0; i
< num
; i
++) {
1449 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1450 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1452 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1453 manage(wins
[i
], &wa
);
1455 for(i
= 0; i
< num
; i
++) { /* now the transients */
1456 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1458 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1459 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1460 manage(wins
[i
], &wa
);
1468 setclientstate(Client
*c
, long state
) {
1469 long data
[] = {state
, None
};
1471 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1472 PropModeReplace
, (unsigned char *)data
, 2);
1476 setlayout(const char *arg
) {
1478 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1482 if(m
->layout
== &layouts
[LENGTH(layouts
)])
1483 m
->layout
= &layouts
[0];
1486 for(i
= 0; i
< LENGTH(layouts
); i
++)
1487 if(!strcmp(arg
, layouts
[i
].symbol
))
1489 if(i
== LENGTH(layouts
))
1491 m
->layout
= &layouts
[i
];
1500 setmwfact(const char *arg
) {
1503 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1507 /* arg handling, manipulate mwfact */
1510 else if(sscanf(arg
, "%lf", &delta
) == 1) {
1511 if(arg
[0] == '+' || arg
[0] == '-')
1517 else if(m
->mwfact
> 0.9)
1525 unsigned int i
, j
, k
;
1526 XModifierKeymap
*modmap
;
1527 XSetWindowAttributes wa
;
1530 XineramaScreenInfo
*info
= NULL
;
1533 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1534 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1535 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1536 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1537 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1538 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1539 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1540 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1543 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1544 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1545 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1548 /* init modifier map */
1549 modmap
= XGetModifierMapping(dpy
);
1550 for(i
= 0; i
< 8; i
++)
1551 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
1552 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1553 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1554 numlockmask
= (1 << i
);
1556 XFreeModifiermap(modmap
);
1558 /* select for events */
1559 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
1560 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
1561 wa
.cursor
= cursor
[CurNormal
];
1562 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
1563 XSelectInput(dpy
, root
, wa
.event_mask
);
1571 if (XineramaIsActive(dpy
)) {
1572 info
= XineramaQueryScreens(dpy
, &s
);
1575 monitors
= emallocz(s
*sizeof(Monitor
));
1578 for(i
= 0; i
< s
; i
++) {
1581 monitors
[i
].sx
= info
[i
].x_org
;
1582 monitors
[i
].sy
= info
[i
].y_org
;
1583 monitors
[i
].sw
= info
[i
].width
;
1584 monitors
[i
].sh
= info
[i
].height
;
1589 monitors
[i
].sw
= DisplayWidth(dpy
, screen
);
1590 monitors
[i
].sh
= DisplayHeight(dpy
, screen
);
1594 monitors
[i
].seltags
= emallocz(sizeof initags
);
1595 monitors
[i
].prevtags
= emallocz(sizeof initags
);
1597 memcpy(monitors
[i
].seltags
, initags
, sizeof initags
);
1598 memcpy(monitors
[i
].prevtags
, initags
, sizeof initags
);
1600 /* init appearance */
1601 monitors
[i
].dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
);
1602 monitors
[i
].dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
1603 monitors
[i
].dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
1604 monitors
[i
].dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
);
1605 monitors
[i
].dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
1606 monitors
[i
].dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
1607 initfont(&(monitors
[i
]), FONT
);
1608 monitors
[i
].dc
.h
= bh
= monitors
[i
].dc
.font
.height
+ 2;
1611 monitors
[i
].mwfact
= MWFACT
;
1612 monitors
[i
].layout
= &layouts
[0];
1613 for(blw
= k
= 0; k
< LENGTH(layouts
); k
++) {
1614 j
= textw(&monitors
[i
], layouts
[k
].symbol
);
1620 wa
.override_redirect
= 1;
1621 wa
.background_pixmap
= ParentRelative
;
1622 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
1625 monitors
[i
].barwin
= XCreateWindow(dpy
, root
, monitors
[i
].sx
, monitors
[i
].sy
, monitors
[i
].sw
, bh
, 0,
1626 DefaultDepth(dpy
, screen
), CopyFromParent
, DefaultVisual(dpy
, screen
),
1627 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
1628 XDefineCursor(dpy
, monitors
[i
].barwin
, cursor
[CurNormal
]);
1629 updatebarpos(&monitors
[i
]);
1630 XMapRaised(dpy
, monitors
[i
].barwin
);
1631 strcpy(stext
, "dwm-"VERSION
);
1632 monitors
[i
].dc
.drawable
= XCreatePixmap(dpy
, root
, monitors
[i
].sw
, bh
, DefaultDepth(dpy
, screen
));
1633 g
= XCreateGC(dpy
, root
, 0, 0);
1634 monitors
[i
].dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1635 XSetLineAttributes(dpy
, monitors
[i
].dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1636 if(!monitors
[i
].dc
.font
.set
)
1637 XSetFont(dpy
, monitors
[i
].dc
.gc
, monitors
[i
].dc
.font
.xfont
->fid
);
1642 spawn(const char *arg
) {
1643 static char *shell
= NULL
;
1645 if(!shell
&& !(shell
= getenv("SHELL")))
1649 /* The double-fork construct avoids zombie processes and keeps the code
1650 * clean from stupid signal handlers. */
1654 close(ConnectionNumber(dpy
));
1656 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1657 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1666 tag(const char *arg
) {
1671 for(i
= 0; i
< LENGTH(tags
); i
++)
1672 sel
->tags
[i
] = (NULL
== arg
);
1673 sel
->tags
[idxoftag(arg
)] = True
;
1678 textnw(Monitor
*m
, const char *text
, unsigned int len
) {
1681 if(m
->dc
.font
.set
) {
1682 XmbTextExtents(m
->dc
.font
.set
, text
, len
, NULL
, &r
);
1685 return XTextWidth(m
->dc
.font
.xfont
, text
, len
);
1689 textw(Monitor
*m
, const char *text
) {
1690 return textnw(m
, text
, strlen(text
)) + m
->dc
.font
.height
;
1695 unsigned int i
, n
, nx
, ny
, nw
, nh
, mw
, th
;
1699 domwfact
= dozoom
= True
;
1701 nw
= 0; /* gcc stupidity requires this */
1703 for (s
= 0; s
< mcount
; s
++) {
1704 Monitor
*m
= &monitors
[s
];
1706 for(n
= 0, c
= nexttiled(clients
, m
); c
; c
= nexttiled(c
->next
, m
))
1709 for(i
= 0, c
= mc
= nexttiled(clients
, m
); c
; c
= nexttiled(c
->next
, m
)) {
1711 mw
= (n
== 1) ? m
->waw
: m
->mwfact
* m
->waw
;
1712 th
= (n
> 1) ? m
->wah
/ (n
- 1) : 0;
1713 if(n
> 1 && th
< bh
)
1715 if(i
== 0) { /* master */
1718 nw
= mw
- 2 * c
->border
;
1719 nh
= m
->wah
- 2 * c
->border
;
1721 else { /* tile window */
1724 nx
+= mc
->w
+ 2 * mc
->border
;
1725 nw
= m
->waw
- mw
- 2 * c
->border
;
1727 if(i
+ 1 == n
) /* remainder */
1728 nh
= (m
->way
+ m
->wah
) - ny
- 2 * c
->border
;
1730 nh
= th
- 2 * c
->border
;
1732 resize(c
, nx
, ny
, nw
, nh
, RESIZEHINTS
);
1733 if((RESIZEHINTS
) && ((c
->h
< bh
) || (c
->h
> nh
) || (c
->w
< bh
) || (c
->w
> nw
)))
1734 /* client doesn't accept size constraints */
1735 resize(c
, nx
, ny
, nw
, nh
, False
);
1736 if(n
> 1 && th
!= m
->wah
)
1737 ny
= c
->y
+ c
->h
+ 2 * c
->border
;
1744 togglebar(const char *arg
) {
1746 bpos
= (BARPOS
== BarOff
) ? BarTop
: BARPOS
;
1749 updatebarpos(&monitors
[monitorat(-1,-1)]);
1754 togglefloating(const char *arg
) {
1757 sel
->isfloating
= !sel
->isfloating
;
1759 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1764 toggletag(const char *arg
) {
1770 sel
->tags
[i
] = !sel
->tags
[i
];
1771 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1772 if(j
== LENGTH(tags
))
1773 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1778 toggleview(const char *arg
) {
1781 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1784 m
->seltags
[i
] = !m
->seltags
[i
];
1785 for(j
= 0; j
< LENGTH(tags
) && !m
->seltags
[j
]; j
++);
1786 if(j
== LENGTH(tags
))
1787 m
->seltags
[i
] = True
; /* at least one tag must be viewed */
1795 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1796 c
->isbanned
= False
;
1800 unmanage(Client
*c
) {
1803 wc
.border_width
= c
->oldborder
;
1804 /* The server grab construct avoids race conditions. */
1806 XSetErrorHandler(xerrordummy
);
1807 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1812 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1813 setclientstate(c
, WithdrawnState
);
1817 XSetErrorHandler(xerror
);
1823 unmapnotify(XEvent
*e
) {
1825 XUnmapEvent
*ev
= &e
->xunmap
;
1827 if((c
= getclient(ev
->window
)))
1832 updatebarpos(Monitor
*s
) {
1843 XMoveWindow(dpy
, s
->barwin
, s
->sx
, s
->sy
);
1847 XMoveWindow(dpy
, s
->barwin
, s
->sx
, s
->sy
+ s
->wah
);
1850 XMoveWindow(dpy
, s
->barwin
, s
->sx
, s
->sy
- bh
);
1854 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1858 updatesizehints(Client
*c
) {
1862 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1864 c
->flags
= size
.flags
;
1865 if(c
->flags
& PBaseSize
) {
1866 c
->basew
= size
.base_width
;
1867 c
->baseh
= size
.base_height
;
1869 else if(c
->flags
& PMinSize
) {
1870 c
->basew
= size
.min_width
;
1871 c
->baseh
= size
.min_height
;
1874 c
->basew
= c
->baseh
= 0;
1875 if(c
->flags
& PResizeInc
) {
1876 c
->incw
= size
.width_inc
;
1877 c
->inch
= size
.height_inc
;
1880 c
->incw
= c
->inch
= 0;
1881 if(c
->flags
& PMaxSize
) {
1882 c
->maxw
= size
.max_width
;
1883 c
->maxh
= size
.max_height
;
1886 c
->maxw
= c
->maxh
= 0;
1887 if(c
->flags
& PMinSize
) {
1888 c
->minw
= size
.min_width
;
1889 c
->minh
= size
.min_height
;
1891 else if(c
->flags
& PBaseSize
) {
1892 c
->minw
= size
.base_width
;
1893 c
->minh
= size
.base_height
;
1896 c
->minw
= c
->minh
= 0;
1897 if(c
->flags
& PAspect
) {
1898 c
->minax
= size
.min_aspect
.x
;
1899 c
->maxax
= size
.max_aspect
.x
;
1900 c
->minay
= size
.min_aspect
.y
;
1901 c
->maxay
= size
.max_aspect
.y
;
1904 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1905 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1906 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1910 updatetitle(Client
*c
) {
1911 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1912 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1915 /* There's no way to check accesses to destroyed windows, thus those cases are
1916 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1917 * default error handler, which may call exit. */
1919 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1920 if(ee
->error_code
== BadWindow
1921 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1922 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1923 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1924 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1925 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1926 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1927 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1929 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1930 ee
->request_code
, ee
->error_code
);
1931 return xerrorxlib(dpy
, ee
); /* may call exit */
1935 xerrordummy(Display
*dsply
, XErrorEvent
*ee
) {
1939 /* Startup Error handler to check if another window manager
1940 * is already running. */
1942 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
1948 view(const char *arg
) {
1951 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1953 memcpy(m
->prevtags
, m
->seltags
, sizeof initags
);
1954 for(i
= 0; i
< LENGTH(tags
); i
++)
1955 m
->seltags
[i
] = (NULL
== arg
);
1956 m
->seltags
[idxoftag(arg
)] = True
;
1961 viewprevtag(const char *arg
) {
1962 static Bool tmp
[LENGTH(tags
)];
1964 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1966 memcpy(tmp
, m
->seltags
, sizeof initags
);
1967 memcpy(m
->seltags
, m
->prevtags
, sizeof initags
);
1968 memcpy(m
->prevtags
, tmp
, sizeof initags
);
1973 zoom(const char *arg
) {
1976 if(!sel
|| !dozoom
|| sel
->isfloating
)
1978 if((c
= sel
) == nexttiled(clients
, &monitors
[c
->monitor
]))
1979 if(!(c
= nexttiled(c
->next
, &monitors
[c
->monitor
])))
1988 monitorat(int x
, int y
) {
1991 if(!XineramaIsActive(dpy
))
1994 if (x
< 0 || y
< 0) {
1997 XQueryPointer(dpy
, root
, &win
, &win
, &x
, &y
, &i
, &i
, &mask
);
2000 for(i
= 0; i
< mcount
; i
++)
2001 if((x
< 0 || (x
>= monitors
[i
].sx
&& x
< monitors
[i
].sx
+ monitors
[i
].sw
))
2002 && (y
< 0 || (y
>= monitors
[i
].sy
&& y
< monitors
[i
].sy
+ monitors
[i
].sh
)))
2010 movetomonitor(const char *arg
) {
2012 sel
->monitor
= arg
? atoi(arg
) : (sel
->monitor
+1) % mcount
;
2014 memcpy(sel
->tags
, monitors
[sel
->monitor
].seltags
, sizeof initags
);
2015 resize(sel
, monitors
[sel
->monitor
].wax
, monitors
[sel
->monitor
].way
, sel
->w
, sel
->h
, True
);
2021 selectmonitor(const char *arg
) {
2022 Monitor
*m
= &monitors
[arg
? atoi(arg
) : (monitorat(-1, -1)+1) % mcount
];
2024 XWarpPointer(dpy
, None
, root
, 0, 0, 0, 0, m
->wax
+m
->waw
/2, m
->way
+m
->wah
/2);
2030 main(int argc
, char *argv
[]) {
2031 if(argc
== 2 && !strcmp("-v", argv
[1]))
2032 eprint("dwm-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
2033 "Jukka Salmi, Premysl Hruby, Szabolcs Nagy\n");
2035 eprint("usage: dwm [-v]\n");
2037 setlocale(LC_CTYPE
, "");
2038 if(!(dpy
= XOpenDisplay(0)))
2039 eprint("dwm: cannot open display\n");
2040 screen
= DefaultScreen(dpy
);
2041 root
= RootWindow(dpy
, screen
);