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);
123 int sx
, sy
, sw
, sh
, wax
, way
, wah
, waw
;
131 /* function declarations */
132 void applyrules(Client
*c
);
134 void attach(Client
*c
);
135 void attachstack(Client
*c
);
137 void buttonpress(XEvent
*e
);
138 void checkotherwm(void);
140 void compileregs(void);
141 void configure(Client
*c
);
142 void configurenotify(XEvent
*e
);
143 void configurerequest(XEvent
*e
);
144 void destroynotify(XEvent
*e
);
145 void detach(Client
*c
);
146 void detachstack(Client
*c
);
148 void drawsquare(Monitor
*, Bool filled
, Bool empty
, unsigned long col
[ColLast
]);
149 void drawtext(Monitor
*, const char *text
, unsigned long col
[ColLast
]);
150 void *emallocz(unsigned int size
);
151 void enternotify(XEvent
*e
);
152 void eprint(const char *errstr
, ...);
153 void expose(XEvent
*e
);
154 void floating(void); /* default floating layout */
155 void focus(Client
*c
);
156 void focusin(XEvent
*e
);
157 void focusnext(const char *arg
);
158 void focusprev(const char *arg
);
159 Client
*getclient(Window w
);
160 unsigned long getcolor(const char *colstr
, int screen
);
161 long getstate(Window w
);
162 Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
163 void grabbuttons(Client
*c
, Bool focused
);
165 unsigned int idxoftag(const char *tag
);
166 void initfont(Monitor
*, const char *fontstr
);
167 Bool
isoccupied(Monitor
*m
, unsigned int t
);
168 Bool
isprotodel(Client
*c
);
169 Bool
isvisible(Client
*c
, Monitor
*m
);
170 void keypress(XEvent
*e
);
171 void killclient(const char *arg
);
172 void manage(Window w
, XWindowAttributes
*wa
);
173 void mappingnotify(XEvent
*e
);
174 void maprequest(XEvent
*e
);
175 void movemouse(Client
*c
);
176 Client
*nexttiled(Client
*c
, Monitor
*m
);
177 void propertynotify(XEvent
*e
);
178 void quit(const char *arg
);
179 void reapply(const char *arg
);
180 void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
);
181 void resizemouse(Client
*c
);
185 void setclientstate(Client
*c
, long state
);
186 void setlayout(const char *arg
);
187 void setmwfact(const char *arg
);
189 void spawn(const char *arg
);
190 void tag(const char *arg
);
191 unsigned int textnw(Monitor
*, const char *text
, unsigned int len
);
192 unsigned int textw(Monitor
*, const char *text
);
194 void togglebar(const char *arg
);
195 void togglefloating(const char *arg
);
196 void toggletag(const char *arg
);
197 void toggleview(const char *arg
);
198 void unban(Client
*c
);
199 void unmanage(Client
*c
);
200 void unmapnotify(XEvent
*e
);
201 void updatebarpos(Monitor
*s
);
202 void updatesizehints(Client
*c
);
203 void updatetitle(Client
*c
);
204 void view(const char *arg
);
205 void viewprevtag(const char *arg
); /* views previous selected tags */
206 int xerror(Display
*dpy
, XErrorEvent
*ee
);
207 int xerrordummy(Display
*dsply
, XErrorEvent
*ee
);
208 int xerrorstart(Display
*dsply
, XErrorEvent
*ee
);
209 void zoom(const char *arg
);
210 int monitorat(int, int);
211 void movetomonitor(const char *arg
);
212 void selectmonitor(const char *arg
);
218 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
219 unsigned int bh
, bpos
;
220 unsigned int blw
= 0;
221 unsigned int numlockmask
= 0;
222 void (*handler
[LASTEvent
]) (XEvent
*) = {
223 [ButtonPress
] = buttonpress
,
224 [ConfigureRequest
] = configurerequest
,
225 [ConfigureNotify
] = configurenotify
,
226 [DestroyNotify
] = destroynotify
,
227 [EnterNotify
] = enternotify
,
230 [KeyPress
] = keypress
,
231 [MappingNotify
] = mappingnotify
,
232 [MapRequest
] = maprequest
,
233 [PropertyNotify
] = propertynotify
,
234 [UnmapNotify
] = unmapnotify
236 Atom wmatom
[WMLast
], netatom
[NetLast
];
237 Bool domwfact
= True
;
239 Bool otherwm
, readin
;
241 Client
*clients
= NULL
;
243 Client
*stack
= NULL
;
244 Cursor cursor
[CurLast
];
251 /* configuration, allows nested code to access above variables */
254 //Bool prevtags[LENGTH(tags)];
256 /* function implementations */
258 applyrules(Client
*c
) {
259 static char buf
[512];
262 Bool matched_tag
= False
;
263 Bool matched_monitor
= False
;
264 XClassHint ch
= { 0 };
267 XGetClassHint(dpy
, c
->win
, &ch
);
268 snprintf(buf
, sizeof buf
, "%s:%s:%s",
269 ch
.res_class
? ch
.res_class
: "",
270 ch
.res_name
? ch
.res_name
: "", c
->name
);
271 for(i
= 0; i
< LENGTH(rules
); i
++)
272 if(regs
[i
].propregex
&& !regexec(regs
[i
].propregex
, buf
, 1, &tmp
, 0)) {
273 if (rules
[i
].monitor
>= 0 && rules
[i
].monitor
< mcount
) {
274 matched_monitor
= True
;
275 c
->monitor
= rules
[i
].monitor
;
278 c
->isfloating
= rules
[i
].isfloating
;
279 for(j
= 0; regs
[i
].tagregex
&& j
< LENGTH(tags
); j
++) {
280 if(!regexec(regs
[i
].tagregex
, tags
[j
], 1, &tmp
, 0)) {
291 memcpy(c
->tags
, monitors
[monitorat(-1, -1)].seltags
, sizeof initags
);
292 if (!matched_monitor
)
293 c
->monitor
= monitorat(-1, -1);
300 for(c
= clients
; c
; c
= c
->next
)
301 if(isvisible(c
, &monitors
[c
->monitor
]))
306 monitors
[selmonitor
].layout
->arrange();
320 attachstack(Client
*c
) {
329 XMoveWindow(dpy
, c
->win
, c
->x
+ 3 * monitors
[c
->monitor
].sw
, c
->y
);
334 buttonpress(XEvent
*e
) {
337 XButtonPressedEvent
*ev
= &e
->xbutton
;
339 Monitor s
= monitors
[monitorat(-1, -1)];
341 if(ev
->window
== s
.barwin
) {
343 for(i
= 0; i
< LENGTH(tags
); i
++) {
344 x
+= textw(&s
, tags
[i
]);
346 if(ev
->button
== Button1
) {
347 if(ev
->state
& MODKEY
)
352 else if(ev
->button
== Button3
) {
353 if(ev
->state
& MODKEY
)
361 if((ev
->x
< x
+ blw
) && ev
->button
== Button1
)
364 else if((c
= getclient(ev
->window
))) {
366 if(CLEANMASK(ev
->state
) != MODKEY
)
368 if(ev
->button
== Button1
) {
369 if((s
.layout
->arrange
== floating
) || c
->isfloating
)
372 togglefloating(NULL
);
375 else if(ev
->button
== Button2
) {
376 if((floating
!= s
.layout
->arrange
) && c
->isfloating
)
377 togglefloating(NULL
);
381 else if(ev
->button
== Button3
&& !c
->isfixed
) {
382 if((floating
== s
.layout
->arrange
) || c
->isfloating
)
385 togglefloating(NULL
);
394 XSetErrorHandler(xerrorstart
);
396 /* this causes an error if some other window manager is running */
397 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
400 eprint("dwm: another window manager is already running\n");
402 XSetErrorHandler(NULL
);
403 xerrorxlib
= XSetErrorHandler(xerror
);
415 for(i
= 0; i
< mcount
; i
++) {
416 Monitor
*m
= &monitors
[i
];
418 XFreeFontSet(dpy
, m
->dc
.font
.set
);
420 XFreeFont(dpy
, m
->dc
.font
.xfont
);
421 XUngrabKey(dpy
, AnyKey
, AnyModifier
, m
->root
);
422 XFreePixmap(dpy
, m
->dc
.drawable
);
423 XFreeGC(dpy
, m
->dc
.gc
);
424 XDestroyWindow(dpy
, m
->barwin
);
425 XFreeCursor(dpy
, cursor
[CurNormal
]);
426 XFreeCursor(dpy
, cursor
[CurResize
]);
427 XFreeCursor(dpy
, cursor
[CurMove
]);
428 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
440 regs
= emallocz(LENGTH(rules
) * sizeof(Regs
));
441 for(i
= 0; i
< LENGTH(rules
); i
++) {
443 reg
= emallocz(sizeof(regex_t
));
444 if(regcomp(reg
, rules
[i
].prop
, REG_EXTENDED
))
447 regs
[i
].propregex
= reg
;
450 reg
= emallocz(sizeof(regex_t
));
451 if(regcomp(reg
, rules
[i
].tags
, REG_EXTENDED
))
454 regs
[i
].tagregex
= reg
;
460 configure(Client
*c
) {
463 ce
.type
= ConfigureNotify
;
471 ce
.border_width
= c
->border
;
473 ce
.override_redirect
= False
;
474 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
478 configurenotify(XEvent
*e
) {
479 XConfigureEvent
*ev
= &e
->xconfigure
;
480 Monitor
*m
= &monitors
[selmonitor
];
482 if(ev
->window
== m
->root
&& (ev
->width
!= m
->sw
|| ev
->height
!= m
->sh
)) {
485 XFreePixmap(dpy
, dc
.drawable
);
486 dc
.drawable
= XCreatePixmap(dpy
, m
->root
, m
->sw
, bh
, DefaultDepth(dpy
, m
->screen
));
487 XResizeWindow(dpy
, m
->barwin
, m
->sw
, bh
);
494 configurerequest(XEvent
*e
) {
496 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
499 if((c
= getclient(ev
->window
))) {
500 Monitor
*m
= &monitors
[c
->monitor
];
501 if(ev
->value_mask
& CWBorderWidth
)
502 c
->border
= ev
->border_width
;
503 if(c
->isfixed
|| c
->isfloating
|| (floating
== m
->layout
->arrange
)) {
504 if(ev
->value_mask
& CWX
)
506 if(ev
->value_mask
& CWY
)
508 if(ev
->value_mask
& CWWidth
)
510 if(ev
->value_mask
& CWHeight
)
512 if((c
->x
- m
->sx
+ c
->w
) > m
->sw
&& c
->isfloating
)
513 c
->x
= m
->sx
+ (m
->sw
/ 2 - c
->w
/ 2); /* center in x direction */
514 if((c
->y
- m
->sy
+ c
->h
) > m
->sh
&& c
->isfloating
)
515 c
->y
= m
->sy
+ (m
->sh
/ 2 - c
->h
/ 2); /* center in y direction */
516 if((ev
->value_mask
& (CWX
| CWY
))
517 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
519 if(isvisible(c
, &monitors
[monitorat(-1,-1)]))
520 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
528 wc
.width
= ev
->width
;
529 wc
.height
= ev
->height
;
530 wc
.border_width
= ev
->border_width
;
531 wc
.sibling
= ev
->above
;
532 wc
.stack_mode
= ev
->detail
;
533 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
539 destroynotify(XEvent
*e
) {
541 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
543 if((c
= getclient(ev
->window
)))
550 c
->prev
->next
= c
->next
;
552 c
->next
->prev
= c
->prev
;
555 c
->next
= c
->prev
= NULL
;
559 detachstack(Client
*c
) {
562 for(tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
570 for(s
= 0; s
< mcount
; ++s
) {
571 Monitor
*m
= &monitors
[s
];
573 for(i
= 0; i
< LENGTH(tags
); i
++) {
574 m
->dc
.w
= textw(m
, tags
[i
]);
576 drawtext(m
, tags
[i
], m
->dc
.sel
);
577 drawsquare(m
, sel
&& sel
->tags
[i
] && sel
->monitor
== selmonitor
, isoccupied(m
, i
), m
->dc
.sel
);
580 drawtext(m
, tags
[i
], m
->dc
.norm
);
581 drawsquare(m
, sel
&& sel
->tags
[i
] && sel
->monitor
== selmonitor
, isoccupied(m
, i
), m
->dc
.norm
);
586 drawtext(m
, m
->layout
->symbol
, m
->dc
.norm
);
587 x
= m
->dc
.x
+ m
->dc
.w
;
588 m
->dc
.w
= textw(m
, stext
);
589 m
->dc
.x
= m
->sw
- m
->dc
.w
;
594 drawtext(m
, stext
, m
->dc
.norm
);
595 if((m
->dc
.w
= m
->dc
.x
- x
) > bh
) {
597 if(sel
&& sel
->monitor
== selmonitor
) {
598 drawtext(m
, sel
->name
, m
->dc
.sel
);
599 drawsquare(m
, False
, sel
->isfloating
, m
->dc
.sel
);
602 drawtext(m
, NULL
, m
->dc
.norm
);
604 XCopyArea(dpy
, m
->dc
.drawable
, m
->barwin
, m
->dc
.gc
, 0, 0, m
->sw
, bh
, 0, 0);
610 drawsquare(Monitor
*m
, Bool filled
, Bool empty
, unsigned long col
[ColLast
]) {
613 XRectangle r
= { m
->dc
.x
, m
->dc
.y
, m
->dc
.w
, m
->dc
.h
};
615 gcv
.foreground
= col
[ColFG
];
616 XChangeGC(dpy
, m
->dc
.gc
, GCForeground
, &gcv
);
617 x
= (m
->dc
.font
.ascent
+ m
->dc
.font
.descent
+ 2) / 4;
621 r
.width
= r
.height
= x
+ 1;
622 XFillRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
625 r
.width
= r
.height
= x
;
626 XDrawRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
631 drawtext(Monitor
*m
, const char *text
, unsigned long col
[ColLast
]) {
633 static char buf
[256];
634 unsigned int len
, olen
;
635 XRectangle r
= { m
->dc
.x
, m
->dc
.y
, m
->dc
.w
, m
->dc
.h
};
637 XSetForeground(dpy
, m
->dc
.gc
, col
[ColBG
]);
638 XFillRectangles(dpy
, m
->dc
.drawable
, m
->dc
.gc
, &r
, 1);
642 olen
= len
= strlen(text
);
643 if(len
>= sizeof buf
)
644 len
= sizeof buf
- 1;
645 memcpy(buf
, text
, len
);
647 h
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
648 y
= m
->dc
.y
+ (m
->dc
.h
/ 2) - (h
/ 2) + m
->dc
.font
.ascent
;
649 x
= m
->dc
.x
+ (h
/ 2);
650 /* shorten text if necessary */
651 while(len
&& (w
= textnw(m
, buf
, len
)) > m
->dc
.w
- h
)
662 return; /* too long */
663 XSetForeground(dpy
, m
->dc
.gc
, col
[ColFG
]);
665 XmbDrawString(dpy
, m
->dc
.drawable
, m
->dc
.font
.set
, m
->dc
.gc
, x
, y
, buf
, len
);
667 XDrawString(dpy
, m
->dc
.drawable
, m
->dc
.gc
, x
, y
, buf
, len
);
671 emallocz(unsigned int size
) {
672 void *res
= calloc(1, size
);
675 eprint("fatal: could not malloc() %u bytes\n", size
);
680 enternotify(XEvent
*e
) {
683 XCrossingEvent
*ev
= &e
->xcrossing
;
685 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
)
687 if((c
= getclient(ev
->window
)))
690 for(i
= 0; i
< mcount
; i
++)
691 if(ev
->window
== monitors
[i
].root
) {
700 eprint(const char *errstr
, ...) {
703 va_start(ap
, errstr
);
704 vfprintf(stderr
, errstr
, ap
);
711 XExposeEvent
*ev
= &e
->xexpose
;
714 if(ev
->window
== monitors
[selmonitor
].barwin
)
720 floating(void) { /* default floating layout */
723 domwfact
= dozoom
= False
;
724 for(c
= clients
; c
; c
= c
->next
)
725 if(isvisible(c
, &monitors
[selmonitor
]))
726 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
731 Monitor
*m
= &monitors
[c
? c
->monitor
: selmonitor
];
732 if(!c
|| (c
&& !isvisible(c
, m
)))
733 for(c
= stack
; c
&& !isvisible(c
, m
); c
= c
->snext
);
734 if(sel
&& sel
!= c
) {
735 grabbuttons(sel
, False
);
736 XSetWindowBorder(dpy
, sel
->win
, monitors
[sel
->monitor
].dc
.norm
[ColBorder
]);
741 grabbuttons(c
, True
);
746 XSetWindowBorder(dpy
, c
->win
, m
->dc
.sel
[ColBorder
]);
747 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
748 selmonitor
= c
->monitor
;
751 XSetInputFocus(dpy
, m
->root
, RevertToPointerRoot
, CurrentTime
);
756 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
757 XFocusChangeEvent
*ev
= &e
->xfocus
;
759 if(sel
&& ev
->window
!= sel
->win
)
760 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
764 focusnext(const char *arg
) {
766 Monitor
*m
= &monitors
[selmonitor
];
770 for(c
= sel
->next
; c
&& !isvisible(c
, m
); c
= c
->next
);
772 for(c
= clients
; c
&& !isvisible(c
, m
); c
= c
->next
);
780 focusprev(const char *arg
) {
782 Monitor
*m
= &monitors
[selmonitor
];
786 for(c
= sel
->prev
; c
&& !isvisible(c
, m
); c
= c
->prev
);
788 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
789 for(; c
&& !isvisible(c
, m
); c
= c
->prev
);
798 getclient(Window w
) {
801 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
806 getcolor(const char *colstr
, int screen
) {
807 Colormap cmap
= DefaultColormap(dpy
, screen
);
810 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
811 eprint("error, cannot allocate color '%s'\n", colstr
);
819 unsigned char *p
= NULL
;
820 unsigned long n
, extra
;
823 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
824 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
825 if(status
!= Success
)
834 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
839 if(!text
|| size
== 0)
842 XGetTextProperty(dpy
, w
, &name
, atom
);
845 if(name
.encoding
== XA_STRING
)
846 strncpy(text
, (char *)name
.value
, size
- 1);
848 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
850 strncpy(text
, *list
, size
- 1);
851 XFreeStringList(list
);
854 text
[size
- 1] = '\0';
860 grabbuttons(Client
*c
, Bool focused
) {
861 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
864 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
865 GrabModeAsync
, GrabModeSync
, None
, None
);
866 XGrabButton(dpy
, Button1
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
867 GrabModeAsync
, GrabModeSync
, None
, None
);
868 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
869 GrabModeAsync
, GrabModeSync
, None
, None
);
870 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
871 GrabModeAsync
, GrabModeSync
, None
, None
);
873 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
874 GrabModeAsync
, GrabModeSync
, None
, None
);
875 XGrabButton(dpy
, Button2
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
876 GrabModeAsync
, GrabModeSync
, None
, None
);
877 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
878 GrabModeAsync
, GrabModeSync
, None
, None
);
879 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
880 GrabModeAsync
, GrabModeSync
, None
, None
);
882 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
883 GrabModeAsync
, GrabModeSync
, None
, None
);
884 XGrabButton(dpy
, Button3
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
885 GrabModeAsync
, GrabModeSync
, None
, None
);
886 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
887 GrabModeAsync
, GrabModeSync
, None
, None
);
888 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
889 GrabModeAsync
, GrabModeSync
, None
, None
);
892 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
893 GrabModeAsync
, GrabModeSync
, None
, None
);
900 XModifierKeymap
*modmap
;
902 /* init modifier map */
903 modmap
= XGetModifierMapping(dpy
);
904 for(i
= 0; i
< 8; i
++)
905 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
906 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
907 numlockmask
= (1 << i
);
909 XFreeModifiermap(modmap
);
911 for(i
= 0; i
< mcount
; i
++) {
912 Monitor
*m
= &monitors
[i
];
913 XUngrabKey(dpy
, AnyKey
, AnyModifier
, m
->root
);
914 for(j
= 0; j
< LENGTH(keys
); j
++) {
915 code
= XKeysymToKeycode(dpy
, keys
[j
].keysym
);
916 XGrabKey(dpy
, code
, keys
[j
].mod
, m
->root
, True
,
917 GrabModeAsync
, GrabModeAsync
);
918 XGrabKey(dpy
, code
, keys
[j
].mod
| LockMask
, m
->root
, True
,
919 GrabModeAsync
, GrabModeAsync
);
920 XGrabKey(dpy
, code
, keys
[j
].mod
| numlockmask
, m
->root
, True
,
921 GrabModeAsync
, GrabModeAsync
);
922 XGrabKey(dpy
, code
, keys
[j
].mod
| numlockmask
| LockMask
, m
->root
, True
,
923 GrabModeAsync
, GrabModeAsync
);
929 idxoftag(const char *tag
) {
932 for(i
= 0; (i
< LENGTH(tags
)) && (tags
[i
] != tag
); i
++);
933 return (i
< LENGTH(tags
)) ? i
: 0;
937 initfont(Monitor
*m
, const char *fontstr
) {
938 char *def
, **missing
;
943 XFreeFontSet(dpy
, m
->dc
.font
.set
);
944 m
->dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
947 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
948 XFreeStringList(missing
);
951 XFontSetExtents
*font_extents
;
952 XFontStruct
**xfonts
;
954 m
->dc
.font
.ascent
= m
->dc
.font
.descent
= 0;
955 font_extents
= XExtentsOfFontSet(m
->dc
.font
.set
);
956 n
= XFontsOfFontSet(m
->dc
.font
.set
, &xfonts
, &font_names
);
957 for(i
= 0, m
->dc
.font
.ascent
= 0, m
->dc
.font
.descent
= 0; i
< n
; i
++) {
958 if(m
->dc
.font
.ascent
< (*xfonts
)->ascent
)
959 m
->dc
.font
.ascent
= (*xfonts
)->ascent
;
960 if(m
->dc
.font
.descent
< (*xfonts
)->descent
)
961 m
->dc
.font
.descent
= (*xfonts
)->descent
;
967 XFreeFont(dpy
, m
->dc
.font
.xfont
);
968 m
->dc
.font
.xfont
= NULL
;
969 if(!(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
970 && !(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
971 eprint("error, cannot load font: '%s'\n", fontstr
);
972 m
->dc
.font
.ascent
= m
->dc
.font
.xfont
->ascent
;
973 m
->dc
.font
.descent
= m
->dc
.font
.xfont
->descent
;
975 m
->dc
.font
.height
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
979 isoccupied(Monitor
*m
, unsigned int t
) {
982 for(c
= clients
; c
; c
= c
->next
)
983 if(c
->tags
[t
] && c
->monitor
== selmonitor
)
989 isprotodel(Client
*c
) {
994 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
995 for(i
= 0; !ret
&& i
< n
; i
++)
996 if(protocols
[i
] == wmatom
[WMDelete
])
1004 isvisible(Client
*c
, Monitor
*m
) {
1007 for(i
= 0; i
< LENGTH(tags
); i
++)
1008 if(c
->tags
[i
] && monitors
[c
->monitor
].seltags
[i
] && c
->monitor
== selmonitor
)
1014 keypress(XEvent
*e
) {
1020 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1021 for(i
= 0; i
< LENGTH(keys
); i
++)
1022 if(keysym
== keys
[i
].keysym
1023 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
1026 keys
[i
].func(keys
[i
].arg
);
1031 killclient(const char *arg
) {
1036 if(isprotodel(sel
)) {
1037 ev
.type
= ClientMessage
;
1038 ev
.xclient
.window
= sel
->win
;
1039 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1040 ev
.xclient
.format
= 32;
1041 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1042 ev
.xclient
.data
.l
[1] = CurrentTime
;
1043 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
1046 XKillClient(dpy
, sel
->win
);
1050 manage(Window w
, XWindowAttributes
*wa
) {
1051 Client
*c
, *t
= NULL
;
1052 Monitor
*m
= &monitors
[selmonitor
];
1057 c
= emallocz(sizeof(Client
));
1058 c
->tags
= emallocz(sizeof initags
);
1063 c
->x
= wa
->x
+ m
->sx
;
1064 c
->y
= wa
->y
+ m
->sy
;
1067 c
->oldborder
= wa
->border_width
;
1069 if (monitorat(c
->x
, c
->y
) != c
->monitor
) {
1074 if(c
->w
== m
->sw
&& c
->h
== m
->sh
) {
1077 c
->border
= wa
->border_width
;
1080 if(c
->x
+ c
->w
+ 2 * c
->border
> m
->wax
+ m
->waw
)
1081 c
->x
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1082 if(c
->y
+ c
->h
+ 2 * c
->border
> m
->way
+ m
->wah
)
1083 c
->y
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1088 c
->border
= BORDERPX
;
1090 wc
.border_width
= c
->border
;
1091 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1092 XSetWindowBorder(dpy
, w
, m
->dc
.norm
[ColBorder
]);
1093 configure(c
); /* propagates border_width, if size doesn't change */
1095 XSelectInput(dpy
, w
, EnterWindowMask
| FocusChangeMask
| PropertyChangeMask
| StructureNotifyMask
);
1096 grabbuttons(c
, False
);
1098 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
1099 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
1101 memcpy(c
->tags
, t
->tags
, sizeof initags
);
1103 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
1106 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1108 XMapWindow(dpy
, c
->win
);
1109 setclientstate(c
, NormalState
);
1114 mappingnotify(XEvent
*e
) {
1115 XMappingEvent
*ev
= &e
->xmapping
;
1117 XRefreshKeyboardMapping(ev
);
1118 if(ev
->request
== MappingKeyboard
)
1123 maprequest(XEvent
*e
) {
1124 static XWindowAttributes wa
;
1125 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1127 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1129 if(wa
.override_redirect
)
1131 if(!getclient(ev
->window
))
1132 manage(ev
->window
, &wa
);
1136 movemouse(Client
*c
) {
1137 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1144 if(XGrabPointer(dpy
, monitors
[selmonitor
].root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1145 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1147 XQueryPointer(dpy
, monitors
[selmonitor
].root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1149 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1152 XUngrabPointer(dpy
, CurrentTime
);
1154 case ConfigureRequest
:
1157 handler
[ev
.type
](&ev
);
1161 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1162 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1163 Monitor
*m
= &monitors
[monitorat(nx
, ny
)];
1164 if(abs(m
->wax
- nx
) < SNAP
)
1166 else if(abs((m
->wax
+ m
->waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
1167 nx
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1168 if(abs(m
->way
- ny
) < SNAP
)
1170 else if(abs((m
->way
+ m
->wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
1171 ny
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1172 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1173 memcpy(c
->tags
, monitors
[monitorat(nx
, ny
)].seltags
, sizeof initags
);
1180 nexttiled(Client
*c
, Monitor
*m
) {
1181 for(; c
&& (c
->isfloating
|| !isvisible(c
, m
)); c
= c
->next
);
1186 propertynotify(XEvent
*e
) {
1189 XPropertyEvent
*ev
= &e
->xproperty
;
1191 if(ev
->state
== PropertyDelete
)
1192 return; /* ignore */
1193 if((c
= getclient(ev
->window
))) {
1196 case XA_WM_TRANSIENT_FOR
:
1197 XGetTransientForHint(dpy
, c
->win
, &trans
);
1198 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1201 case XA_WM_NORMAL_HINTS
:
1205 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1214 quit(const char *arg
) {
1215 readin
= running
= False
;
1219 reapply(const char *arg
) {
1220 static Bool zerotags
[LENGTH(tags
)] = { 0 };
1223 for(c
= clients
; c
; c
= c
->next
) {
1224 memcpy(c
->tags
, zerotags
, sizeof zerotags
);
1231 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1233 Monitor scr
= monitors
[monitorat(x
, y
)];
1234 c
->monitor
= monitorat(x
, y
);
1237 /* set minimum possible */
1243 /* temporarily remove base dimensions */
1247 /* adjust for aspect limits */
1248 if (c
->minay
> 0 && c
->maxay
> 0 && c
->minax
> 0 && c
->maxax
> 0) {
1249 if (w
* c
->maxay
> h
* c
->maxax
)
1250 w
= h
* c
->maxax
/ c
->maxay
;
1251 else if (w
* c
->minay
< h
* c
->minax
)
1252 h
= w
* c
->minay
/ c
->minax
;
1255 /* adjust for increment value */
1261 /* restore base dimensions */
1265 if(c
->minw
> 0 && w
< c
->minw
)
1267 if(c
->minh
> 0 && h
< c
->minh
)
1269 if(c
->maxw
> 0 && w
> c
->maxw
)
1271 if(c
->maxh
> 0 && h
> c
->maxh
)
1274 if(w
<= 0 || h
<= 0)
1276 /* TODO: offscreen appearance fixes */
1279 x = scr.sw - w - 2 * c->border;
1281 y = scr.sh - h - 2 * c->border;
1282 if(x + w + 2 * c->border < scr.sx)
1284 if(y + h + 2 * c->border < scr.sy)
1287 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1290 c
->w
= wc
.width
= w
;
1291 c
->h
= wc
.height
= h
;
1292 wc
.border_width
= c
->border
;
1293 XConfigureWindow(dpy
, c
->win
, CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
, &wc
);
1300 resizemouse(Client
*c
) {
1307 if(XGrabPointer(dpy
, monitors
[selmonitor
].root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1308 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1310 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1312 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1315 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1316 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1317 XUngrabPointer(dpy
, CurrentTime
);
1318 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1320 case ConfigureRequest
:
1323 handler
[ev
.type
](&ev
);
1327 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
1329 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
1331 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1347 if(sel
->isfloating
|| (monitors
[selmonitor
].layout
->arrange
== floating
))
1348 XRaiseWindow(dpy
, sel
->win
);
1349 if(monitors
[selmonitor
].layout
->arrange
!= floating
) {
1350 wc
.stack_mode
= Below
;
1351 wc
.sibling
= monitors
[selmonitor
].barwin
;
1352 if(!sel
->isfloating
) {
1353 XConfigureWindow(dpy
, sel
->win
, CWSibling
| CWStackMode
, &wc
);
1354 wc
.sibling
= sel
->win
;
1356 for(s
= 0; s
< mcount
; s
++) {
1357 for(c
= nexttiled(clients
, &monitors
[s
]); c
; c
= nexttiled(c
->next
, &monitors
[s
])) {
1360 XConfigureWindow(dpy
, c
->win
, CWSibling
| CWStackMode
, &wc
);
1361 wc
.sibling
= c
->win
;
1366 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1372 char buf
[sizeof stext
];
1375 unsigned int len
, offset
;
1378 /* main event loop, also reads status text from stdin */
1380 xfd
= ConnectionNumber(dpy
);
1383 len
= sizeof stext
- 1;
1384 buf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1388 FD_SET(STDIN_FILENO
, &rd
);
1390 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1393 eprint("select failed\n");
1395 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1396 switch((r
= read(STDIN_FILENO
, buf
+ offset
, len
- offset
))) {
1398 strncpy(stext
, strerror(errno
), len
);
1402 strncpy(stext
, "EOF", 4);
1406 for(p
= buf
+ offset
; r
> 0; p
++, r
--, offset
++)
1407 if(*p
== '\n' || *p
== '\0') {
1409 strncpy(stext
, buf
, len
);
1410 p
+= r
- 1; /* p is buf + offset + r - 1 */
1411 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1414 memmove(buf
, p
- r
+ 1, r
);
1421 while(XPending(dpy
)) {
1422 XNextEvent(dpy
, &ev
);
1423 if(handler
[ev
.type
])
1424 (handler
[ev
.type
])(&ev
); /* call handler */
1431 unsigned int i
, j
, num
;
1432 Window
*wins
, d1
, d2
;
1433 XWindowAttributes wa
;
1435 for(i
= 0; i
< mcount
; i
++) {
1436 Monitor
*m
= &monitors
[i
];
1438 if(XQueryTree(dpy
, m
->root
, &d1
, &d2
, &wins
, &num
)) {
1439 for(j
= 0; j
< num
; j
++) {
1440 if(!XGetWindowAttributes(dpy
, wins
[j
], &wa
)
1441 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[j
], &d1
))
1443 if(wa
.map_state
== IsViewable
|| getstate(wins
[j
]) == IconicState
)
1444 manage(wins
[j
], &wa
);
1446 for(j
= 0; j
< num
; j
++) { /* now the transients */
1447 if(!XGetWindowAttributes(dpy
, wins
[j
], &wa
))
1449 if(XGetTransientForHint(dpy
, wins
[j
], &d1
)
1450 && (wa
.map_state
== IsViewable
|| getstate(wins
[j
]) == IconicState
))
1451 manage(wins
[j
], &wa
);
1460 setclientstate(Client
*c
, long state
) {
1461 long data
[] = {state
, None
};
1463 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1464 PropModeReplace
, (unsigned char *)data
, 2);
1468 setlayout(const char *arg
) {
1470 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1474 if(m
->layout
== &layouts
[LENGTH(layouts
)])
1475 m
->layout
= &layouts
[0];
1478 for(i
= 0; i
< LENGTH(layouts
); i
++)
1479 if(!strcmp(arg
, layouts
[i
].symbol
))
1481 if(i
== LENGTH(layouts
))
1483 m
->layout
= &layouts
[i
];
1492 setmwfact(const char *arg
) {
1495 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1499 /* arg handling, manipulate mwfact */
1502 else if(sscanf(arg
, "%lf", &delta
) == 1) {
1503 if(arg
[0] == '+' || arg
[0] == '-')
1509 else if(m
->mwfact
> 0.9)
1517 unsigned int i
, j
, k
;
1519 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
);
1531 wa
.cursor
= cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1532 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1533 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1535 // init screens/monitors first
1536 if (XineramaIsActive(dpy
)) {
1537 info
= XineramaQueryScreens(dpy
, &mcount
);
1540 monitors
= emallocz(mcount
* sizeof(Monitor
));
1542 for(i
= 0; i
< mcount
; i
++) {
1547 m
->root
= RootWindow(dpy
, i
);
1549 if (mcount
!= 1) { // TODO: Xinerama
1550 m
->sx
= info
[i
].x_org
;
1551 m
->sy
= info
[i
].y_org
;
1552 m
->sw
= info
[i
].width
;
1553 m
->sh
= info
[i
].height
;
1558 m
->sw
= DisplayWidth(dpy
, m
->screen
);
1559 m
->sh
= DisplayHeight(dpy
, m
->screen
);
1562 m
->seltags
= emallocz(sizeof initags
);
1563 m
->prevtags
= emallocz(sizeof initags
);
1565 memcpy(m
->seltags
, initags
, sizeof initags
);
1566 memcpy(m
->prevtags
, initags
, sizeof initags
);
1568 /* init appearance */
1569 m
->dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
, i
);
1570 m
->dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
, i
);
1571 m
->dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
, i
);
1572 m
->dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
, i
);
1573 m
->dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
, i
);
1574 m
->dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
, i
);
1576 m
->dc
.h
= bh
= m
->dc
.font
.height
+ 2;
1580 m
->layout
= &layouts
[0];
1581 for(blw
= k
= 0; k
< LENGTH(layouts
); k
++) {
1582 j
= textw(m
, layouts
[k
].symbol
);
1587 // TODO: bpos per screen?
1589 wa
.override_redirect
= 1;
1590 wa
.background_pixmap
= ParentRelative
;
1591 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
1594 m
->barwin
= XCreateWindow(dpy
, m
->root
, m
->sx
, m
->sy
, m
->sw
, bh
, 0,
1595 DefaultDepth(dpy
, m
->screen
), CopyFromParent
, DefaultVisual(dpy
, m
->screen
),
1596 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
1597 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1599 XMapRaised(dpy
, m
->barwin
);
1600 strcpy(stext
, "dwm-"VERSION
);
1601 m
->dc
.drawable
= XCreatePixmap(dpy
, m
->root
, m
->sw
, bh
, DefaultDepth(dpy
, m
->screen
));
1602 m
->dc
.gc
= XCreateGC(dpy
, m
->root
, 0, 0);
1603 XSetLineAttributes(dpy
, m
->dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1605 XSetFont(dpy
, m
->dc
.gc
, m
->dc
.font
.xfont
->fid
);
1607 /* EWMH support per monitor */
1608 XChangeProperty(dpy
, m
->root
, netatom
[NetSupported
], XA_ATOM
, 32,
1609 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1611 /* select for events */
1612 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
1613 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
1614 XChangeWindowAttributes(dpy
, m
->root
, CWEventMask
| CWCursor
, &wa
);
1615 XSelectInput(dpy
, m
->root
, wa
.event_mask
);
1626 spawn(const char *arg
) {
1627 static char *shell
= NULL
;
1629 if(!shell
&& !(shell
= getenv("SHELL")))
1633 /* The double-fork construct avoids zombie processes and keeps the code
1634 * clean from stupid signal handlers. */
1638 close(ConnectionNumber(dpy
));
1640 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1641 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1650 tag(const char *arg
) {
1655 for(i
= 0; i
< LENGTH(tags
); i
++)
1656 sel
->tags
[i
] = (NULL
== arg
);
1657 sel
->tags
[idxoftag(arg
)] = True
;
1662 textnw(Monitor
*m
, const char *text
, unsigned int len
) {
1665 if(m
->dc
.font
.set
) {
1666 XmbTextExtents(m
->dc
.font
.set
, text
, len
, NULL
, &r
);
1669 return XTextWidth(m
->dc
.font
.xfont
, text
, len
);
1673 textw(Monitor
*m
, const char *text
) {
1674 return textnw(m
, text
, strlen(text
)) + m
->dc
.font
.height
;
1679 unsigned int i
, n
, nx
, ny
, nw
, nh
, mw
, th
;
1683 domwfact
= dozoom
= True
;
1685 nw
= 0; /* gcc stupidity requires this */
1687 for (s
= 0; s
< mcount
; s
++) {
1688 Monitor
*m
= &monitors
[s
];
1690 for(n
= 0, c
= nexttiled(clients
, m
); c
; c
= nexttiled(c
->next
, m
))
1693 for(i
= 0, c
= mc
= nexttiled(clients
, m
); c
; c
= nexttiled(c
->next
, m
)) {
1695 mw
= (n
== 1) ? m
->waw
: m
->mwfact
* m
->waw
;
1696 th
= (n
> 1) ? m
->wah
/ (n
- 1) : 0;
1697 if(n
> 1 && th
< bh
)
1699 if(i
== 0) { /* master */
1702 nw
= mw
- 2 * c
->border
;
1703 nh
= m
->wah
- 2 * c
->border
;
1705 else { /* tile window */
1708 nx
+= mc
->w
+ 2 * mc
->border
;
1709 nw
= m
->waw
- mw
- 2 * c
->border
;
1711 if(i
+ 1 == n
) /* remainder */
1712 nh
= (m
->way
+ m
->wah
) - ny
- 2 * c
->border
;
1714 nh
= th
- 2 * c
->border
;
1716 resize(c
, nx
, ny
, nw
, nh
, RESIZEHINTS
);
1717 if((RESIZEHINTS
) && ((c
->h
< bh
) || (c
->h
> nh
) || (c
->w
< bh
) || (c
->w
> nw
)))
1718 /* client doesn't accept size constraints */
1719 resize(c
, nx
, ny
, nw
, nh
, False
);
1720 if(n
> 1 && th
!= m
->wah
)
1721 ny
= c
->y
+ c
->h
+ 2 * c
->border
;
1728 togglebar(const char *arg
) {
1730 bpos
= (BARPOS
== BarOff
) ? BarTop
: BARPOS
;
1733 updatebarpos(&monitors
[monitorat(-1,-1)]);
1738 togglefloating(const char *arg
) {
1741 sel
->isfloating
= !sel
->isfloating
;
1743 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1748 toggletag(const char *arg
) {
1754 sel
->tags
[i
] = !sel
->tags
[i
];
1755 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1756 if(j
== LENGTH(tags
))
1757 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1762 toggleview(const char *arg
) {
1765 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1768 m
->seltags
[i
] = !m
->seltags
[i
];
1769 for(j
= 0; j
< LENGTH(tags
) && !m
->seltags
[j
]; j
++);
1770 if(j
== LENGTH(tags
))
1771 m
->seltags
[i
] = True
; /* at least one tag must be viewed */
1779 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1780 c
->isbanned
= False
;
1784 unmanage(Client
*c
) {
1787 wc
.border_width
= c
->oldborder
;
1788 /* The server grab construct avoids race conditions. */
1790 XSetErrorHandler(xerrordummy
);
1791 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1796 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1797 setclientstate(c
, WithdrawnState
);
1801 XSetErrorHandler(xerror
);
1807 unmapnotify(XEvent
*e
) {
1809 XUnmapEvent
*ev
= &e
->xunmap
;
1811 if((c
= getclient(ev
->window
)))
1816 updatebarpos(Monitor
*s
) {
1827 XMoveWindow(dpy
, s
->barwin
, s
->sx
, s
->sy
);
1831 XMoveWindow(dpy
, s
->barwin
, s
->sx
, s
->sy
+ s
->wah
);
1834 XMoveWindow(dpy
, s
->barwin
, s
->sx
, s
->sy
- bh
);
1838 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1842 updatesizehints(Client
*c
) {
1846 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1848 c
->flags
= size
.flags
;
1849 if(c
->flags
& PBaseSize
) {
1850 c
->basew
= size
.base_width
;
1851 c
->baseh
= size
.base_height
;
1853 else if(c
->flags
& PMinSize
) {
1854 c
->basew
= size
.min_width
;
1855 c
->baseh
= size
.min_height
;
1858 c
->basew
= c
->baseh
= 0;
1859 if(c
->flags
& PResizeInc
) {
1860 c
->incw
= size
.width_inc
;
1861 c
->inch
= size
.height_inc
;
1864 c
->incw
= c
->inch
= 0;
1865 if(c
->flags
& PMaxSize
) {
1866 c
->maxw
= size
.max_width
;
1867 c
->maxh
= size
.max_height
;
1870 c
->maxw
= c
->maxh
= 0;
1871 if(c
->flags
& PMinSize
) {
1872 c
->minw
= size
.min_width
;
1873 c
->minh
= size
.min_height
;
1875 else if(c
->flags
& PBaseSize
) {
1876 c
->minw
= size
.base_width
;
1877 c
->minh
= size
.base_height
;
1880 c
->minw
= c
->minh
= 0;
1881 if(c
->flags
& PAspect
) {
1882 c
->minax
= size
.min_aspect
.x
;
1883 c
->maxax
= size
.max_aspect
.x
;
1884 c
->minay
= size
.min_aspect
.y
;
1885 c
->maxay
= size
.max_aspect
.y
;
1888 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1889 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1890 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1894 updatetitle(Client
*c
) {
1895 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1896 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1899 /* There's no way to check accesses to destroyed windows, thus those cases are
1900 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1901 * default error handler, which may call exit. */
1903 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1904 if(ee
->error_code
== BadWindow
1905 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1906 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1907 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1908 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1909 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1910 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1911 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1913 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1914 ee
->request_code
, ee
->error_code
);
1915 return xerrorxlib(dpy
, ee
); /* may call exit */
1919 xerrordummy(Display
*dsply
, XErrorEvent
*ee
) {
1923 /* Startup Error handler to check if another window manager
1924 * is already running. */
1926 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
1932 view(const char *arg
) {
1935 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1937 memcpy(m
->prevtags
, m
->seltags
, sizeof initags
);
1938 for(i
= 0; i
< LENGTH(tags
); i
++)
1939 m
->seltags
[i
] = (NULL
== arg
);
1940 m
->seltags
[idxoftag(arg
)] = True
;
1945 viewprevtag(const char *arg
) {
1946 static Bool tmp
[LENGTH(tags
)];
1948 Monitor
*m
= &monitors
[monitorat(-1, -1)];
1950 memcpy(tmp
, m
->seltags
, sizeof initags
);
1951 memcpy(m
->seltags
, m
->prevtags
, sizeof initags
);
1952 memcpy(m
->prevtags
, tmp
, sizeof initags
);
1957 zoom(const char *arg
) {
1960 if(!sel
|| !dozoom
|| sel
->isfloating
)
1962 if((c
= sel
) == nexttiled(clients
, &monitors
[c
->monitor
]))
1963 if(!(c
= nexttiled(c
->next
, &monitors
[c
->monitor
])))
1972 monitorat(int x
, int y
) {
1976 if(!XineramaIsActive(dpy
))
1979 if (x
< 0 || y
< 0) {
1982 XQueryPointer(dpy
, DefaultRootWindow(dpy
), &win
, &win
, &x
, &y
, &i
, &i
, &mask
);
1985 for(i
= 0; i
< mcount
; i
++) {
1986 Monitor
*m
= &monitors
[i
];
1987 if((x
< 0 || (x
>= m
->sx
&& x
< m
->sx
+ m
->sw
))
1988 && (y
< 0 || (y
>= m
->sy
&& y
< m
->sy
+ m
->sh
)))
1997 movetomonitor(const char *arg
) {
1999 sel
->monitor
= arg
? atoi(arg
) : (sel
->monitor
+1) % mcount
;
2001 memcpy(sel
->tags
, monitors
[sel
->monitor
].seltags
, sizeof initags
);
2002 resize(sel
, monitors
[sel
->monitor
].wax
, monitors
[sel
->monitor
].way
, sel
->w
, sel
->h
, True
);
2008 selectmonitor(const char *arg
) {
2009 Monitor
*m
= &monitors
[arg
? atoi(arg
) : (monitorat(-1, -1)+1) % mcount
];
2011 XWarpPointer(dpy
, None
, m
->root
, 0, 0, 0, 0, m
->wax
+m
->waw
/2, m
->way
+m
->wah
/2);
2017 main(int argc
, char *argv
[]) {
2018 if(argc
== 2 && !strcmp("-v", argv
[1]))
2019 eprint("dwm-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
2020 "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
2022 eprint("usage: dwm [-v]\n");
2024 setlocale(LC_CTYPE
, "");
2025 if(!(dpy
= XOpenDisplay(0)))
2026 eprint("dwm: cannot open display\n");