Xinqi Bao's Git
a3439bccdbbae324de1392c1b15fd435cee80d68
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
, int monitor
);
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
, int monitor
);
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
*m
);
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
);
211 void movetomonitor(const char *arg
);
212 void selectmonitor(const char *arg
);
217 int (*xerrorxlib
)(Display
*, XErrorEvent
*);
218 unsigned int bh
, bpos
;
219 unsigned int blw
= 0;
220 unsigned int numlockmask
= 0;
221 void (*handler
[LASTEvent
]) (XEvent
*) = {
222 [ButtonPress
] = buttonpress
,
223 [ConfigureRequest
] = configurerequest
,
224 [ConfigureNotify
] = configurenotify
,
225 [DestroyNotify
] = destroynotify
,
226 [EnterNotify
] = enternotify
,
229 [KeyPress
] = keypress
,
230 [MappingNotify
] = mappingnotify
,
231 [MapRequest
] = maprequest
,
232 [PropertyNotify
] = propertynotify
,
233 [UnmapNotify
] = unmapnotify
235 Atom wmatom
[WMLast
], netatom
[NetLast
];
236 Bool isxinerama
= False
;
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()].seltags
, sizeof initags
);
292 if (!matched_monitor
)
293 c
->monitor
= monitorat();
300 for(c
= clients
; c
; c
= c
->next
)
301 if(isvisible(c
, 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
*m
= &monitors
[monitorat()];
341 if(ev
->window
== m
->barwin
) {
343 for(i
= 0; i
< LENGTH(tags
); i
++) {
344 x
+= textw(m
, 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((m
->layout
->arrange
== floating
) || c
->isfloating
)
372 togglefloating(NULL
);
375 else if(ev
->button
== Button2
) {
376 if((floating
!= m
->layout
->arrange
) && c
->isfloating
)
377 togglefloating(NULL
);
381 else if(ev
->button
== Button3
&& !c
->isfixed
) {
382 if((floating
== m
->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
, monitorat()))
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(i
= 0; i
< mcount
; i
++) {
571 Monitor
*m
= &monitors
[i
];
573 for(j
= 0; j
< LENGTH(tags
); j
++) {
574 m
->dc
.w
= textw(m
, tags
[j
]);
576 drawtext(m
, tags
[j
], m
->dc
.sel
);
577 drawsquare(m
, sel
&& sel
->tags
[j
] && sel
->monitor
== selmonitor
, isoccupied(m
, j
), m
->dc
.sel
);
580 drawtext(m
, tags
[j
], m
->dc
.norm
);
581 drawsquare(m
, sel
&& sel
->tags
[j
] && sel
->monitor
== selmonitor
, isoccupied(m
, j
), 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
) {
682 XCrossingEvent
*ev
= &e
->xcrossing
;
684 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
);
686 if((c
= getclient(ev
->window
)))
689 selmonitor
= monitorat();
690 fprintf(stderr
, "updating selmonitor %d\n", selmonitor
);
696 eprint(const char *errstr
, ...) {
699 va_start(ap
, errstr
);
700 vfprintf(stderr
, errstr
, ap
);
707 XExposeEvent
*ev
= &e
->xexpose
;
710 if(ev
->window
== monitors
[selmonitor
].barwin
)
716 floating(void) { /* default floating layout */
719 domwfact
= dozoom
= False
;
720 for(c
= clients
; c
; c
= c
->next
)
721 if(isvisible(c
, selmonitor
))
722 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
730 selmonitor
= c
->monitor
;
731 m
= &monitors
[selmonitor
];
732 if(!c
|| (c
&& !isvisible(c
, selmonitor
)))
733 for(c
= stack
; c
&& !isvisible(c
, c
->monitor
); 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
) {
769 for(c
= sel
->next
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
771 for(c
= clients
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
779 focusprev(const char *arg
) {
784 for(c
= sel
->prev
; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
786 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
787 for(; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
796 getclient(Window w
) {
799 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
804 getcolor(const char *colstr
, int screen
) {
805 Colormap cmap
= DefaultColormap(dpy
, screen
);
808 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
809 eprint("error, cannot allocate color '%s'\n", colstr
);
817 unsigned char *p
= NULL
;
818 unsigned long n
, extra
;
821 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
822 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
823 if(status
!= Success
)
832 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
837 if(!text
|| size
== 0)
840 XGetTextProperty(dpy
, w
, &name
, atom
);
843 if(name
.encoding
== XA_STRING
)
844 strncpy(text
, (char *)name
.value
, size
- 1);
846 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
848 strncpy(text
, *list
, size
- 1);
849 XFreeStringList(list
);
852 text
[size
- 1] = '\0';
858 grabbuttons(Client
*c
, Bool focused
) {
859 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
862 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
863 GrabModeAsync
, GrabModeSync
, None
, None
);
864 XGrabButton(dpy
, Button1
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
865 GrabModeAsync
, GrabModeSync
, None
, None
);
866 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
867 GrabModeAsync
, GrabModeSync
, None
, None
);
868 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
869 GrabModeAsync
, GrabModeSync
, None
, None
);
871 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
872 GrabModeAsync
, GrabModeSync
, None
, None
);
873 XGrabButton(dpy
, Button2
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
874 GrabModeAsync
, GrabModeSync
, None
, None
);
875 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
876 GrabModeAsync
, GrabModeSync
, None
, None
);
877 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
878 GrabModeAsync
, GrabModeSync
, None
, None
);
880 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
881 GrabModeAsync
, GrabModeSync
, None
, None
);
882 XGrabButton(dpy
, Button3
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
883 GrabModeAsync
, GrabModeSync
, None
, None
);
884 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
885 GrabModeAsync
, GrabModeSync
, None
, None
);
886 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
887 GrabModeAsync
, GrabModeSync
, None
, None
);
890 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
891 GrabModeAsync
, GrabModeSync
, None
, None
);
898 XModifierKeymap
*modmap
;
900 /* init modifier map */
901 modmap
= XGetModifierMapping(dpy
);
902 for(i
= 0; i
< 8; i
++)
903 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
904 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
905 numlockmask
= (1 << i
);
907 XFreeModifiermap(modmap
);
909 for(i
= 0; i
< mcount
; i
++) {
910 Monitor
*m
= &monitors
[i
];
911 XUngrabKey(dpy
, AnyKey
, AnyModifier
, m
->root
);
912 for(j
= 0; j
< LENGTH(keys
); j
++) {
913 code
= XKeysymToKeycode(dpy
, keys
[j
].keysym
);
914 XGrabKey(dpy
, code
, keys
[j
].mod
, m
->root
, True
,
915 GrabModeAsync
, GrabModeAsync
);
916 XGrabKey(dpy
, code
, keys
[j
].mod
| LockMask
, m
->root
, True
,
917 GrabModeAsync
, GrabModeAsync
);
918 XGrabKey(dpy
, code
, keys
[j
].mod
| numlockmask
, m
->root
, True
,
919 GrabModeAsync
, GrabModeAsync
);
920 XGrabKey(dpy
, code
, keys
[j
].mod
| numlockmask
| LockMask
, m
->root
, True
,
921 GrabModeAsync
, GrabModeAsync
);
927 idxoftag(const char *tag
) {
930 for(i
= 0; (i
< LENGTH(tags
)) && (tags
[i
] != tag
); i
++);
931 return (i
< LENGTH(tags
)) ? i
: 0;
935 initfont(Monitor
*m
, const char *fontstr
) {
936 char *def
, **missing
;
941 XFreeFontSet(dpy
, m
->dc
.font
.set
);
942 m
->dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
945 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
946 XFreeStringList(missing
);
949 XFontSetExtents
*font_extents
;
950 XFontStruct
**xfonts
;
952 m
->dc
.font
.ascent
= m
->dc
.font
.descent
= 0;
953 font_extents
= XExtentsOfFontSet(m
->dc
.font
.set
);
954 n
= XFontsOfFontSet(m
->dc
.font
.set
, &xfonts
, &font_names
);
955 for(i
= 0, m
->dc
.font
.ascent
= 0, m
->dc
.font
.descent
= 0; i
< n
; i
++) {
956 if(m
->dc
.font
.ascent
< (*xfonts
)->ascent
)
957 m
->dc
.font
.ascent
= (*xfonts
)->ascent
;
958 if(m
->dc
.font
.descent
< (*xfonts
)->descent
)
959 m
->dc
.font
.descent
= (*xfonts
)->descent
;
965 XFreeFont(dpy
, m
->dc
.font
.xfont
);
966 m
->dc
.font
.xfont
= NULL
;
967 if(!(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
968 && !(m
->dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
969 eprint("error, cannot load font: '%s'\n", fontstr
);
970 m
->dc
.font
.ascent
= m
->dc
.font
.xfont
->ascent
;
971 m
->dc
.font
.descent
= m
->dc
.font
.xfont
->descent
;
973 m
->dc
.font
.height
= m
->dc
.font
.ascent
+ m
->dc
.font
.descent
;
977 isoccupied(Monitor
*m
, unsigned int t
) {
980 for(c
= clients
; c
; c
= c
->next
)
981 if(c
->tags
[t
] && c
->monitor
== selmonitor
)
987 isprotodel(Client
*c
) {
992 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
993 for(i
= 0; !ret
&& i
< n
; i
++)
994 if(protocols
[i
] == wmatom
[WMDelete
])
1002 isvisible(Client
*c
, int monitor
) {
1005 for(i
= 0; i
< LENGTH(tags
); i
++)
1006 if(c
->tags
[i
] && monitors
[c
->monitor
].seltags
[i
] && c
->monitor
== monitor
)
1012 keypress(XEvent
*e
) {
1018 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1019 for(i
= 0; i
< LENGTH(keys
); i
++)
1020 if(keysym
== keys
[i
].keysym
1021 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
1024 keys
[i
].func(keys
[i
].arg
);
1029 killclient(const char *arg
) {
1034 if(isprotodel(sel
)) {
1035 ev
.type
= ClientMessage
;
1036 ev
.xclient
.window
= sel
->win
;
1037 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1038 ev
.xclient
.format
= 32;
1039 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1040 ev
.xclient
.data
.l
[1] = CurrentTime
;
1041 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
1044 XKillClient(dpy
, sel
->win
);
1048 manage(Window w
, XWindowAttributes
*wa
) {
1049 Client
*c
, *t
= NULL
;
1055 c
= emallocz(sizeof(Client
));
1056 c
->tags
= emallocz(sizeof initags
);
1061 m
= &monitors
[c
->monitor
];
1063 c
->x
= wa
->x
+ m
->sx
;
1064 c
->y
= wa
->y
+ m
->sy
;
1067 c
->oldborder
= wa
->border_width
;
1069 if(c
->w
== m
->sw
&& c
->h
== m
->sh
) {
1072 c
->border
= wa
->border_width
;
1075 if(c
->x
+ c
->w
+ 2 * c
->border
> m
->wax
+ m
->waw
)
1076 c
->x
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1077 if(c
->y
+ c
->h
+ 2 * c
->border
> m
->way
+ m
->wah
)
1078 c
->y
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1083 c
->border
= BORDERPX
;
1085 wc
.border_width
= c
->border
;
1086 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1087 XSetWindowBorder(dpy
, w
, m
->dc
.norm
[ColBorder
]);
1088 configure(c
); /* propagates border_width, if size doesn't change */
1090 XSelectInput(dpy
, w
, EnterWindowMask
| FocusChangeMask
| PropertyChangeMask
| StructureNotifyMask
);
1091 grabbuttons(c
, False
);
1093 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
1094 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
1096 memcpy(c
->tags
, t
->tags
, sizeof initags
);
1098 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
1101 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1103 XMapWindow(dpy
, c
->win
);
1104 setclientstate(c
, NormalState
);
1109 mappingnotify(XEvent
*e
) {
1110 XMappingEvent
*ev
= &e
->xmapping
;
1112 XRefreshKeyboardMapping(ev
);
1113 if(ev
->request
== MappingKeyboard
)
1118 maprequest(XEvent
*e
) {
1119 static XWindowAttributes wa
;
1120 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1122 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1124 if(wa
.override_redirect
)
1126 if(!getclient(ev
->window
))
1127 manage(ev
->window
, &wa
);
1136 XQueryPointer(dpy
, monitors
[selmonitor
].root
, &win
, &win
, &x
, &y
, &i
, &i
, &mask
);
1137 for(i
= 0; i
< mcount
; i
++) {
1138 fprintf(stderr
, "checking monitor[%d]: %d %d %d %d\n", i
, monitors
[i
].sx
, monitors
[i
].sy
, monitors
[i
].sw
, monitors
[i
].sh
);
1139 if((x
>= monitors
[i
].sx
&& x
< monitors
[i
].sx
+ monitors
[i
].sw
)
1140 && (y
>= monitors
[i
].sy
&& y
< monitors
[i
].sy
+ monitors
[i
].sh
)) {
1141 fprintf(stderr
, "%d,%d -> %d\n", x
, y
, i
);
1145 fprintf(stderr
, "?,? -> 0\n");
1150 movemouse(Client
*c
) {
1151 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1158 if(XGrabPointer(dpy
, monitors
[selmonitor
].root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1159 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1161 XQueryPointer(dpy
, monitors
[selmonitor
].root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1163 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1166 XUngrabPointer(dpy
, CurrentTime
);
1168 case ConfigureRequest
:
1171 handler
[ev
.type
](&ev
);
1175 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1176 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1177 Monitor
*m
= &monitors
[monitorat()];
1178 if(abs(m
->wax
- nx
) < SNAP
)
1180 else if(abs((m
->wax
+ m
->waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
1181 nx
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1182 if(abs(m
->way
- ny
) < SNAP
)
1184 else if(abs((m
->way
+ m
->wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
1185 ny
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1186 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1187 memcpy(c
->tags
, monitors
[monitorat()].seltags
, sizeof initags
);
1194 nexttiled(Client
*c
, int monitor
) {
1195 for(; c
&& (c
->isfloating
|| !isvisible(c
, monitor
)); c
= c
->next
);
1200 propertynotify(XEvent
*e
) {
1203 XPropertyEvent
*ev
= &e
->xproperty
;
1205 if(ev
->state
== PropertyDelete
)
1206 return; /* ignore */
1207 if((c
= getclient(ev
->window
))) {
1210 case XA_WM_TRANSIENT_FOR
:
1211 XGetTransientForHint(dpy
, c
->win
, &trans
);
1212 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1215 case XA_WM_NORMAL_HINTS
:
1219 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1228 quit(const char *arg
) {
1229 readin
= running
= False
;
1233 reapply(const char *arg
) {
1234 static Bool zerotags
[LENGTH(tags
)] = { 0 };
1237 for(c
= clients
; c
; c
= c
->next
) {
1238 memcpy(c
->tags
, zerotags
, sizeof zerotags
);
1245 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1247 //Monitor scr = monitors[monitorat()];
1248 // c->monitor = monitorat();
1251 /* set minimum possible */
1257 /* temporarily remove base dimensions */
1261 /* adjust for aspect limits */
1262 if (c
->minay
> 0 && c
->maxay
> 0 && c
->minax
> 0 && c
->maxax
> 0) {
1263 if (w
* c
->maxay
> h
* c
->maxax
)
1264 w
= h
* c
->maxax
/ c
->maxay
;
1265 else if (w
* c
->minay
< h
* c
->minax
)
1266 h
= w
* c
->minay
/ c
->minax
;
1269 /* adjust for increment value */
1275 /* restore base dimensions */
1279 if(c
->minw
> 0 && w
< c
->minw
)
1281 if(c
->minh
> 0 && h
< c
->minh
)
1283 if(c
->maxw
> 0 && w
> c
->maxw
)
1285 if(c
->maxh
> 0 && h
> c
->maxh
)
1288 if(w
<= 0 || h
<= 0)
1290 /* TODO: offscreen appearance fixes */
1293 x = scr.sw - w - 2 * c->border;
1295 y = scr.sh - h - 2 * c->border;
1296 if(x + w + 2 * c->border < scr.sx)
1298 if(y + h + 2 * c->border < scr.sy)
1301 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1304 c
->w
= wc
.width
= w
;
1305 c
->h
= wc
.height
= h
;
1306 wc
.border_width
= c
->border
;
1307 XConfigureWindow(dpy
, c
->win
, CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
, &wc
);
1314 resizemouse(Client
*c
) {
1321 if(XGrabPointer(dpy
, monitors
[selmonitor
].root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1322 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1324 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1326 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1329 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1330 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1331 XUngrabPointer(dpy
, CurrentTime
);
1332 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1334 case ConfigureRequest
:
1337 handler
[ev
.type
](&ev
);
1341 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
1343 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
1345 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1361 if(sel
->isfloating
|| (monitors
[selmonitor
].layout
->arrange
== floating
))
1362 XRaiseWindow(dpy
, sel
->win
);
1363 if(monitors
[selmonitor
].layout
->arrange
!= floating
) {
1364 wc
.stack_mode
= Below
;
1365 wc
.sibling
= monitors
[selmonitor
].barwin
;
1366 if(!sel
->isfloating
) {
1367 XConfigureWindow(dpy
, sel
->win
, CWSibling
| CWStackMode
, &wc
);
1368 wc
.sibling
= sel
->win
;
1370 for(i
= 0; i
< mcount
; i
++) {
1371 for(c
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
)) {
1374 XConfigureWindow(dpy
, c
->win
, CWSibling
| CWStackMode
, &wc
);
1375 wc
.sibling
= c
->win
;
1380 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1386 char buf
[sizeof stext
];
1389 unsigned int len
, offset
;
1392 /* main event loop, also reads status text from stdin */
1394 xfd
= ConnectionNumber(dpy
);
1397 len
= sizeof stext
- 1;
1398 buf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1402 FD_SET(STDIN_FILENO
, &rd
);
1404 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1407 eprint("select failed\n");
1409 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1410 switch((r
= read(STDIN_FILENO
, buf
+ offset
, len
- offset
))) {
1412 strncpy(stext
, strerror(errno
), len
);
1416 strncpy(stext
, "EOF", 4);
1420 for(p
= buf
+ offset
; r
> 0; p
++, r
--, offset
++)
1421 if(*p
== '\n' || *p
== '\0') {
1423 strncpy(stext
, buf
, len
);
1424 p
+= r
- 1; /* p is buf + offset + r - 1 */
1425 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1428 memmove(buf
, p
- r
+ 1, r
);
1435 while(XPending(dpy
)) {
1436 XNextEvent(dpy
, &ev
);
1437 if(handler
[ev
.type
])
1438 (handler
[ev
.type
])(&ev
); /* call handler */
1445 unsigned int i
, j
, num
;
1446 Window
*wins
, d1
, d2
;
1447 XWindowAttributes wa
;
1449 for(i
= 0; i
< mcount
; i
++) {
1450 Monitor
*m
= &monitors
[i
];
1452 if(XQueryTree(dpy
, m
->root
, &d1
, &d2
, &wins
, &num
)) {
1453 for(j
= 0; j
< num
; j
++) {
1454 if(!XGetWindowAttributes(dpy
, wins
[j
], &wa
)
1455 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[j
], &d1
))
1457 if(wa
.map_state
== IsViewable
|| getstate(wins
[j
]) == IconicState
)
1458 manage(wins
[j
], &wa
);
1460 for(j
= 0; j
< num
; j
++) { /* now the transients */
1461 if(!XGetWindowAttributes(dpy
, wins
[j
], &wa
))
1463 if(XGetTransientForHint(dpy
, wins
[j
], &d1
)
1464 && (wa
.map_state
== IsViewable
|| getstate(wins
[j
]) == IconicState
))
1465 manage(wins
[j
], &wa
);
1474 setclientstate(Client
*c
, long state
) {
1475 long data
[] = {state
, None
};
1477 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1478 PropModeReplace
, (unsigned char *)data
, 2);
1482 setlayout(const char *arg
) {
1484 Monitor
*m
= &monitors
[monitorat()];
1488 if(m
->layout
== &layouts
[LENGTH(layouts
)])
1489 m
->layout
= &layouts
[0];
1492 for(i
= 0; i
< LENGTH(layouts
); i
++)
1493 if(!strcmp(arg
, layouts
[i
].symbol
))
1495 if(i
== LENGTH(layouts
))
1497 m
->layout
= &layouts
[i
];
1506 setmwfact(const char *arg
) {
1509 Monitor
*m
= &monitors
[monitorat()];
1513 /* arg handling, manipulate mwfact */
1516 else if(sscanf(arg
, "%lf", &delta
) == 1) {
1517 if(arg
[0] == '+' || arg
[0] == '-')
1523 else if(m
->mwfact
> 0.9)
1531 unsigned int i
, j
, k
;
1533 XSetWindowAttributes wa
;
1534 XineramaScreenInfo
*info
= NULL
;
1537 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1538 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1539 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1540 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1541 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1542 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1545 wa
.cursor
= cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1546 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1547 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1549 // init screens/monitors first
1551 if((isxinerama
= XineramaIsActive(dpy
)))
1552 info
= XineramaQueryScreens(dpy
, &mcount
);
1553 monitors
= emallocz(mcount
* sizeof(Monitor
));
1555 for(i
= 0; i
< mcount
; i
++) {
1559 m
->screen
= isxinerama
? 0 : i
;
1560 m
->root
= RootWindow(dpy
, m
->screen
);
1562 if (mcount
!= 1 && isxinerama
) {
1563 m
->sx
= info
[i
].x_org
;
1564 m
->sy
= info
[i
].y_org
;
1565 m
->sw
= info
[i
].width
;
1566 m
->sh
= info
[i
].height
;
1567 fprintf(stderr
, "monitor[%d]: %d,%d,%d,%d\n", i
, m
->sx
, m
->sy
, m
->sw
, m
->sh
);
1572 m
->sw
= DisplayWidth(dpy
, m
->screen
);
1573 m
->sh
= DisplayHeight(dpy
, m
->screen
);
1576 m
->seltags
= emallocz(sizeof initags
);
1577 m
->prevtags
= emallocz(sizeof initags
);
1579 memcpy(m
->seltags
, initags
, sizeof initags
);
1580 memcpy(m
->prevtags
, initags
, sizeof initags
);
1582 /* init appearance */
1583 m
->dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
, m
->screen
);
1584 m
->dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
, m
->screen
);
1585 m
->dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
, m
->screen
);
1586 m
->dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
, m
->screen
);
1587 m
->dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
, m
->screen
);
1588 m
->dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
, m
->screen
);
1590 m
->dc
.h
= bh
= m
->dc
.font
.height
+ 2;
1594 m
->layout
= &layouts
[0];
1595 for(blw
= k
= 0; k
< LENGTH(layouts
); k
++) {
1596 j
= textw(m
, layouts
[k
].symbol
);
1601 // TODO: bpos per screen?
1603 wa
.override_redirect
= 1;
1604 wa
.background_pixmap
= ParentRelative
;
1605 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
1608 m
->barwin
= XCreateWindow(dpy
, m
->root
, m
->sx
, m
->sy
, m
->sw
, bh
, 0,
1609 DefaultDepth(dpy
, m
->screen
), CopyFromParent
, DefaultVisual(dpy
, m
->screen
),
1610 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
1611 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1613 XMapRaised(dpy
, m
->barwin
);
1614 strcpy(stext
, "dwm-"VERSION
);
1615 m
->dc
.drawable
= XCreatePixmap(dpy
, m
->root
, m
->sw
, bh
, DefaultDepth(dpy
, m
->screen
));
1616 m
->dc
.gc
= XCreateGC(dpy
, m
->root
, 0, 0);
1617 XSetLineAttributes(dpy
, m
->dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1619 XSetFont(dpy
, m
->dc
.gc
, m
->dc
.font
.xfont
->fid
);
1621 /* EWMH support per monitor */
1622 XChangeProperty(dpy
, m
->root
, netatom
[NetSupported
], XA_ATOM
, 32,
1623 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1625 /* select for events */
1626 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
1627 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
1628 XChangeWindowAttributes(dpy
, m
->root
, CWEventMask
| CWCursor
, &wa
);
1629 XSelectInput(dpy
, m
->root
, wa
.event_mask
);
1640 selmonitor
= monitorat();
1641 fprintf(stderr
, "selmonitor == %d\n", selmonitor
);
1645 spawn(const char *arg
) {
1646 static char *shell
= NULL
;
1648 if(!shell
&& !(shell
= getenv("SHELL")))
1652 /* The double-fork construct avoids zombie processes and keeps the code
1653 * clean from stupid signal handlers. */
1657 close(ConnectionNumber(dpy
));
1659 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1660 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1669 tag(const char *arg
) {
1674 for(i
= 0; i
< LENGTH(tags
); i
++)
1675 sel
->tags
[i
] = (NULL
== arg
);
1676 sel
->tags
[idxoftag(arg
)] = True
;
1681 textnw(Monitor
*m
, const char *text
, unsigned int len
) {
1684 if(m
->dc
.font
.set
) {
1685 XmbTextExtents(m
->dc
.font
.set
, text
, len
, NULL
, &r
);
1688 return XTextWidth(m
->dc
.font
.xfont
, text
, len
);
1692 textw(Monitor
*m
, const char *text
) {
1693 return textnw(m
, text
, strlen(text
)) + m
->dc
.font
.height
;
1698 unsigned int i
, j
, n
, nx
, ny
, nw
, nh
, mw
, th
;
1701 domwfact
= dozoom
= True
;
1703 nx
= ny
= nw
= 0; /* gcc stupidity requires this */
1705 for (i
= 0; i
< mcount
; i
++) {
1706 Monitor
*m
= &monitors
[i
];
1708 for(n
= 0, c
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
))
1711 for(j
= 0, c
= mc
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
)) {
1713 mw
= (n
== 1) ? m
->waw
: m
->mwfact
* m
->waw
;
1714 th
= (n
> 1) ? m
->wah
/ (n
- 1) : 0;
1715 if(n
> 1 && th
< bh
)
1717 if(j
== 0) { /* master */
1720 nw
= mw
- 2 * c
->border
;
1721 nh
= m
->wah
- 2 * c
->border
;
1723 else { /* tile window */
1726 nx
+= mc
->w
+ 2 * mc
->border
;
1727 nw
= m
->waw
- mw
- 2 * c
->border
;
1729 if(j
+ 1 == n
) /* remainder */
1730 nh
= (m
->way
+ m
->wah
) - ny
- 2 * c
->border
;
1732 nh
= th
- 2 * c
->border
;
1734 fprintf(stderr
, "tile(%d, %d, %d, %d)\n", nx
, ny
, nw
, nh
);
1735 resize(c
, nx
, ny
, nw
, nh
, RESIZEHINTS
);
1736 if((RESIZEHINTS
) && ((c
->h
< bh
) || (c
->h
> nh
) || (c
->w
< bh
) || (c
->w
> nw
)))
1737 /* client doesn't accept size constraints */
1738 resize(c
, nx
, ny
, nw
, nh
, False
);
1739 if(n
> 1 && th
!= m
->wah
)
1740 ny
= c
->y
+ c
->h
+ 2 * c
->border
;
1745 fprintf(stderr
, "done\n");
1748 togglebar(const char *arg
) {
1750 bpos
= (BARPOS
== BarOff
) ? BarTop
: BARPOS
;
1753 updatebarpos(&monitors
[monitorat()]);
1758 togglefloating(const char *arg
) {
1761 sel
->isfloating
= !sel
->isfloating
;
1763 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1768 toggletag(const char *arg
) {
1774 sel
->tags
[i
] = !sel
->tags
[i
];
1775 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1776 if(j
== LENGTH(tags
))
1777 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1782 toggleview(const char *arg
) {
1785 Monitor
*m
= &monitors
[monitorat()];
1788 m
->seltags
[i
] = !m
->seltags
[i
];
1789 for(j
= 0; j
< LENGTH(tags
) && !m
->seltags
[j
]; j
++);
1790 if(j
== LENGTH(tags
))
1791 m
->seltags
[i
] = True
; /* at least one tag must be viewed */
1799 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1800 c
->isbanned
= False
;
1804 unmanage(Client
*c
) {
1807 wc
.border_width
= c
->oldborder
;
1808 /* The server grab construct avoids race conditions. */
1810 XSetErrorHandler(xerrordummy
);
1811 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1816 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1817 setclientstate(c
, WithdrawnState
);
1821 XSetErrorHandler(xerror
);
1827 unmapnotify(XEvent
*e
) {
1829 XUnmapEvent
*ev
= &e
->xunmap
;
1831 if((c
= getclient(ev
->window
)))
1836 updatebarpos(Monitor
*m
) {
1847 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
);
1851 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
+ m
->wah
);
1854 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
- bh
);
1858 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1862 updatesizehints(Client
*c
) {
1866 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1868 c
->flags
= size
.flags
;
1869 if(c
->flags
& PBaseSize
) {
1870 c
->basew
= size
.base_width
;
1871 c
->baseh
= size
.base_height
;
1873 else if(c
->flags
& PMinSize
) {
1874 c
->basew
= size
.min_width
;
1875 c
->baseh
= size
.min_height
;
1878 c
->basew
= c
->baseh
= 0;
1879 if(c
->flags
& PResizeInc
) {
1880 c
->incw
= size
.width_inc
;
1881 c
->inch
= size
.height_inc
;
1884 c
->incw
= c
->inch
= 0;
1885 if(c
->flags
& PMaxSize
) {
1886 c
->maxw
= size
.max_width
;
1887 c
->maxh
= size
.max_height
;
1890 c
->maxw
= c
->maxh
= 0;
1891 if(c
->flags
& PMinSize
) {
1892 c
->minw
= size
.min_width
;
1893 c
->minh
= size
.min_height
;
1895 else if(c
->flags
& PBaseSize
) {
1896 c
->minw
= size
.base_width
;
1897 c
->minh
= size
.base_height
;
1900 c
->minw
= c
->minh
= 0;
1901 if(c
->flags
& PAspect
) {
1902 c
->minax
= size
.min_aspect
.x
;
1903 c
->maxax
= size
.max_aspect
.x
;
1904 c
->minay
= size
.min_aspect
.y
;
1905 c
->maxay
= size
.max_aspect
.y
;
1908 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1909 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1910 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1914 updatetitle(Client
*c
) {
1915 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1916 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1919 /* There's no way to check accesses to destroyed windows, thus those cases are
1920 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1921 * default error handler, which may call exit. */
1923 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1924 if(ee
->error_code
== BadWindow
1925 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1926 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1927 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1928 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1929 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1930 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1931 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1933 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1934 ee
->request_code
, ee
->error_code
);
1935 return xerrorxlib(dpy
, ee
); /* may call exit */
1939 xerrordummy(Display
*dsply
, XErrorEvent
*ee
) {
1943 /* Startup Error handler to check if another window manager
1944 * is already running. */
1946 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
1952 view(const char *arg
) {
1955 Monitor
*m
= &monitors
[monitorat()];
1957 memcpy(m
->prevtags
, m
->seltags
, sizeof initags
);
1958 for(i
= 0; i
< LENGTH(tags
); i
++)
1959 m
->seltags
[i
] = (NULL
== arg
);
1960 m
->seltags
[idxoftag(arg
)] = True
;
1965 viewprevtag(const char *arg
) {
1966 static Bool tmp
[LENGTH(tags
)];
1968 Monitor
*m
= &monitors
[monitorat()];
1970 memcpy(tmp
, m
->seltags
, sizeof initags
);
1971 memcpy(m
->seltags
, m
->prevtags
, sizeof initags
);
1972 memcpy(m
->prevtags
, tmp
, sizeof initags
);
1977 zoom(const char *arg
) {
1980 if(!sel
|| !dozoom
|| sel
->isfloating
)
1982 if((c
= sel
) == nexttiled(clients
, c
->monitor
))
1983 if(!(c
= nexttiled(c
->next
, c
->monitor
)))
1992 movetomonitor(const char *arg
) {
1994 sel
->monitor
= arg
? atoi(arg
) : (sel
->monitor
+1) % mcount
;
1996 memcpy(sel
->tags
, monitors
[sel
->monitor
].seltags
, sizeof initags
);
1997 resize(sel
, monitors
[sel
->monitor
].wax
, monitors
[sel
->monitor
].way
, sel
->w
, sel
->h
, True
);
2003 selectmonitor(const char *arg
) {
2004 Monitor
*m
= &monitors
[arg
? atoi(arg
) : (monitorat()+1) % mcount
];
2006 XWarpPointer(dpy
, None
, m
->root
, 0, 0, 0, 0, m
->wax
+m
->waw
/2, m
->way
+m
->wah
/2);
2012 main(int argc
, char *argv
[]) {
2013 if(argc
== 2 && !strcmp("-v", argv
[1]))
2014 eprint("dwm-"VERSION
", © 2006-2007 Anselm R. Garbe, Sander van Dijk, "
2015 "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
2017 eprint("usage: dwm [-v]\n");
2019 setlocale(LC_CTYPE
, "");
2020 if(!(dpy
= XOpenDisplay(0)))
2021 eprint("dwm: cannot open display\n");