Xinqi Bao's Git
ff9e66ab5983bd3704b9a84f683b91470ed8ee1b
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
, isurgent
;
82 unsigned long norm
[ColLast
];
83 unsigned long sel
[ColLast
];
93 } DC
; /* draw context */
98 void (*func
)(const char *arg
);
102 typedef struct Monitor Monitor
;
105 void (*arrange
)(Monitor
*);
122 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
, Bool invert
, unsigned long col
[ColLast
]);
149 void drawtext(Monitor
*, const char *text
, unsigned long col
[ColLast
], Bool invert
);
150 void *emallocz(unsigned int size
);
151 void enternotify(XEvent
*e
);
152 void eprint(const char *errstr
, ...);
153 void expose(XEvent
*e
);
154 void floating(Monitor
*m
); /* default floating layout */
155 void focus(Client
*c
);
156 void focusin(XEvent
*e
);
157 void focusnext(const char *arg
);
158 void focusprev(const char *arg
);
159 Client
*getclient(Window w
);
160 unsigned long getcolor(const char *colstr
);
161 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(const char *fontstr
);
167 Bool
isoccupied(unsigned int monitor
, unsigned int t
);
168 Bool
isprotodel(Client
*c
);
169 Bool
isurgent(unsigned int monitor
, unsigned int t
);
170 Bool
isvisible(Client
*c
, int monitor
);
171 void keypress(XEvent
*e
);
172 void killclient(const char *arg
);
173 void manage(Window w
, XWindowAttributes
*wa
);
174 void mappingnotify(XEvent
*e
);
175 void maprequest(XEvent
*e
);
176 void movemouse(Client
*c
);
177 Client
*nexttiled(Client
*c
, int monitor
);
178 void propertynotify(XEvent
*e
);
179 void quit(const char *arg
);
180 void reapply(const char *arg
);
181 void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
);
182 void resizemouse(Client
*c
);
186 void setclientstate(Client
*c
, long state
);
187 void setlayout(const char *arg
);
188 void setmwfact(const char *arg
);
190 void spawn(const char *arg
);
191 void tag(const char *arg
);
192 unsigned int textnw(const char *text
, unsigned int len
);
193 unsigned int textw(const char *text
);
194 void tile(Monitor
*m
);
195 void togglebar(const char *arg
);
196 void togglefloating(const char *arg
);
197 void toggletag(const char *arg
);
198 void toggleview(const char *arg
);
199 void unban(Client
*c
);
200 void unmanage(Client
*c
);
201 void unmapnotify(XEvent
*e
);
202 void updatebarpos(Monitor
*m
);
203 void updatesizehints(Client
*c
);
204 void updatetitle(Client
*c
);
205 void updatewmhints(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
);
213 void movetomonitor(const char *arg
);
214 void selectmonitor(const char *arg
);
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 [MappingNotify
] = mappingnotify
,
235 [MapRequest
] = maprequest
,
236 [PropertyNotify
] = propertynotify
,
237 [UnmapNotify
] = unmapnotify
239 Atom wmatom
[WMLast
], netatom
[NetLast
];
240 Bool isxinerama
= False
;
241 Bool domwfact
= True
;
243 Bool otherwm
, readin
;
245 Client
*clients
= NULL
;
247 Client
*stack
= NULL
;
248 Cursor cursor
[CurLast
];
255 /* configuration, allows nested code to access above variables */
258 //Bool prevtags[LENGTH(tags)];
260 /* function implementations */
262 applyrules(Client
*c
) {
263 static char buf
[512];
266 Bool matched_tag
= False
;
267 Bool matched_monitor
= False
;
268 XClassHint ch
= { 0 };
271 XGetClassHint(dpy
, c
->win
, &ch
);
272 snprintf(buf
, sizeof buf
, "%s:%s:%s",
273 ch
.res_class
? ch
.res_class
: "",
274 ch
.res_name
? ch
.res_name
: "", c
->name
);
275 for(i
= 0; i
< LENGTH(rules
); i
++)
276 if(regs
[i
].propregex
&& !regexec(regs
[i
].propregex
, buf
, 1, &tmp
, 0)) {
277 if (rules
[i
].monitor
>= 0 && rules
[i
].monitor
< mcount
) {
278 matched_monitor
= True
;
279 c
->monitor
= rules
[i
].monitor
;
282 c
->isfloating
= rules
[i
].isfloating
;
283 for(j
= 0; regs
[i
].tagregex
&& j
< LENGTH(tags
); j
++) {
284 if(!regexec(regs
[i
].tagregex
, tags
[j
], 1, &tmp
, 0)) {
295 memcpy(c
->tags
, monitors
[monitorat()].seltags
, sizeof initags
);
296 if (!matched_monitor
)
297 c
->monitor
= monitorat();
304 for(c
= clients
; c
; c
= c
->next
)
305 if(isvisible(c
, c
->monitor
))
310 monitors
[selmonitor
].layout
->arrange(&monitors
[selmonitor
]);
324 attachstack(Client
*c
) {
333 XMoveWindow(dpy
, c
->win
, c
->x
+ 3 * monitors
[c
->monitor
].sw
, c
->y
);
338 buttonpress(XEvent
*e
) {
341 XButtonPressedEvent
*ev
= &e
->xbutton
;
343 Monitor
*m
= &monitors
[monitorat()];
345 if(ev
->window
== m
->barwin
) {
347 for(i
= 0; i
< LENGTH(tags
); i
++) {
350 if(ev
->button
== Button1
) {
351 if(ev
->state
& MODKEY
)
356 else if(ev
->button
== Button3
) {
357 if(ev
->state
& MODKEY
)
365 if((ev
->x
< x
+ blw
) && ev
->button
== Button1
)
368 else if((c
= getclient(ev
->window
))) {
370 if(CLEANMASK(ev
->state
) != MODKEY
)
372 if(ev
->button
== Button1
) {
376 else if(ev
->button
== Button2
) {
377 if((floating
!= m
->layout
->arrange
) && c
->isfloating
)
378 togglefloating(NULL
);
382 else if(ev
->button
== Button3
&& !c
->isfixed
) {
392 XSetErrorHandler(xerrorstart
);
394 /* this causes an error if some other window manager is running */
395 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
398 eprint("dwm: another window manager is already running\n");
400 XSetErrorHandler(NULL
);
401 xerrorxlib
= XSetErrorHandler(xerror
);
414 XFreeFontSet(dpy
, dc
.font
.set
);
416 XFreeFont(dpy
, dc
.font
.xfont
);
418 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
419 XFreePixmap(dpy
, dc
.drawable
);
421 XFreeCursor(dpy
, cursor
[CurNormal
]);
422 XFreeCursor(dpy
, cursor
[CurResize
]);
423 XFreeCursor(dpy
, cursor
[CurMove
]);
424 for(i
= 0; i
< mcount
; i
++)
425 XDestroyWindow(dpy
, monitors
[i
].barwin
);
427 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
437 regs
= emallocz(LENGTH(rules
) * sizeof(Regs
));
438 for(i
= 0; i
< LENGTH(rules
); i
++) {
440 reg
= emallocz(sizeof(regex_t
));
441 if(regcomp(reg
, rules
[i
].prop
, REG_EXTENDED
))
444 regs
[i
].propregex
= reg
;
447 reg
= emallocz(sizeof(regex_t
));
448 if(regcomp(reg
, rules
[i
].tags
, REG_EXTENDED
))
451 regs
[i
].tagregex
= reg
;
457 configure(Client
*c
) {
460 ce
.type
= ConfigureNotify
;
468 ce
.border_width
= c
->border
;
470 ce
.override_redirect
= False
;
471 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
475 configurenotify(XEvent
*e
) {
476 XConfigureEvent
*ev
= &e
->xconfigure
;
477 Monitor
*m
= &monitors
[selmonitor
];
479 if(ev
->window
== root
&& (ev
->width
!= m
->sw
|| ev
->height
!= m
->sh
)) {
480 /* TODO -- update Xinerama dimensions here */
483 XFreePixmap(dpy
, dc
.drawable
);
484 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(root
, screen
), bh
, DefaultDepth(dpy
, screen
));
485 XResizeWindow(dpy
, m
->barwin
, m
->sw
, bh
);
492 configurerequest(XEvent
*e
) {
494 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
497 if((c
= getclient(ev
->window
))) {
498 Monitor
*m
= &monitors
[c
->monitor
];
499 if(ev
->value_mask
& CWBorderWidth
)
500 c
->border
= ev
->border_width
;
501 if(c
->isfixed
|| c
->isfloating
|| (floating
== m
->layout
->arrange
)) {
502 if(ev
->value_mask
& CWX
)
504 if(ev
->value_mask
& CWY
)
506 if(ev
->value_mask
& CWWidth
)
508 if(ev
->value_mask
& CWHeight
)
510 if((c
->x
- m
->sx
+ c
->w
) > m
->sw
&& c
->isfloating
)
511 c
->x
= m
->sx
+ (m
->sw
/ 2 - c
->w
/ 2); /* center in x direction */
512 if((c
->y
- m
->sy
+ c
->h
) > m
->sh
&& c
->isfloating
)
513 c
->y
= m
->sy
+ (m
->sh
/ 2 - c
->h
/ 2); /* center in y direction */
514 if((ev
->value_mask
& (CWX
| CWY
))
515 && !(ev
->value_mask
& (CWWidth
| CWHeight
)))
517 if(isvisible(c
, monitorat()))
518 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
526 wc
.width
= ev
->width
;
527 wc
.height
= ev
->height
;
528 wc
.border_width
= ev
->border_width
;
529 wc
.sibling
= ev
->above
;
530 wc
.stack_mode
= ev
->detail
;
531 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
537 destroynotify(XEvent
*e
) {
539 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
541 if((c
= getclient(ev
->window
)))
548 c
->prev
->next
= c
->next
;
550 c
->next
->prev
= c
->prev
;
553 c
->next
= c
->prev
= NULL
;
557 detachstack(Client
*c
) {
560 for(tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
569 for(i
= 0; i
< mcount
; i
++) {
570 Monitor
*m
= &monitors
[i
];
572 for(c
= stack
; c
&& !isvisible(c
, i
); c
= c
->snext
);
573 fprintf(stderr
, "m%d %s\n", i
, c
? c
->name
: "NIL");
574 for(j
= 0; j
< LENGTH(tags
); j
++) {
575 dc
.w
= textw(tags
[j
]);
577 drawtext(m
, tags
[j
], dc
.sel
, isurgent(i
, j
));
578 drawsquare(m
, c
&& c
->tags
[j
] && c
->monitor
== i
,
579 isoccupied(i
, j
), isurgent(i
, j
), dc
.sel
);
582 drawtext(m
, tags
[j
], dc
.norm
, isurgent(i
, j
));
583 drawsquare(m
, c
&& c
->tags
[j
] && c
->monitor
== i
,
584 isoccupied(i
, j
), isurgent(i
, j
), dc
.norm
);
589 drawtext(m
, m
->layout
->symbol
, dc
.norm
, False
);
591 if(i
== selmonitor
) {
598 drawtext(m
, stext
, dc
.norm
, False
);
602 if((dc
.w
= dc
.x
- x
) > bh
) {
605 drawtext(m
, c
->name
, dc
.sel
, False
);
606 drawsquare(m
, False
, c
->isfloating
, False
, dc
.sel
);
609 drawtext(m
, NULL
, dc
.norm
, False
);
611 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->sw
, bh
, 0, 0);
617 drawsquare(Monitor
*m
, Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
620 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
622 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
623 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
624 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
628 r
.width
= r
.height
= x
+ 1;
629 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
632 r
.width
= r
.height
= x
;
633 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
638 drawtext(Monitor
*m
, const char *text
, unsigned long col
[ColLast
], Bool invert
) {
640 static char buf
[256];
641 unsigned int len
, olen
;
642 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
644 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
645 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
649 olen
= len
= strlen(text
);
650 if(len
>= sizeof buf
)
651 len
= sizeof buf
- 1;
652 memcpy(buf
, text
, len
);
654 h
= dc
.font
.ascent
+ dc
.font
.descent
;
655 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
657 /* shorten text if necessary */
658 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
669 return; /* too long */
670 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
672 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
674 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
678 emallocz(unsigned int size
) {
679 void *res
= calloc(1, size
);
682 eprint("fatal: could not malloc() %u bytes\n", size
);
687 enternotify(XEvent
*e
) {
689 XCrossingEvent
*ev
= &e
->xcrossing
;
691 if(ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) {
692 if(!isxinerama
|| ev
->window
!= root
)
695 if((c
= getclient(ev
->window
)))
698 selmonitor
= monitorat();
699 fprintf(stderr
, "updating selmonitor %d\n", selmonitor
);
705 eprint(const char *errstr
, ...) {
708 va_start(ap
, errstr
);
709 vfprintf(stderr
, errstr
, ap
);
716 XExposeEvent
*ev
= &e
->xexpose
;
719 if(ev
->window
== monitors
[selmonitor
].barwin
)
725 floating(Monitor
*m
) { /* default floating layout */
728 domwfact
= dozoom
= False
;
729 for(c
= clients
; c
; c
= c
->next
)
730 if(isvisible(c
, m
->id
))
731 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
);
739 selmonitor
= c
->monitor
;
740 m
= &monitors
[selmonitor
];
741 if(!c
|| (c
&& !isvisible(c
, selmonitor
)))
742 for(c
= stack
; c
&& !isvisible(c
, c
->monitor
); c
= c
->snext
);
743 if(sel
&& sel
!= c
) {
744 grabbuttons(sel
, False
);
745 XSetWindowBorder(dpy
, sel
->win
, dc
.norm
[ColBorder
]);
750 grabbuttons(c
, True
);
755 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
756 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
757 selmonitor
= c
->monitor
;
760 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
766 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
767 XFocusChangeEvent
*ev
= &e
->xfocus
;
769 if(sel
&& ev
->window
!= sel
->win
)
770 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
);
774 focusnext(const char *arg
) {
779 for(c
= sel
->next
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
781 for(c
= clients
; c
&& !isvisible(c
, selmonitor
); c
= c
->next
);
789 focusprev(const char *arg
) {
794 for(c
= sel
->prev
; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
796 for(c
= clients
; c
&& c
->next
; c
= c
->next
);
797 for(; c
&& !isvisible(c
, selmonitor
); c
= c
->prev
);
806 getclient(Window w
) {
809 for(c
= clients
; c
&& c
->win
!= w
; c
= c
->next
);
814 getcolor(const char *colstr
) {
815 Colormap cmap
= DefaultColormap(dpy
, screen
);
818 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
819 eprint("error, cannot allocate color '%s'\n", colstr
);
827 unsigned char *p
= NULL
;
828 unsigned long n
, extra
;
831 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
832 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
833 if(status
!= Success
)
842 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
847 if(!text
|| size
== 0)
850 XGetTextProperty(dpy
, w
, &name
, atom
);
853 if(name
.encoding
== XA_STRING
)
854 strncpy(text
, (char *)name
.value
, size
- 1);
856 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
858 strncpy(text
, *list
, size
- 1);
859 XFreeStringList(list
);
862 text
[size
- 1] = '\0';
868 grabbuttons(Client
*c
, Bool focused
) {
869 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
872 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
,
873 GrabModeAsync
, GrabModeSync
, None
, None
);
874 XGrabButton(dpy
, Button1
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
875 GrabModeAsync
, GrabModeSync
, None
, None
);
876 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
877 GrabModeAsync
, GrabModeSync
, None
, None
);
878 XGrabButton(dpy
, Button1
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
879 GrabModeAsync
, GrabModeSync
, None
, None
);
881 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
,
882 GrabModeAsync
, GrabModeSync
, None
, None
);
883 XGrabButton(dpy
, Button2
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
884 GrabModeAsync
, GrabModeSync
, None
, None
);
885 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
886 GrabModeAsync
, GrabModeSync
, None
, None
);
887 XGrabButton(dpy
, Button2
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
888 GrabModeAsync
, GrabModeSync
, None
, None
);
890 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
,
891 GrabModeAsync
, GrabModeSync
, None
, None
);
892 XGrabButton(dpy
, Button3
, MODKEY
| LockMask
, c
->win
, False
, BUTTONMASK
,
893 GrabModeAsync
, GrabModeSync
, None
, None
);
894 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
, c
->win
, False
, BUTTONMASK
,
895 GrabModeAsync
, GrabModeSync
, None
, None
);
896 XGrabButton(dpy
, Button3
, MODKEY
| numlockmask
| LockMask
, c
->win
, False
, BUTTONMASK
,
897 GrabModeAsync
, GrabModeSync
, None
, None
);
900 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
,
901 GrabModeAsync
, GrabModeSync
, None
, None
);
908 XModifierKeymap
*modmap
;
910 /* init modifier map */
911 modmap
= XGetModifierMapping(dpy
);
912 for(i
= 0; i
< 8; i
++)
913 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
914 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
915 numlockmask
= (1 << i
);
917 XFreeModifiermap(modmap
);
919 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
920 for(i
= 0; i
< LENGTH(keys
); i
++) {
921 code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
);
922 XGrabKey(dpy
, code
, keys
[i
].mod
, root
, True
,
923 GrabModeAsync
, GrabModeAsync
);
924 XGrabKey(dpy
, code
, keys
[i
].mod
| LockMask
, root
, True
,
925 GrabModeAsync
, GrabModeAsync
);
926 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
, root
, True
,
927 GrabModeAsync
, GrabModeAsync
);
928 XGrabKey(dpy
, code
, keys
[i
].mod
| numlockmask
| LockMask
, root
, True
,
929 GrabModeAsync
, GrabModeAsync
);
934 idxoftag(const char *tag
) {
937 for(i
= 0; (i
< LENGTH(tags
)) && (tags
[i
] != tag
); i
++);
938 return (i
< LENGTH(tags
)) ? i
: 0;
942 initfont(const char *fontstr
) {
943 char *def
, **missing
;
948 XFreeFontSet(dpy
, dc
.font
.set
);
949 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
952 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
953 XFreeStringList(missing
);
956 XFontSetExtents
*font_extents
;
957 XFontStruct
**xfonts
;
959 dc
.font
.ascent
= dc
.font
.descent
= 0;
960 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
961 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
962 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
963 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
964 dc
.font
.ascent
= (*xfonts
)->ascent
;
965 if(dc
.font
.descent
< (*xfonts
)->descent
)
966 dc
.font
.descent
= (*xfonts
)->descent
;
972 XFreeFont(dpy
, dc
.font
.xfont
);
973 dc
.font
.xfont
= NULL
;
974 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
975 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
976 eprint("error, cannot load font: '%s'\n", fontstr
);
977 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
978 dc
.font
.descent
= dc
.font
.xfont
->descent
;
980 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
984 isoccupied(unsigned int monitor
, unsigned int t
) {
987 for(c
= clients
; c
; c
= c
->next
)
988 if(c
->tags
[t
] && c
->monitor
== monitor
)
994 isprotodel(Client
*c
) {
999 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1000 for(i
= 0; !ret
&& i
< n
; i
++)
1001 if(protocols
[i
] == wmatom
[WMDelete
])
1009 isurgent(unsigned int monitor
, unsigned int t
) {
1012 for(c
= clients
; c
; c
= c
->next
)
1013 if(c
->monitor
== monitor
&& c
->isurgent
&& c
->tags
[t
])
1019 isvisible(Client
*c
, int monitor
) {
1022 if(c
->monitor
!= monitor
)
1024 for(i
= 0; i
< LENGTH(tags
); i
++)
1025 if(c
->tags
[i
] && monitors
[c
->monitor
].seltags
[i
])
1031 keypress(XEvent
*e
) {
1037 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1038 for(i
= 0; i
< LENGTH(keys
); i
++)
1039 if(keysym
== keys
[i
].keysym
1040 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
))
1043 keys
[i
].func(keys
[i
].arg
);
1048 killclient(const char *arg
) {
1053 if(isprotodel(sel
)) {
1054 ev
.type
= ClientMessage
;
1055 ev
.xclient
.window
= sel
->win
;
1056 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1057 ev
.xclient
.format
= 32;
1058 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1059 ev
.xclient
.data
.l
[1] = CurrentTime
;
1060 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
);
1063 XKillClient(dpy
, sel
->win
);
1067 manage(Window w
, XWindowAttributes
*wa
) {
1068 Client
*c
, *t
= NULL
;
1074 c
= emallocz(sizeof(Client
));
1075 c
->tags
= emallocz(sizeof initags
);
1080 m
= &monitors
[c
->monitor
];
1082 c
->x
= wa
->x
+ m
->sx
;
1083 c
->y
= wa
->y
+ m
->sy
;
1086 c
->oldborder
= wa
->border_width
;
1088 if(c
->w
== m
->sw
&& c
->h
== m
->sh
) {
1091 c
->border
= wa
->border_width
;
1094 if(c
->x
+ c
->w
+ 2 * c
->border
> m
->wax
+ m
->waw
)
1095 c
->x
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1096 if(c
->y
+ c
->h
+ 2 * c
->border
> m
->way
+ m
->wah
)
1097 c
->y
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1102 c
->border
= BORDERPX
;
1104 wc
.border_width
= c
->border
;
1105 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1106 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1107 configure(c
); /* propagates border_width, if size doesn't change */
1109 XSelectInput(dpy
, w
, EnterWindowMask
| FocusChangeMask
| PropertyChangeMask
| StructureNotifyMask
);
1110 grabbuttons(c
, False
);
1112 if((rettrans
= XGetTransientForHint(dpy
, w
, &trans
) == Success
))
1113 for(t
= clients
; t
&& t
->win
!= trans
; t
= t
->next
);
1115 memcpy(c
->tags
, t
->tags
, sizeof initags
);
1117 c
->isfloating
= (rettrans
== Success
) || c
->isfixed
;
1120 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1122 XMapWindow(dpy
, c
->win
);
1123 setclientstate(c
, NormalState
);
1128 mappingnotify(XEvent
*e
) {
1129 XMappingEvent
*ev
= &e
->xmapping
;
1131 XRefreshKeyboardMapping(ev
);
1132 if(ev
->request
== MappingKeyboard
)
1137 maprequest(XEvent
*e
) {
1138 static XWindowAttributes wa
;
1139 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1141 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1143 if(wa
.override_redirect
)
1145 if(!getclient(ev
->window
))
1146 manage(ev
->window
, &wa
);
1155 XQueryPointer(dpy
, root
, &win
, &win
, &x
, &y
, &i
, &i
, &mask
);
1156 for(i
= 0; i
< mcount
; i
++) {
1157 if((x
>= monitors
[i
].sx
&& x
< monitors
[i
].sx
+ monitors
[i
].sw
)
1158 && (y
>= monitors
[i
].sy
&& y
< monitors
[i
].sy
+ monitors
[i
].sh
)) {
1166 movemouse(Client
*c
) {
1167 int x1
, y1
, ocx
, ocy
, di
, nx
, ny
;
1175 m
= &monitors
[c
->monitor
];
1176 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1177 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1179 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
);
1181 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1184 XUngrabPointer(dpy
, CurrentTime
);
1186 case ConfigureRequest
:
1189 handler
[ev
.type
](&ev
);
1193 nx
= ocx
+ (ev
.xmotion
.x
- x1
);
1194 ny
= ocy
+ (ev
.xmotion
.y
- y1
);
1195 if(abs(m
->wax
- nx
) < SNAP
)
1197 else if(abs((m
->wax
+ m
->waw
) - (nx
+ c
->w
+ 2 * c
->border
)) < SNAP
)
1198 nx
= m
->wax
+ m
->waw
- c
->w
- 2 * c
->border
;
1199 if(abs(m
->way
- ny
) < SNAP
)
1201 else if(abs((m
->way
+ m
->wah
) - (ny
+ c
->h
+ 2 * c
->border
)) < SNAP
)
1202 ny
= m
->way
+ m
->wah
- c
->h
- 2 * c
->border
;
1203 if((m
->layout
->arrange
!= floating
) && (abs(nx
- c
->x
) > SNAP
|| abs(ny
- c
->y
) > SNAP
))
1204 togglefloating(NULL
);
1205 if((m
->layout
->arrange
== floating
) || c
->isfloating
)
1206 resize(c
, nx
, ny
, c
->w
, c
->h
, False
);
1213 nexttiled(Client
*c
, int monitor
) {
1214 for(; c
&& (c
->isfloating
|| !isvisible(c
, monitor
)); c
= c
->next
);
1219 propertynotify(XEvent
*e
) {
1222 XPropertyEvent
*ev
= &e
->xproperty
;
1224 if(ev
->state
== PropertyDelete
)
1225 return; /* ignore */
1226 if((c
= getclient(ev
->window
))) {
1229 case XA_WM_TRANSIENT_FOR
:
1230 XGetTransientForHint(dpy
, c
->win
, &trans
);
1231 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1234 case XA_WM_NORMAL_HINTS
:
1242 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1251 quit(const char *arg
) {
1252 readin
= running
= False
;
1256 reapply(const char *arg
) {
1257 static Bool zerotags
[LENGTH(tags
)] = { 0 };
1260 for(c
= clients
; c
; c
= c
->next
) {
1261 memcpy(c
->tags
, zerotags
, sizeof zerotags
);
1268 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) {
1272 m
= &monitors
[c
->monitor
];
1275 /* set minimum possible */
1281 /* temporarily remove base dimensions */
1285 /* adjust for aspect limits */
1286 if (c
->minay
> 0 && c
->maxay
> 0 && c
->minax
> 0 && c
->maxax
> 0) {
1287 if (w
* c
->maxay
> h
* c
->maxax
)
1288 w
= h
* c
->maxax
/ c
->maxay
;
1289 else if (w
* c
->minay
< h
* c
->minax
)
1290 h
= w
* c
->minay
/ c
->minax
;
1293 /* adjust for increment value */
1299 /* restore base dimensions */
1303 if(c
->minw
> 0 && w
< c
->minw
)
1305 if(c
->minh
> 0 && h
< c
->minh
)
1307 if(c
->maxw
> 0 && w
> c
->maxw
)
1309 if(c
->maxh
> 0 && h
> c
->maxh
)
1312 if(w
<= 0 || h
<= 0)
1315 x
= m
->sw
- w
- 2 * c
->border
;
1317 y
= m
->sh
- h
- 2 * c
->border
;
1318 if(x
+ w
+ 2 * c
->border
< m
->sx
)
1320 if(y
+ h
+ 2 * c
->border
< m
->sy
)
1322 if(c
->x
!= x
|| c
->y
!= y
|| c
->w
!= w
|| c
->h
!= h
) {
1325 c
->w
= wc
.width
= w
;
1326 c
->h
= wc
.height
= h
;
1327 wc
.border_width
= c
->border
;
1328 XConfigureWindow(dpy
, c
->win
, CWX
| CWY
| CWWidth
| CWHeight
| CWBorderWidth
, &wc
);
1335 resizemouse(Client
*c
) {
1343 m
= &monitors
[c
->monitor
];
1344 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1345 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1347 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1349 XMaskEvent(dpy
, MOUSEMASK
| ExposureMask
| SubstructureRedirectMask
, &ev
);
1352 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0,
1353 c
->w
+ c
->border
- 1, c
->h
+ c
->border
- 1);
1354 XUngrabPointer(dpy
, CurrentTime
);
1355 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1357 case ConfigureRequest
:
1360 handler
[ev
.type
](&ev
);
1364 if((nw
= ev
.xmotion
.x
- ocx
- 2 * c
->border
+ 1) <= 0)
1366 if((nh
= ev
.xmotion
.y
- ocy
- 2 * c
->border
+ 1) <= 0)
1368 if((m
->layout
->arrange
!= floating
) && (abs(nw
- c
->w
) > SNAP
|| abs(nh
- c
->h
) > SNAP
))
1369 togglefloating(NULL
);
1370 if((m
->layout
->arrange
== floating
) || c
->isfloating
)
1371 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1387 if(sel
->isfloating
|| (monitors
[selmonitor
].layout
->arrange
== floating
))
1388 XRaiseWindow(dpy
, sel
->win
);
1389 if(monitors
[selmonitor
].layout
->arrange
!= floating
) {
1390 wc
.stack_mode
= Below
;
1391 wc
.sibling
= monitors
[selmonitor
].barwin
;
1392 if(!sel
->isfloating
) {
1393 XConfigureWindow(dpy
, sel
->win
, CWSibling
| CWStackMode
, &wc
);
1394 wc
.sibling
= sel
->win
;
1396 for(i
= 0; i
< mcount
; i
++) {
1397 for(c
= nexttiled(clients
, i
); c
; c
= nexttiled(c
->next
, i
)) {
1400 XConfigureWindow(dpy
, c
->win
, CWSibling
| CWStackMode
, &wc
);
1401 wc
.sibling
= c
->win
;
1406 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1412 char buf
[sizeof stext
];
1415 unsigned int len
, offset
;
1418 /* main event loop, also reads status text from stdin */
1420 xfd
= ConnectionNumber(dpy
);
1423 len
= sizeof stext
- 1;
1424 buf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */
1428 FD_SET(STDIN_FILENO
, &rd
);
1430 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
1433 eprint("select failed\n");
1435 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
1436 switch((r
= read(STDIN_FILENO
, buf
+ offset
, len
- offset
))) {
1438 strncpy(stext
, strerror(errno
), len
);
1442 strncpy(stext
, "EOF", 4);
1446 for(p
= buf
+ offset
; r
> 0; p
++, r
--, offset
++)
1447 if(*p
== '\n' || *p
== '\0') {
1449 strncpy(stext
, buf
, len
);
1450 p
+= r
- 1; /* p is buf + offset + r - 1 */
1451 for(r
= 0; *(p
- r
) && *(p
- r
) != '\n'; r
++);
1454 memmove(buf
, p
- r
+ 1, r
);
1461 while(XPending(dpy
)) {
1462 XNextEvent(dpy
, &ev
);
1463 if(handler
[ev
.type
])
1464 (handler
[ev
.type
])(&ev
); /* call handler */
1471 unsigned int i
, num
;
1472 Window
*wins
, d1
, d2
;
1473 XWindowAttributes wa
;
1476 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1477 for(i
= 0; i
< num
; i
++) {
1478 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1479 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1481 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1482 manage(wins
[i
], &wa
);
1484 for(i
= 0; i
< num
; i
++) { /* now the transients */
1485 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1487 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1488 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1489 manage(wins
[i
], &wa
);
1497 setclientstate(Client
*c
, long state
) {
1498 long data
[] = {state
, None
};
1500 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1501 PropModeReplace
, (unsigned char *)data
, 2);
1505 setlayout(const char *arg
) {
1507 Monitor
*m
= &monitors
[monitorat()];
1511 if(m
->layout
== &layouts
[LENGTH(layouts
)])
1512 m
->layout
= &layouts
[0];
1515 for(i
= 0; i
< LENGTH(layouts
); i
++)
1516 if(!strcmp(arg
, layouts
[i
].symbol
))
1518 if(i
== LENGTH(layouts
))
1520 m
->layout
= &layouts
[i
];
1529 setmwfact(const char *arg
) {
1532 Monitor
*m
= &monitors
[monitorat()];
1536 /* arg handling, manipulate mwfact */
1539 else if(sscanf(arg
, "%lf", &delta
) == 1) {
1540 if(arg
[0] == '+' || arg
[0] == '-')
1546 else if(m
->mwfact
> 0.9)
1556 XSetWindowAttributes wa
;
1557 XineramaScreenInfo
*info
= NULL
;
1560 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1561 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1562 wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
);
1563 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1564 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1565 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1568 wa
.cursor
= cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1569 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1570 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1572 // init screens/monitors first
1574 if((isxinerama
= XineramaIsActive(dpy
)))
1575 info
= XineramaQueryScreens(dpy
, &mcount
);
1576 monitors
= emallocz(mcount
* sizeof(Monitor
));
1578 screen
= DefaultScreen(dpy
);
1579 root
= RootWindow(dpy
, screen
);
1581 /* init appearance */
1582 dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
);
1583 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
1584 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
1585 dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
);
1586 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
1587 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
1589 dc
.h
= bh
= dc
.font
.height
+ 2;
1590 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1591 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
1592 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1594 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1596 for(blw
= i
= 0; i
< LENGTH(layouts
); i
++) {
1597 i
= textw(layouts
[i
].symbol
);
1601 for(i
= 0; i
< mcount
; i
++) {
1606 if (mcount
!= 1 && isxinerama
) {
1607 m
->sx
= info
[i
].x_org
;
1608 m
->sy
= info
[i
].y_org
;
1609 m
->sw
= info
[i
].width
;
1610 m
->sh
= info
[i
].height
;
1611 fprintf(stderr
, "monitor[%d]: %d,%d,%d,%d\n", i
, m
->sx
, m
->sy
, m
->sw
, m
->sh
);
1616 m
->sw
= DisplayWidth(dpy
, screen
);
1617 m
->sh
= DisplayHeight(dpy
, screen
);
1620 m
->seltags
= emallocz(sizeof initags
);
1621 m
->prevtags
= emallocz(sizeof initags
);
1623 memcpy(m
->seltags
, initags
, sizeof initags
);
1624 memcpy(m
->prevtags
, initags
, sizeof initags
);
1628 m
->layout
= &layouts
[0];
1630 // TODO: bpos per screen?
1632 wa
.override_redirect
= 1;
1633 wa
.background_pixmap
= ParentRelative
;
1634 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
1637 m
->barwin
= XCreateWindow(dpy
, root
, m
->sx
, m
->sy
, m
->sw
, bh
, 0,
1638 DefaultDepth(dpy
, screen
), CopyFromParent
, DefaultVisual(dpy
, screen
),
1639 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
1640 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1642 XMapRaised(dpy
, m
->barwin
);
1643 strcpy(stext
, "dwm-"VERSION
);
1645 /* EWMH support per monitor */
1646 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1647 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1649 /* select for events */
1650 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
1651 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
1652 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
1653 XSelectInput(dpy
, root
, wa
.event_mask
);
1664 selmonitor
= monitorat();
1665 fprintf(stderr
, "selmonitor == %d\n", selmonitor
);
1669 spawn(const char *arg
) {
1670 static char *shell
= NULL
;
1672 if(!shell
&& !(shell
= getenv("SHELL")))
1676 /* The double-fork construct avoids zombie processes and keeps the code
1677 * clean from stupid signal handlers. */
1681 close(ConnectionNumber(dpy
));
1683 execl(shell
, shell
, "-c", arg
, (char *)NULL
);
1684 fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
);
1693 tag(const char *arg
) {
1698 for(i
= 0; i
< LENGTH(tags
); i
++)
1699 sel
->tags
[i
] = (NULL
== arg
);
1700 sel
->tags
[idxoftag(arg
)] = True
;
1705 textnw(const char *text
, unsigned int len
) {
1709 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1712 return XTextWidth(dc
.font
.xfont
, text
, len
);
1716 textw(const char *text
) {
1717 return textnw(text
, strlen(text
)) + dc
.font
.height
;
1722 unsigned int i
, n
, nx
, ny
, nw
, nh
, mw
, th
;
1725 domwfact
= dozoom
= True
;
1727 nx
= ny
= nw
= 0; /* gcc stupidity requires this */
1729 for(n
= 0, c
= nexttiled(clients
, m
->id
); c
; c
= nexttiled(c
->next
, m
->id
))
1733 mw
= (n
== 1) ? m
->waw
: m
->mwfact
* m
->waw
;
1734 th
= (n
> 1) ? m
->wah
/ (n
- 1) : 0;
1735 if(n
> 1 && th
< bh
)
1738 for(i
= 0, c
= mc
= nexttiled(clients
, m
->id
); c
; c
= nexttiled(c
->next
, m
->id
)) {
1739 if(i
== 0) { /* master */
1742 nw
= mw
- 2 * c
->border
;
1743 nh
= m
->wah
- 2 * c
->border
;
1745 else { /* tile window */
1748 nx
+= mc
->w
+ 2 * mc
->border
;
1749 nw
= m
->waw
- mw
- 2 * c
->border
;
1751 if(i
+ 1 == n
) /* remainder */
1752 nh
= (m
->way
+ m
->wah
) - ny
- 2 * c
->border
;
1754 nh
= th
- 2 * c
->border
;
1756 resize(c
, nx
, ny
, nw
, nh
, RESIZEHINTS
);
1757 if((RESIZEHINTS
) && ((c
->h
< bh
) || (c
->h
> nh
) || (c
->w
< bh
) || (c
->w
> nw
)))
1758 /* client doesn't accept size constraints */
1759 resize(c
, nx
, ny
, nw
, nh
, False
);
1760 if(n
> 1 && th
!= m
->wah
)
1761 ny
= c
->y
+ c
->h
+ 2 * c
->border
;
1767 togglebar(const char *arg
) {
1769 bpos
= (BARPOS
== BarOff
) ? BarTop
: BARPOS
;
1772 updatebarpos(&monitors
[monitorat()]);
1777 togglefloating(const char *arg
) {
1780 sel
->isfloating
= !sel
->isfloating
;
1782 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
);
1787 toggletag(const char *arg
) {
1793 sel
->tags
[i
] = !sel
->tags
[i
];
1794 for(j
= 0; j
< LENGTH(tags
) && !sel
->tags
[j
]; j
++);
1795 if(j
== LENGTH(tags
))
1796 sel
->tags
[i
] = True
; /* at least one tag must be enabled */
1801 toggleview(const char *arg
) {
1804 Monitor
*m
= &monitors
[monitorat()];
1807 m
->seltags
[i
] = !m
->seltags
[i
];
1808 for(j
= 0; j
< LENGTH(tags
) && !m
->seltags
[j
]; j
++);
1809 if(j
== LENGTH(tags
))
1810 m
->seltags
[i
] = True
; /* at least one tag must be viewed */
1818 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1819 c
->isbanned
= False
;
1823 unmanage(Client
*c
) {
1826 wc
.border_width
= c
->oldborder
;
1827 /* The server grab construct avoids race conditions. */
1829 XSetErrorHandler(xerrordummy
);
1830 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1835 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1836 setclientstate(c
, WithdrawnState
);
1840 XSetErrorHandler(xerror
);
1846 unmapnotify(XEvent
*e
) {
1848 XUnmapEvent
*ev
= &e
->xunmap
;
1850 if((c
= getclient(ev
->window
)))
1855 updatebarpos(Monitor
*m
) {
1866 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
);
1870 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
+ m
->wah
);
1873 XMoveWindow(dpy
, m
->barwin
, m
->sx
, m
->sy
- bh
);
1877 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1881 updatesizehints(Client
*c
) {
1885 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
)
1887 c
->flags
= size
.flags
;
1888 if(c
->flags
& PBaseSize
) {
1889 c
->basew
= size
.base_width
;
1890 c
->baseh
= size
.base_height
;
1892 else if(c
->flags
& PMinSize
) {
1893 c
->basew
= size
.min_width
;
1894 c
->baseh
= size
.min_height
;
1897 c
->basew
= c
->baseh
= 0;
1898 if(c
->flags
& PResizeInc
) {
1899 c
->incw
= size
.width_inc
;
1900 c
->inch
= size
.height_inc
;
1903 c
->incw
= c
->inch
= 0;
1904 if(c
->flags
& PMaxSize
) {
1905 c
->maxw
= size
.max_width
;
1906 c
->maxh
= size
.max_height
;
1909 c
->maxw
= c
->maxh
= 0;
1910 if(c
->flags
& PMinSize
) {
1911 c
->minw
= size
.min_width
;
1912 c
->minh
= size
.min_height
;
1914 else if(c
->flags
& PBaseSize
) {
1915 c
->minw
= size
.base_width
;
1916 c
->minh
= size
.base_height
;
1919 c
->minw
= c
->minh
= 0;
1920 if(c
->flags
& PAspect
) {
1921 c
->minax
= size
.min_aspect
.x
;
1922 c
->maxax
= size
.max_aspect
.x
;
1923 c
->minay
= size
.min_aspect
.y
;
1924 c
->maxay
= size
.max_aspect
.y
;
1927 c
->minax
= c
->maxax
= c
->minay
= c
->maxay
= 0;
1928 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1929 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1933 updatetitle(Client
*c
) {
1934 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1935 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
);
1939 updatewmhints(Client
*c
) {
1942 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1943 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1948 /* There's no way to check accesses to destroyed windows, thus those cases are
1949 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1950 * default error handler, which may call exit. */
1952 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1953 if(ee
->error_code
== BadWindow
1954 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1955 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1956 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1957 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1958 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1959 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1960 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1962 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1963 ee
->request_code
, ee
->error_code
);
1964 return xerrorxlib(dpy
, ee
); /* may call exit */
1968 xerrordummy(Display
*dsply
, XErrorEvent
*ee
) {
1972 /* Startup Error handler to check if another window manager
1973 * is already running. */
1975 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
1981 view(const char *arg
) {
1983 Bool tmp
[LENGTH(tags
)];
1984 Monitor
*m
= &monitors
[monitorat()];
1986 for(i
= 0; i
< LENGTH(tags
); i
++)
1987 tmp
[i
] = (NULL
== arg
);
1988 tmp
[idxoftag(arg
)] = True
;
1989 if(memcmp(m
->seltags
, tmp
, sizeof initags
) != 0) {
1990 memcpy(m
->prevtags
, m
->seltags
, sizeof initags
);
1991 memcpy(m
->seltags
, tmp
, sizeof initags
);
1997 viewprevtag(const char *arg
) {
1998 static Bool tmp
[LENGTH(tags
)];
2000 Monitor
*m
= &monitors
[monitorat()];
2002 memcpy(tmp
, m
->seltags
, sizeof initags
);
2003 memcpy(m
->seltags
, m
->prevtags
, sizeof initags
);
2004 memcpy(m
->prevtags
, tmp
, sizeof initags
);
2009 zoom(const char *arg
) {
2012 if(!sel
|| !dozoom
|| sel
->isfloating
)
2014 if(c
== nexttiled(clients
, c
->monitor
))
2015 if(!(c
= nexttiled(c
->next
, c
->monitor
)))
2024 movetomonitor(const char *arg
) {
2026 sel
->monitor
= arg
? atoi(arg
) : (sel
->monitor
+1) % mcount
;
2028 memcpy(sel
->tags
, monitors
[sel
->monitor
].seltags
, sizeof initags
);
2029 resize(sel
, monitors
[sel
->monitor
].wax
, monitors
[sel
->monitor
].way
, sel
->w
, sel
->h
, True
);
2035 selectmonitor(const char *arg
) {
2036 Monitor
*m
= &monitors
[arg
? atoi(arg
) : (monitorat()+1) % mcount
];
2038 XWarpPointer(dpy
, None
, root
, 0, 0, 0, 0, m
->wax
+m
->waw
/2, m
->way
+m
->wah
/2);
2044 main(int argc
, char *argv
[]) {
2045 if(argc
== 2 && !strcmp("-v", argv
[1]))
2046 eprint("dwm-"VERSION
", © 2006-2008 Anselm R. Garbe, Sander van Dijk, "
2047 "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
2049 eprint("usage: dwm [-v]\n");
2051 setlocale(LC_CTYPE
, "");
2052 if(!(dpy
= XOpenDisplay(0)))
2053 eprint("dwm: cannot open display\n");