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 movemouse(Client
*c
);
178 Client
*nexttiled(Client
*c
, Monitor
*m
);
179 void propertynotify(XEvent
*e
);
180 void quit(const char *arg
);
181 void reapply(const char *arg
);
182 void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
);
183 void resizemouse(Client
*c
);
187 void setclientstate(Client
*c
, long state
);
188 void setlayout(const char *arg
);
189 void setmwfact(const char *arg
);
191 void spawn(const char *arg
);
192 void tag(const char *arg
);
193 unsigned int textnw(Monitor
*, const char *text
, unsigned int len
);
194 unsigned int textw(Monitor
*, const char *text
);
196 void togglebar(const char *arg
);
197 void togglefloating(const char *arg
);
198 void toggletag(const char *arg
);
199 void toggleview(const char *arg
);
200 void unban(Client
*c
);
201 void unmanage(Client
*c
);
202 void unmapnotify(XEvent
*e
);
203 void updatebarpos(Monitor
*s
);
204 void updatesizehints(Client
*c
);
205 void updatetitle(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
);
212 int monitorat(int, int);
213 void movetomonitor(const char *arg
);
214 void selectmonitor(const char *arg
);
220 //int screen, sx, sy, sw, sh, wax, way, waw, wah;
221 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
222 unsigned int bh
, bpos
;
223 unsigned int blw
= 0;
224 unsigned int numlockmask
= 0;
225 void (*handler
[LASTEvent
]) (XEvent
*) = {
226 [ButtonPress
] = buttonpress
,
227 [ConfigureRequest
] = configurerequest
,
228 [ConfigureNotify
] = configurenotify
,
229 [DestroyNotify
] = destroynotify
,
230 [EnterNotify
] = enternotify
,
233 [KeyPress
] = keypress
,
234 [LeaveNotify
] = leavenotify
,
235 [MappingNotify
] = mappingnotify
,
236 [MapRequest
] = maprequest
,
237 [PropertyNotify
] = propertynotify
,
238 [UnmapNotify
] = unmapnotify
240 Atom wmatom
[WMLast
], netatom
[NetLast
];
241 Bool domwfact
= True
;
243 Bool otherwm
, readin
;
245 //Bool selscreen = True;
246 Client
*clients
= NULL
;
248 Client
*stack
= NULL
;
249 Cursor cursor
[CurLast
];
253 //Layout *layout = NULL;
254 //Window barwin, root;
259 /* configuration, allows nested code to access above variables */
262 //Bool prevtags[LENGTH(tags)];
264 /* function implementations */
266 applyrules(Client
*c
) {
267 static char buf
[512];
270 Bool matched_tag
= False
;
271 Bool matched_monitor
= False
;
272 XClassHint ch
= { 0 };
275 XGetClassHint(dpy
, c
->win
, &ch
);
276 snprintf(buf
, sizeof buf
, "%s:%s:%s",
277 ch
.res_class
? ch
.res_class
: "",
278 ch
.res_name
? ch
.res_name
: "", c
->name
);
279 for(i
= 0; i
< LENGTH(rules
); i
++)
280 if(regs
[i
].propregex
&& !regexec(regs
[i
].propregex
, buf
, 1, &tmp
, 0)) {
281 if (rules
[i
].monitor
>= 0 && rules
[i
].monitor
< mcount
) {
282 matched_monitor
= True
;
283 c
->monitor
= rules
[i
].monitor
;
286 c
->isfloating
= rules
[i
].isfloating
;
287 for(j
= 0; regs
[i
].tagregex
&& j
< LENGTH(tags
); j
++) {
288 if(!regexec(regs
[i
].tagregex
, tags
[j
], 1, &tmp
, 0)) {
299 memcpy(c
->tags
, monitors
[monitorat(-1, -1)].seltags
, sizeof initags
);
300 if (!matched_monitor
)
301 c
->monitor
= monitorat(-1, -1);
308 for(c
= clients
; c
; c
= c
->next
)
309 if(isvisible(c
, &monitors
[c
->monitor
]))
314 monitors
[selmonitor
].layout
->arrange();
328 attachstack(Client
*c
) {
337 XMoveWindow(dpy
, c
->win
, c
->x
+ 3 * monitors
[c
->monitor
].sw
, c
->y
);
342 buttonpress(XEvent
*e
) {
345 XButtonPressedEvent
*ev
= &e
->xbutton
;
347 Monitor s
= monitors
[monitorat(-1, -1)];
349 if(ev
->window
== s
.barwin
) {
351 for(i
= 0; i
< LENGTH(tags
); i
++) {
352 x
+= textw(&s
, tags
[i
]);
354 if(ev
->button
== Button1
) {
355 if(ev
->state
& MODKEY
)
360 else if(ev
->button
== Button3
) {
361 if(ev
->state
& MODKEY
)
369 if((ev
->x
< x
+ blw
) && ev
->button
== Button1
)
372 else if((c
= getclient(ev
->window
))) {
374 if(CLEANMASK(ev
->state
) != MODKEY
)
376 if(ev
->button
== Button1
) {
377 if((s
.layout
->arrange
== floating
) || c
->isfloating
)
380 togglefloating(NULL
);
383 else if(ev
->button
== Button2
) {
384 if((floating
!= s
.layout
->arrange
) && c
->isfloating
)
385 togglefloating(NULL
);
389 else if(ev
->button
== Button3
&& !c
->isfixed
) {
390 if((floating
== s
.layout
->arrange
) || c
->isfloating
)
393 togglefloating(NULL
);
402 XSetErrorHandler(xerrorstart
);
404 /* this causes an error if some other window manager is running */
405 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
408 eprint("dwm: another window manager is already running\n");
410 XSetErrorHandler(NULL
);
411 xerrorxlib
= XSetErrorHandler(xerror
);
423 for(i
= 0; i
< mcount
; i
++) {
424 Monitor
*m
= &monitors
[i
];
426 XFreeFontSet(dpy
, m
->dc
.font
.set
);
428 XFreeFont(dpy
, m
->dc
.font
.xfont
);
429 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
430 XFreePixmap(dpy
, m
->dc
.drawable
);
431 XFreeGC(dpy
, m
->dc
.gc
);
432 XDestroyWindow(dpy
, m
->barwin
);
433 XFreeCursor(dpy
, cursor
[CurNormal
]);
434 XFreeCursor(dpy
, cursor
[CurResize
]);
435 XFreeCursor(dpy
, cursor
[CurMove
]);
436 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
448 regs
= emallocz(LENGTH(rules
) * sizeof(Regs
));
449 for(i
= 0; i
< LENGTH(rules
); i
++) {
451 reg
= emallocz(sizeof(regex_t
));
452 if(regcomp(reg
, rules
[i
].prop
, REG_EXTENDED
))
455 regs
[i
].propregex
= reg
;
458 reg
= emallocz(sizeof(regex_t
));
459 if(regcomp(reg
, rules
[i
].tags
, REG_EXTENDED
))
462 regs
[i
].tagregex
= reg
;
468 configure(Client
*c
) {
471 ce
.type
= ConfigureNotify
;
479 ce
.border_width
= c
->border
;
481 ce
.override_redirect
= False
;
482 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
486 configurenotify(XEvent
*e
) {
487 XConfigureEvent
*ev
= &e
->xconfigure
;
488 Monitor
*m
= &monitors
[selmonitor
];
490 if(ev
->window
== root
&& (ev
->width
!= m
->sw
|| ev
->height
!= m
->sh
)) {
493 XFreePixmap(dpy
, dc
.drawable
);
494 dc
.drawable
= XCreatePixmap(dpy
, root
, m
->sw
, bh
, DefaultDepth(dpy
, screen
));
495 XResizeWindow(dpy
, m
->barwin
, m
->sw
, bh
);
502 configurerequest(XEvent
*e
) {
504 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
507 if((c
= getclient(ev
->window
))) {
508 Monitor
*m
= &monitors
[c
->monitor
];
509 if(ev
->value_mask
& CWBorderWidth
)
510 c
->border
= ev
->border_width
;
511 if(c
->isfixed
|| c
->isfloating
|| (floating
== m
->layout
->arrange
)) {
512 if(ev
->value_mask
& CWX
)
514 if(ev
->value_mask
& CWY
)
516 if(ev
->value_mask
& CWWidth
)
518 if(ev
->value_mask
& CWHeight
)
520 if((c
->x
- m
->sx
+ c
->w
) > m
->sw
&& c
->isfloating
)
521 c
->x
= m
->sx
+ (m
->sw
/ 2 - c
->w
/ 2); /* center in x direction */
522 if((c
->y
- m
->sy
+ c
->h
) > m
->sh
&& c
->isfloating
)
523 c
->y
= m
->sy
+ (m
->sh
/ 2 - c
->h
/ 2); /* center in y direction */
524 if((ev
->value_mask
& (CWX
| CWY
))
525 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
527 if(isvisible(c
, &monitors
[monitorat(-1,-1)]))
528 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
536 wc
.width
= ev
->width
;
537 wc
.height
= ev
->height
;
538 wc
.border_width
= ev
->border_width
;
539 wc
.sibling
= ev
->above
;
540 wc
.stack_mode
= ev
->detail
;
541 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
547 destroynotify(XEvent
*e
) {
549 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
551 if((c
= getclient(ev
->window
)))
558 c
->prev
->next
= c
->next
;
560 c
->next
->prev
= c
->prev
;
563 c
->next
= c
->prev
= NULL
;
567 detachstack(Client
*c
) {
570 for(tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
578 for(s
= 0; s
< mcount
; ++s
) {
579 Monitor
*m
= &monitors
[s
];
581 for(i
= 0; i
< LENGTH(tags
); i
++) {
582 m
->dc
.w
= textw(m
, tags
[i
]);
584 drawtext(m
, tags
[i
], m
->dc
.sel
);
585 drawsquare(m
, sel
&& sel
->tags
[i
] && sel
->monitor
== m
->id
, isoccupied(m
, i
), m
->dc
.sel
);
588 drawtext(m
, tags
[i
], m
->dc
.norm
);
589 drawsquare(m
, sel
&& sel
->tags
[i
] && sel
->monitor
== m
->id
, isoccupied(m
, i
), m
->dc
.norm
);
594 drawtext(m
, m
->layout
->symbol
, m
->dc
.norm
);
595 x
= m
->dc
.x
+ m
->dc
.w
;
596 m
->dc
.w
= textw(m
, stext
);
597 m
->dc
.x
= m
->sw
- m
->dc
.w
;
602 drawtext(m
, stext
, m
->dc
.norm
);
603 if((m
->dc
.w
= m
->dc
.x
- x
) > bh
) {
605 if(sel
&& sel
->monitor
== m
->id
) {
606 drawtext(m
, sel
->name
, m
->dc
.sel
);
607 drawsquare(m
, False
, sel
->isfloating
, m
->dc
.sel
);
610 drawtext(m
, NULL
, m
->dc
.norm
);
612 XCopyArea(dpy
, m
->dc
.drawable
, m
->barwin
, m
->dc
.gc
, 0, 0, m
->sw
, bh
, 0, 0);
618 drawsquare(Monitor
*m
, Bool filled
, Bool empty
, unsigned long col
[ColLast
]) {
621 XRectangle r
= { m
->dc
.x
, m
->dc
.y
, m
->dc
.w
, m
->dc
.h
};
623 gcv
.foreground
= col
[ColFG
];
624 XChangeGC(dpy
, m
->dc
.gc
, GCForeground
, &gcv
);
625 x
= (m
->dc
.font
.ascent
+ m
->dc
.font
.descent
+ 2) / 4;
629 r
.width
= r
.height
= x
+ 1;
630 XFillRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
633 r
.width
= r
.height
= x
;
634 XDrawRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
639 drawtext(Monitor
*m
, const char *text
, unsigned long col
[ColLast
]) {
641 static char buf
[256];
642 unsigned int len
, olen
;
643 XRectangle r
= { m
->dc
.x
, m
->dc
.y
, m
->dc
.w
, m
->dc
.h
};
645 XSetForeground(dpy
, m
->dc
.gc
, col
[ColBG
]);
646 XFillRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
650 olen
= len
= strlen(text
);
651 if(len
>= sizeof buf
)
652 len
= sizeof buf
- 1;
653 memcpy(buf
, text
, len
);
655 h
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
656 y
= m
->dc
.y
+ (m
->dc
.h
/ 2) - (h
/ 2) + m
->dc
.font
.ascent
;
657 x
= m
->dc
.x
+ (h
/ 2);
658 /* shorten text if necessary */
659 while(len
&& (w
= textnw(m
, buf
, len
)) > m
->dc
.w
- h
)
670 return; /* too long */
671 XSetForeground(dpy
, m
->dc
.gc
, col
[ColFG
]);
673 XmbDrawString(dpy
, m
->dc
.drawable
, m
->dc
.font
.set
, m
->dc
.gc
, x
, y
, buf
, len
);
675 XDrawString(dpy
, m
->dc
.drawable
, m
->dc
.gc
, x
, y
, buf
, len
);
679 emallocz(unsigned int size
) {
680 void *res
= calloc(1, size
);
683 eprint("fatal: could not malloc() %u bytes\n", size
);
688 enternotify(XEvent
*e
) {
690 XCrossingEvent
*ev
= &e
->xcrossing
;
692 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
694 if((c
= getclient(ev
->window
)))
696 else if(ev
->window
== root
) {
703 eprint(const char *errstr
, ...) {
706 va_start(ap
, errstr
);
707 vfprintf(stderr
, errstr
, ap
);
714 XExposeEvent
*ev
= &e
->xexpose
;
717 if(ev
->window
== monitors
[selmonitor
].barwin
)
723 floating(void) { /* default floating layout */
726 domwfact
= dozoom
= False
;
727 for(c
= clients
; c
; c
= c
->next
)
728 if(isvisible(c
, &monitors
[selmonitor
]))
729 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
734 Monitor
*m
= &monitors
[monitorat(-1, -1)];
735 if(!c
|| (c
&& !isvisible(c
, m
)))
736 for(c
= stack
; c
&& !isvisible(c
, m
); c
= c
->snext
);
737 if(sel
&& sel
!= c
) {
738 grabbuttons(sel
, False
);
739 XSetWindowBorder(dpy
, sel
->win
, monitors
[sel
->monitor
].dc
.norm
[ColBorder
]);
744 grabbuttons(c
, True
);
749 XSetWindowBorder(dpy
, c
->win
, monitors
[c
->monitor
].dc
.sel
[ColBorder
]);
750 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
751 selmonitor
= monitorat(c
->x
, c
->y
);
754 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
755 selmonitor
= monitorat(-1, -1);
760 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
761 XFocusChangeEvent
*ev
= &e
->xfocus
;
763 if(sel
&& ev
->window
!= sel
->win
)
764 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
768 focusnext(const char *arg
) {
770 Monitor
*m
= &monitors
[selmonitor
];
774 for(c
= sel
->next
; c
&& !isvisible(c
, m
); c
= c
->next
);
776 for(c
= clients
; c
&& !isvisible(c
, m
); c
= c
->next
);
784 focusprev(const char *arg
) {
786 Monitor
*m
= &monitors
[selmonitor
];
790 for(c
= sel
->prev
; c
&& !isvisible(c
, m
); c
= c
->prev
);
792 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
793 for(; c
&& !isvisible(c
, m
); 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
);
823 unsigned char *p
= NULL
;
824 unsigned long n
, extra
;
827 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
828 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
829 if(status
!= Success
)
838 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
843 if(!text
|| size
== 0)
846 XGetTextProperty(dpy
, w
, &name
, atom
);
849 if(name
.encoding
== XA_STRING
)
850 strncpy(text
, (char *)name
.value
, size
- 1);
852 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
854 strncpy(text
, *list
, size
- 1);
855 XFreeStringList(list
);
858 text
[size
- 1] = '\0';
864 grabbuttons(Client
*c
, Bool focused
) {
865 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
868 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
869 GrabModeAsync
, GrabModeSync
, None
, None
);
870 XGrabButton(dpy
, Button1
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
871 GrabModeAsync
, GrabModeSync
, None
, None
);
872 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
873 GrabModeAsync
, GrabModeSync
, None
, None
);
874 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
875 GrabModeAsync
, GrabModeSync
, None
, None
);
877 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
878 GrabModeAsync
, GrabModeSync
, None
, None
);
879 XGrabButton(dpy
, Button2
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
880 GrabModeAsync
, GrabModeSync
, None
, None
);
881 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
882 GrabModeAsync
, GrabModeSync
, None
, None
);
883 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
884 GrabModeAsync
, GrabModeSync
, None
, None
);
886 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
887 GrabModeAsync
, GrabModeSync
, None
, None
);
888 XGrabButton(dpy
, Button3
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
889 GrabModeAsync
, GrabModeSync
, None
, None
);
890 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
891 GrabModeAsync
, GrabModeSync
, None
, None
);
892 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
893 GrabModeAsync
, GrabModeSync
, None
, None
);
896 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
897 GrabModeAsync
, GrabModeSync
, None
, None
);
905 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
906 for(i
= 0; i
< LENGTH(keys
); i
++) {
907 code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
);
908 XGrabKey(dpy
, code
, keys
[i
].mod
, root
, True
,
909 GrabModeAsync
, GrabModeAsync
);
910 XGrabKey(dpy
, code
, keys
[i
].mod
| LockMask
, root
, True
,
911 GrabModeAsync
, GrabModeAsync
);
912 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
, root
, True
,
913 GrabModeAsync
, GrabModeAsync
);
914 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
| LockMask
, root
, True
,
915 GrabModeAsync
, GrabModeAsync
);
920 idxoftag(const char *tag
) {
923 for(i
= 0; (i
< LENGTH(tags
)) && (tags
[i
] != tag
); i
++);
924 return (i
< LENGTH(tags
)) ? i
: 0;
928 initfont(Monitor
*m
, const char *fontstr
) {
929 char *def
, **missing
;
934 XFreeFontSet(dpy
, m
->dc
.font
.set
);
935 m
->dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
938 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
939 XFreeStringList(missing
);
942 XFontSetExtents
*font_extents
;
943 XFontStruct
**xfonts
;
945 m
->dc
.font
.ascent
= m
->dc
.font
.descent
= 0;
946 font_extents
= XExtentsOfFontSet(m
->dc
.font
.set
);
947 n
= XFontsOfFontSet(m
->dc
.font
.set
, &xfonts
, &font_names
);
948 for(i
= 0, m
->dc
.font
.ascent
= 0, m
->dc
.font
.descent
= 0; i
< n
; i
++) {
949 if(m
->dc
.font
.ascent
< (*xfonts
)->ascent
)
950 m
->dc
.font
.ascent
= (*xfonts
)->ascent
;
951 if(m
->dc
.font
.descent
< (*xfonts
)->descent
)
952 m
->dc
.font
.descent
= (*xfonts
)->descent
;
958 XFreeFont(dpy
, m
->dc
.font
.xfont
);
959 m
->dc
.font
.xfont
= NULL
;
960 if(!(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
961 && !(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
962 eprint("error, cannot load font: '%s'\n", fontstr
);
963 m
->dc
.font
.ascent
= m
->dc
.font
.xfont
->ascent
;
964 m
->dc
.font
.descent
= m
->dc
.font
.xfont
->descent
;
966 m
->dc
.font
.height
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
970 isoccupied(Monitor
*m
, unsigned int t
) {
973 for(c
= clients
; c
; c
= c
->next
)
974 if(c
->tags
[t
] && c
->monitor
== m
->id
)
980 isprotodel(Client
*c
) {
985 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
986 for(i
= 0; !ret
&& i
< n
; i
++)
987 if(protocols
[i
] == wmatom
[WMDelete
])
995 isvisible(Client
*c
, Monitor
*m
) {
998 for(i
= 0; i
< LENGTH(tags
); i
++)
999 if(c
->tags
[i
] && monitors
[c
->monitor
].seltags
[i
] && m
->id
== c
->monitor
)
1005 keypress(XEvent
*e
) {
1011 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1012 for(i
= 0; i
< LENGTH(keys
); i
++)
1013 if(keysym
== keys
[i
].keysym
1014 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
1017 keys
[i
].func(keys
[i
].arg
);
1022 killclient(const char *arg
) {
1027 if(isprotodel(sel
)) {
1028 ev
.type
= ClientMessage
;
1029 ev
.xclient
.window
= sel
->win
;
1030 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1031 ev
.xclient
.format
= 32;
1032 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1033 ev
.xclient
.data
.l
[1] = CurrentTime
;
1034 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
1037 XKillClient(dpy
, sel
->win
);
1041 leavenotify(XEvent
*e
) {
1042 XCrossingEvent
*ev
= &e
->xcrossing
;
1044 if((ev
->window
== root
) && !ev
->same_screen
) {
1051 manage(Window w
, XWindowAttributes
*wa
) {
1052 Client
*c
, *t
= NULL
;
1057 c
= emallocz(sizeof(Client
));
1058 c
->tags
= emallocz(sizeof initags
);
1062 Monitor
*m
= &monitors
[c
->monitor
];
1068 c
->oldborder
= wa
->border_width
;
1070 if (monitorat(c
->x
, c
->y
) != c
->monitor
) {
1075 if(c
->w
== m
->sw
&& c
->h
== m
->sh
) {
1078 c
->border
= wa
->border_width
;
1081 if(c
->x
+ c
->w
+ 2 * c
->border
> m
->wax
+ m
->waw
)
1082 c
->x
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1083 if(c
->y
+ c
->h
+ 2 * c
->border
> m
->way
+ m
->wah
)
1084 c
->y
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1089 c
->border
= BORDERPX
;
1091 wc
.border_width
= c
->border
;
1092 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1093 XSetWindowBorder(dpy
, w
, m
->dc
.norm
[ColBorder
]);
1094 configure(c
); /* propagates border_width, if size doesn't change */
1096 XSelectInput(dpy
, w
, EnterWindowMask
| FocusChangeMask
| PropertyChangeMask
| StructureNotifyMask
);
1097 grabbuttons(c
, False
);
1099 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
1100 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
1102 memcpy(c
->tags
, t
->tags
, sizeof initags
);
1104 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
1107 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1109 XMapWindow(dpy
, c
->win
);
1110 setclientstate(c
, NormalState
);
1115 mappingnotify(XEvent
*e
) {
1116 XMappingEvent
*ev
= &e
->xmapping
;
1118 XRefreshKeyboardMapping(ev
);
1119 if(ev
->request
== MappingKeyboard
)
1124 maprequest(XEvent
*e
) {
1125 static XWindowAttributes wa
;
1126 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1128 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1130 if(wa
.override_redirect
)
1132 if(!getclient(ev
->window
))
1133 manage(ev
->window
, &wa
);
1137 movemouse(Client
*c
) {
1138 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1145 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1146 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1148 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1150 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1153 XUngrabPointer(dpy
, CurrentTime
);
1155 case ConfigureRequest
:
1158 handler
[ev
.type
](&ev
);
1162 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1163 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1164 Monitor
*m
= &monitors
[monitorat(nx
, ny
)];
1165 if(abs(m
->wax
- nx
) < SNAP
)
1167 else if(abs((m
->wax
+ m
->waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
1168 nx
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1169 if(abs(m
->way
- ny
) < SNAP
)
1171 else if(abs((m
->way
+ m
->wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
1172 ny
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1173 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1174 memcpy(c
->tags
, monitors
[monitorat(nx
, ny
)].seltags
, sizeof initags
);
1181 nexttiled(Client
*c
, Monitor
*m
) {
1182 for(; c
&& (c
->isfloating
|| !isvisible(c
, m
)); c
= c
->next
);
1187 propertynotify(XEvent
*e
) {
1190 XPropertyEvent
*ev
= &e
->xproperty
;
1192 if(ev
->state
== PropertyDelete
)
1193 return; /* ignore */
1194 if((c
= getclient(ev
->window
))) {
1197 case XA_WM_TRANSIENT_FOR
:
1198 XGetTransientForHint(dpy
, c
->win
, &trans
);
1199 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1202 case XA_WM_NORMAL_HINTS
:
1206 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1215 quit(const char *arg
) {
1216 readin
= running
= False
;
1220 reapply(const char *arg
) {
1221 static Bool zerotags
[LENGTH(tags
)] = { 0 };
1224 for(c
= clients
; c
; c
= c
->next
) {
1225 memcpy(c
->tags
, zerotags
, sizeof zerotags
);
1232 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1234 Monitor scr
= monitors
[monitorat(x
, y
)];
1235 c
->monitor
= scr
.id
;
1238 /* set minimum possible */
1244 /* temporarily remove base dimensions */
1248 /* adjust for aspect limits */
1249 if (c
->minay
> 0 && c
->maxay
> 0 && c
->minax
> 0 && c
->maxax
> 0) {
1250 if (w
* c
->maxay
> h
* c
->maxax
)
1251 w
= h
* c
->maxax
/ c
->maxay
;
1252 else if (w
* c
->minay
< h
* c
->minax
)
1253 h
= w
* c
->minay
/ c
->minax
;
1256 /* adjust for increment value */
1262 /* restore base dimensions */
1266 if(c
->minw
> 0 && w
< c
->minw
)
1268 if(c
->minh
> 0 && h
< c
->minh
)
1270 if(c
->maxw
> 0 && w
> c
->maxw
)
1272 if(c
->maxh
> 0 && h
> c
->maxh
)
1275 if(w
<= 0 || h
<= 0)
1277 /* TODO: offscreen appearance fixes */
1280 x = scr.sw - w - 2 * c->border;
1282 y = scr.sh - h - 2 * c->border;
1283 if(x + w + 2 * c->border < scr.sx)
1285 if(y + h + 2 * c->border < scr.sy)
1288 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1291 c
->w
= wc
.width
= w
;
1292 c
->h
= wc
.height
= h
;
1293 wc
.border_width
= c
->border
;
1294 XConfigureWindow(dpy
, c
->win
, CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
, &wc
);
1301 resizemouse(Client
*c
) {
1308 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1309 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1311 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1313 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1316 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1317 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1318 XUngrabPointer(dpy
, CurrentTime
);
1319 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1321 case ConfigureRequest
:
1324 handler
[ev
.type
](&ev
);
1328 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
1330 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
1332 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1348 if(sel
->isfloating
|| (monitors
[selmonitor
].layout
->arrange
== floating
))
1349 XRaiseWindow(dpy
, sel
->win
);
1350 if(monitors
[selmonitor
].layout
->arrange
!= floating
) {
1351 wc
.stack_mode
= Below
;
1352 wc
.sibling
= monitors
[selmonitor
].barwin
;
1353 if(!sel
->isfloating
) {
1354 XConfigureWindow(dpy
, sel
->win
, CWSibling
| CWStackMode
, &wc
);
1355 wc
.sibling
= sel
->win
;
1357 for(s
= 0; s
< mcount
; s
++) {
1358 for(c
= nexttiled(clients
, &monitors
[s
]); c
; c
= nexttiled(c
->next
, &monitors
[s
])) {
1361 XConfigureWindow(dpy
, c
->win
, CWSibling
| CWStackMode
, &wc
);
1362 wc
.sibling
= c
->win
;
1367 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1373 char buf
[sizeof stext
];
1376 unsigned int len
, offset
;
1379 /* main event loop, also reads status text from stdin */
1381 xfd
= ConnectionNumber(dpy
);
1384 len
= sizeof stext
- 1;
1385 buf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1389 FD_SET(STDIN_FILENO
, &rd
);
1391 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1394 eprint("select failed\n");
1396 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1397 switch((r
= read(STDIN_FILENO
, buf
+ offset
, len
- offset
))) {
1399 strncpy(stext
, strerror(errno
), len
);
1403 strncpy(stext
, "EOF", 4);
1407 for(p
= buf
+ offset
; r
> 0; p
++, r
--, offset
++)
1408 if(*p
== '\n' || *p
== '\0') {
1410 strncpy(stext
, buf
, len
);
1411 p
+= r
- 1; /* p is buf + offset + r - 1 */
1412 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1415 memmove(buf
, p
- r
+ 1, r
);
1422 while(XPending(dpy
)) {
1423 XNextEvent(dpy
, &ev
);
1424 if(handler
[ev
.type
])
1425 (handler
[ev
.type
])(&ev
); /* call handler */
1432 unsigned int i
, num
;
1433 Window
*wins
, d1
, d2
;
1434 XWindowAttributes wa
;
1437 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1438 for(i
= 0; i
< num
; i
++) {
1439 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1440 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1442 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1443 manage(wins
[i
], &wa
);
1445 for(i
= 0; i
< num
; i
++) { /* now the transients */
1446 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1448 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1449 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1450 manage(wins
[i
], &wa
);
1458 setclientstate(Client
*c
, long state
) {
1459 long data
[] = {state
, None
};
1461 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1462 PropModeReplace
, (unsigned char *)data
, 2);
1466 setlayout(const char *arg
) {
1468 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1472 if(m
->layout
== &layouts
[LENGTH(layouts
)])
1473 m
->layout
= &layouts
[0];
1476 for(i
= 0; i
< LENGTH(layouts
); i
++)
1477 if(!strcmp(arg
, layouts
[i
].symbol
))
1479 if(i
== LENGTH(layouts
))
1481 m
->layout
= &layouts
[i
];
1490 setmwfact(const char *arg
) {
1493 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1497 /* arg handling, manipulate mwfact */
1500 else if(sscanf(arg
, "%lf", &delta
) == 1) {
1501 if(arg
[0] == '+' || arg
[0] == '-')
1507 else if(m
->mwfact
> 0.9)
1515 unsigned int i
, j
, k
;
1516 XModifierKeymap
*modmap
;
1517 XSetWindowAttributes wa
;
1520 XineramaScreenInfo
*info
= NULL
;
1523 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1524 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1525 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1526 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1527 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1528 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1529 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1530 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1533 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1534 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1535 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1538 /* init modifier map */
1539 modmap
= XGetModifierMapping(dpy
);
1540 for(i
= 0; i
< 8; i
++)
1541 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
1542 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1543 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1544 numlockmask
= (1 << i
);
1546 XFreeModifiermap(modmap
);
1548 /* select for events */
1549 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
1550 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
1551 wa
.cursor
= cursor
[CurNormal
];
1552 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
1553 XSelectInput(dpy
, root
, wa
.event_mask
);
1561 if (XineramaIsActive(dpy
)) {
1562 info
= XineramaQueryScreens(dpy
, &s
);
1565 monitors
= emallocz(s
*sizeof(Monitor
));
1568 for(i
= 0; i
< s
; i
++) {
1571 monitors
[i
].sx
= info
[i
].x_org
;
1572 monitors
[i
].sy
= info
[i
].y_org
;
1573 monitors
[i
].sw
= info
[i
].width
;
1574 monitors
[i
].sh
= info
[i
].height
;
1579 monitors
[i
].sw
= DisplayWidth(dpy
, screen
);
1580 monitors
[i
].sh
= DisplayHeight(dpy
, screen
);
1584 monitors
[i
].seltags
= emallocz(sizeof initags
);
1585 monitors
[i
].prevtags
= emallocz(sizeof initags
);
1587 memcpy(monitors
[i
].seltags
, initags
, sizeof initags
);
1588 memcpy(monitors
[i
].prevtags
, initags
, sizeof initags
);
1590 /* init appearance */
1591 monitors
[i
].dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
);
1592 monitors
[i
].dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
1593 monitors
[i
].dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
1594 monitors
[i
].dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
);
1595 monitors
[i
].dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
1596 monitors
[i
].dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
1597 initfont(&(monitors
[i
]), FONT
);
1598 monitors
[i
].dc
.h
= bh
= monitors
[i
].dc
.font
.height
+ 2;
1601 monitors
[i
].mwfact
= MWFACT
;
1602 monitors
[i
].layout
= &layouts
[0];
1603 for(blw
= k
= 0; k
< LENGTH(layouts
); k
++) {
1604 j
= textw(&monitors
[i
], layouts
[k
].symbol
);
1610 wa
.override_redirect
= 1;
1611 wa
.background_pixmap
= ParentRelative
;
1612 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
1615 monitors
[i
].barwin
= XCreateWindow(dpy
, root
, monitors
[i
].sx
, monitors
[i
].sy
, monitors
[i
].sw
, bh
, 0,
1616 DefaultDepth(dpy
, screen
), CopyFromParent
, DefaultVisual(dpy
, screen
),
1617 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
1618 XDefineCursor(dpy
, monitors
[i
].barwin
, cursor
[CurNormal
]);
1619 updatebarpos(&monitors
[i
]);
1620 XMapRaised(dpy
, monitors
[i
].barwin
);
1621 strcpy(stext
, "dwm-"VERSION
);
1622 monitors
[i
].dc
.drawable
= XCreatePixmap(dpy
, root
, monitors
[i
].sw
, bh
, DefaultDepth(dpy
, screen
));
1623 g
= XCreateGC(dpy
, root
, 0, 0);
1624 monitors
[i
].dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1625 XSetLineAttributes(dpy
, monitors
[i
].dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1626 if(!monitors
[i
].dc
.font
.set
)
1627 XSetFont(dpy
, monitors
[i
].dc
.gc
, monitors
[i
].dc
.font
.xfont
->fid
);
1632 spawn(const char *arg
) {
1633 static char *shell
= NULL
;
1635 if(!shell
&& !(shell
= getenv("SHELL")))
1639 /* The double-fork construct avoids zombie processes and keeps the code
1640 * clean from stupid signal handlers. */
1644 close(ConnectionNumber(dpy
));
1646 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1647 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1656 tag(const char *arg
) {
1661 for(i
= 0; i
< LENGTH(tags
); i
++)
1662 sel
->tags
[i
] = (NULL
== arg
);
1663 sel
->tags
[idxoftag(arg
)] = True
;
1668 textnw(Monitor
*m
, const char *text
, unsigned int len
) {
1671 if(m
->dc
.font
.set
) {
1672 XmbTextExtents(m
->dc
.font
.set
, text
, len
, NULL
, &r
);
1675 return XTextWidth(m
->dc
.font
.xfont
, text
, len
);
1679 textw(Monitor
*m
, const char *text
) {
1680 return textnw(m
, text
, strlen(text
)) + m
->dc
.font
.height
;
1685 unsigned int i
, n
, nx
, ny
, nw
, nh
, mw
, th
;
1689 domwfact
= dozoom
= True
;
1691 nw
= 0; /* gcc stupidity requires this */
1693 for (s
= 0; s
< mcount
; s
++) {
1694 Monitor
*m
= &monitors
[s
];
1696 for(n
= 0, c
= nexttiled(clients
, m
); c
; c
= nexttiled(c
->next
, m
))
1699 for(i
= 0, c
= mc
= nexttiled(clients
, m
); c
; c
= nexttiled(c
->next
, m
)) {
1701 mw
= (n
== 1) ? m
->waw
: m
->mwfact
* m
->waw
;
1702 th
= (n
> 1) ? m
->wah
/ (n
- 1) : 0;
1703 if(n
> 1 && th
< bh
)
1705 if(i
== 0) { /* master */
1708 nw
= mw
- 2 * c
->border
;
1709 nh
= m
->wah
- 2 * c
->border
;
1711 else { /* tile window */
1714 nx
+= mc
->w
+ 2 * mc
->border
;
1715 nw
= m
->waw
- mw
- 2 * c
->border
;
1717 if(i
+ 1 == n
) /* remainder */
1718 nh
= (m
->way
+ m
->wah
) - ny
- 2 * c
->border
;
1720 nh
= th
- 2 * c
->border
;
1722 resize(c
, nx
, ny
, nw
, nh
, RESIZEHINTS
);
1723 if((RESIZEHINTS
) && ((c
->h
< bh
) || (c
->h
> nh
) || (c
->w
< bh
) || (c
->w
> nw
)))
1724 /* client doesn't accept size constraints */
1725 resize(c
, nx
, ny
, nw
, nh
, False
);
1726 if(n
> 1 && th
!= m
->wah
)
1727 ny
= c
->y
+ c
->h
+ 2 * c
->border
;
1734 togglebar(const char *arg
) {
1736 bpos
= (BARPOS
== BarOff
) ? BarTop
: BARPOS
;
1739 updatebarpos(&monitors
[monitorat(-1,-1)]);
1744 togglefloating(const char *arg
) {
1747 sel
->isfloating
= !sel
->isfloating
;
1749 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1754 toggletag(const char *arg
) {
1760 sel
->tags
[i
] = !sel
->tags
[i
];
1761 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1762 if(j
== LENGTH(tags
))
1763 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1768 toggleview(const char *arg
) {
1771 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1774 m
->seltags
[i
] = !m
->seltags
[i
];
1775 for(j
= 0; j
< LENGTH(tags
) && !m
->seltags
[j
]; j
++);
1776 if(j
== LENGTH(tags
))
1777 m
->seltags
[i
] = True
; /* at least one tag must be viewed */
1785 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1786 c
->isbanned
= False
;
1790 unmanage(Client
*c
) {
1793 wc
.border_width
= c
->oldborder
;
1794 /* The server grab construct avoids race conditions. */
1796 XSetErrorHandler(xerrordummy
);
1797 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1802 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1803 setclientstate(c
, WithdrawnState
);
1807 XSetErrorHandler(xerror
);
1813 unmapnotify(XEvent
*e
) {
1815 XUnmapEvent
*ev
= &e
->xunmap
;
1817 if((c
= getclient(ev
->window
)))
1822 updatebarpos(Monitor
*s
) {
1833 XMoveWindow(dpy
, s
->barwin
, s
->sx
, s
->sy
);
1837 XMoveWindow(dpy
, s
->barwin
, s
->sx
, s
->sy
+ s
->wah
);
1840 XMoveWindow(dpy
, s
->barwin
, s
->sx
, s
->sy
- bh
);
1844 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1848 updatesizehints(Client
*c
) {
1852 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1854 c
->flags
= size
.flags
;
1855 if(c
->flags
& PBaseSize
) {
1856 c
->basew
= size
.base_width
;
1857 c
->baseh
= size
.base_height
;
1859 else if(c
->flags
& PMinSize
) {
1860 c
->basew
= size
.min_width
;
1861 c
->baseh
= size
.min_height
;
1864 c
->basew
= c
->baseh
= 0;
1865 if(c
->flags
& PResizeInc
) {
1866 c
->incw
= size
.width_inc
;
1867 c
->inch
= size
.height_inc
;
1870 c
->incw
= c
->inch
= 0;
1871 if(c
->flags
& PMaxSize
) {
1872 c
->maxw
= size
.max_width
;
1873 c
->maxh
= size
.max_height
;
1876 c
->maxw
= c
->maxh
= 0;
1877 if(c
->flags
& PMinSize
) {
1878 c
->minw
= size
.min_width
;
1879 c
->minh
= size
.min_height
;
1881 else if(c
->flags
& PBaseSize
) {
1882 c
->minw
= size
.base_width
;
1883 c
->minh
= size
.base_height
;
1886 c
->minw
= c
->minh
= 0;
1887 if(c
->flags
& PAspect
) {
1888 c
->minax
= size
.min_aspect
.x
;
1889 c
->maxax
= size
.max_aspect
.x
;
1890 c
->minay
= size
.min_aspect
.y
;
1891 c
->maxay
= size
.max_aspect
.y
;
1894 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1895 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1896 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1900 updatetitle(Client
*c
) {
1901 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1902 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1905 /* There's no way to check accesses to destroyed windows, thus those cases are
1906 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1907 * default error handler, which may call exit. */
1909 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1910 if(ee
->error_code
== BadWindow
1911 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1912 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1913 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1914 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1915 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1916 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1917 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1919 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1920 ee
->request_code
, ee
->error_code
);
1921 return xerrorxlib(dpy
, ee
); /* may call exit */
1925 xerrordummy(Display
*dsply
, XErrorEvent
*ee
) {
1929 /* Startup Error handler to check if another window manager
1930 * is already running. */
1932 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
1938 view(const char *arg
) {
1941 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1943 memcpy(m
->prevtags
, m
->seltags
, sizeof initags
);
1944 for(i
= 0; i
< LENGTH(tags
); i
++)
1945 m
->seltags
[i
] = (NULL
== arg
);
1946 m
->seltags
[idxoftag(arg
)] = True
;
1951 viewprevtag(const char *arg
) {
1952 static Bool tmp
[LENGTH(tags
)];
1954 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1956 memcpy(tmp
, m
->seltags
, sizeof initags
);
1957 memcpy(m
->seltags
, m
->prevtags
, sizeof initags
);
1958 memcpy(m
->prevtags
, tmp
, sizeof initags
);
1963 zoom(const char *arg
) {
1966 if(!sel
|| !dozoom
|| sel
->isfloating
)
1968 if((c
= sel
) == nexttiled(clients
, &monitors
[c
->monitor
]))
1969 if(!(c
= nexttiled(c
->next
, &monitors
[c
->monitor
])))
1978 monitorat(int x
, int y
) {
1981 if(!XineramaIsActive(dpy
))
1984 if (x
< 0 || y
< 0) {
1987 XQueryPointer(dpy
, root
, &win
, &win
, &x
, &y
, &i
, &i
, &mask
);
1990 for(i
= 0; i
< mcount
; i
++)
1991 if((x
< 0 || (x
>= monitors
[i
].sx
&& x
< monitors
[i
].sx
+ monitors
[i
].sw
))
1992 && (y
< 0 || (y
>= monitors
[i
].sy
&& y
< monitors
[i
].sy
+ monitors
[i
].sh
)))
2000 movetomonitor(const char *arg
) {
2002 sel
->monitor
= arg
? atoi(arg
) : (sel
->monitor
+1) % mcount
;
2004 memcpy(sel
->tags
, monitors
[sel
->monitor
].seltags
, sizeof initags
);
2005 resize(sel
, monitors
[sel
->monitor
].wax
, monitors
[sel
->monitor
].way
, sel
->w
, sel
->h
, True
);
2011 selectmonitor(const char *arg
) {
2012 Monitor
*m
= &monitors
[arg
? atoi(arg
) : (monitorat(-1, -1)+1) % mcount
];
2014 XWarpPointer(dpy
, None
, root
, 0, 0, 0, 0, m
->wax
+m
->waw
/2, m
->way
+m
->wah
/2);
2020 main(int argc
, char *argv
[]) {
2021 if(argc
== 2 && !strcmp("-v", argv
[1]))
2022 eprint("dwm-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
2023 "Jukka Salmi, Premysl Hruby, Szabolcs Nagy\n");
2025 eprint("usage: dwm [-v]\n");
2027 setlocale(LC_CTYPE
, "");
2028 if(!(dpy
= XOpenDisplay(0)))
2029 eprint("dwm: cannot open display\n");
2030 screen
= DefaultScreen(dpy
);
2031 root
= RootWindow(dpy
, screen
);