Xinqi Bao's Git
59722d344a5475e359ebe8131a1bbd8b2652d2bb
1 /* TODO: cleanup nexttiled, ISVISIBLE, etc */
2 /* See LICENSE file for copyright and license details.
4 * dynamic window manager is designed like any other X client as well. It is
5 * driven through handling X events. In contrast to other X clients, a window
6 * manager selects for SubstructureRedirectMask on the root window, to receive
7 * events about window (dis-)appearance. Only one X connection at a time is
8 * allowed to select for this event mask.
10 * The event handlers of dwm are organized in an array which is accessed
11 * whenever a new event has been fetched. This allows event dispatching
14 * Each child of the root window is called a client, except windows which have
15 * set the override_redirect flag. Clients are organized in a global
16 * linked client list, the focus history is remembered through a global
17 * stack list. Each client contains a bit array to indicate the tags of a
20 * Keys and tagging rules are organized as arrays and defined in config.h.
22 * To understand everything else, start reading main().
32 #include <sys/types.h>
34 #include <X11/cursorfont.h>
35 #include <X11/keysym.h>
36 #include <X11/Xatom.h>
38 #include <X11/Xproto.h>
39 #include <X11/Xutil.h>
41 #include <X11/extensions/Xinerama.h>
45 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
46 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
47 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
48 #define ISVISIBLE(M, C) ((M) == (C->mon) && (C->tags & M->tagset[M->seltags]))
49 #define LENGTH(X) (sizeof X / sizeof X[0])
50 #define MAX(A, B) ((A) > (B) ? (A) : (B))
51 #define MIN(A, B) ((A) < (B) ? (A) : (B))
52 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
53 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
54 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
55 #define TAGMASK ((int)((1LL << LENGTH(tags)) - 1))
56 #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
59 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
60 enum { ColBorder
, ColFG
, ColBG
, ColLast
}; /* color */
61 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
62 enum { WMProtocols
, WMDelete
, WMState
, WMLast
}; /* default atoms */
63 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
64 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
77 void (*func
)(const Arg
*arg
);
81 typedef struct Monitor Monitor
;
82 typedef struct Client Client
;
87 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
90 Bool isfixed
, isfloating
, isurgent
;
99 unsigned long norm
[ColLast
];
100 unsigned long sel
[ColLast
];
110 } DC
; /* draw context */
115 void (*func
)(const Arg
*);
121 void (*arrange
)(Monitor
*);
127 int by
, btx
; /* bar geometry */
128 int my
, mh
; /* vertical screen size*/
129 int wx
, wy
, ww
, wh
; /* window area */
130 unsigned int seltags
;
132 unsigned int tagset
[2];
144 const char *instance
;
150 /* function declarations */
151 static void applyrules(Client
*c
);
152 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
);
153 static void arrange(void);
154 static void attach(Client
*c
);
155 static void attachstack(Client
*c
);
156 static void buttonpress(XEvent
*e
);
157 static void checkotherwm(void);
158 static void cleanup(void);
159 static void cleanupmons(void);
160 static void clearurgent(Client
*c
);
161 static void configure(Client
*c
);
162 static void configurenotify(XEvent
*e
);
163 static void configurerequest(XEvent
*e
);
164 static void destroynotify(XEvent
*e
);
165 static void detach(Client
*c
);
166 static void detachstack(Client
*c
);
167 static void die(const char *errstr
, ...);
168 static void drawbar(Monitor
*m
);
169 static void drawbars();
170 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
171 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
172 static void enternotify(XEvent
*e
);
173 static void expose(XEvent
*e
);
174 static void focus(Client
*c
);
175 static void focusin(XEvent
*e
);
176 static void focusstack(const Arg
*arg
);
177 static Client
*getclient(Window w
);
178 static unsigned long getcolor(const char *colstr
);
179 static long getstate(Window w
);
180 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
181 static void grabbuttons(Client
*c
, Bool focused
);
182 static void grabkeys(void);
183 static void initfont(const char *fontstr
);
184 static Bool
isprotodel(Client
*c
);
185 static void keypress(XEvent
*e
);
186 static void killclient(const Arg
*arg
);
187 static void manage(Window w
, XWindowAttributes
*wa
);
188 static void mappingnotify(XEvent
*e
);
189 static void maprequest(XEvent
*e
);
190 static void monocle(Monitor
*m
);
191 static void movemouse(const Arg
*arg
);
192 static Client
*nexttiled(Monitor
*m
, Client
*c
);
193 static void propertynotify(XEvent
*e
);
194 static void quit(const Arg
*arg
);
195 static void resize(Client
*c
, int x
, int y
, int w
, int h
);
196 static void resizemouse(const Arg
*arg
);
197 static void restack(Monitor
*m
);
198 static void run(void);
199 static void scan(void);
200 static void setclientstate(Client
*c
, long state
);
201 static void setlayout(const Arg
*arg
);
202 static void setmfact(const Arg
*arg
);
203 static void setup(void);
204 static void showhide(Client
*c
);
205 static void sigchld(int signal
);
206 static void spawn(const Arg
*arg
);
207 static void tag(const Arg
*arg
);
208 static int textnw(const char *text
, unsigned int len
);
209 static void tile(Monitor
*);
210 static void togglebar(const Arg
*arg
);
211 static void togglefloating(const Arg
*arg
);
212 static void toggletag(const Arg
*arg
);
213 static void toggleview(const Arg
*arg
);
214 static void unmanage(Client
*c
);
215 static void unmapnotify(XEvent
*e
);
216 static void updategeom(void);
217 static void updatebarpos(Monitor
*m
);
218 static void updatebars(void);
219 static void updatenumlockmask(void);
220 static void updatesizehints(Client
*c
);
221 static void updatestatus(void);
222 static void updatetitle(Client
*c
);
223 static void updatewmhints(Client
*c
);
224 static void view(const Arg
*arg
);
225 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
226 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
227 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
228 static void zoom(const Arg
*arg
);
230 static void focusmon(const Arg
*arg
);
231 static void tagmon(const Arg
*arg
);
232 #endif /* XINERAMA */
235 static char stext
[256];
237 static int sx
, sy
, sw
, sh
; /* X display screen geometry x, y, width, height */
238 static int bh
, blw
= 0; /* bar geometry */
239 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
240 static unsigned int numlockmask
= 0;
241 static void (*handler
[LASTEvent
]) (XEvent
*) = {
242 [ButtonPress
] = buttonpress
,
243 [ConfigureRequest
] = configurerequest
,
244 [ConfigureNotify
] = configurenotify
,
245 [DestroyNotify
] = destroynotify
,
246 [EnterNotify
] = enternotify
,
249 [KeyPress
] = keypress
,
250 [MappingNotify
] = mappingnotify
,
251 [MapRequest
] = maprequest
,
252 [PropertyNotify
] = propertynotify
,
253 [UnmapNotify
] = unmapnotify
255 static Atom wmatom
[WMLast
], netatom
[NetLast
];
257 static Bool running
= True
;
258 static Cursor cursor
[CurLast
];
261 static Layout
*lt
[] = { NULL
, NULL
};
262 static Monitor
*mons
= NULL
, *selmon
= NULL
;
264 /* configuration, allows nested code to access above variables */
267 /* compile-time check if all tags fit into an unsigned int bit array. */
268 struct NumTags
{ char limitexceeded
[sizeof(unsigned int) * 8 < LENGTH(tags
) ? -1 : 1]; };
270 /* function implementations */
272 applyrules(Client
*c
) {
275 XClassHint ch
= { 0 };
278 c
->isfloating
= c
->tags
= 0;
279 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
280 for(i
= 0; i
< LENGTH(rules
); i
++) {
282 if((!r
->title
|| strstr(c
->name
, r
->title
))
283 && (!r
->class || (ch
.res_class
&& strstr(ch
.res_class
, r
->class)))
284 && (!r
->instance
|| (ch
.res_name
&& strstr(ch
.res_name
, r
->instance
)))) {
285 c
->isfloating
= r
->isfloating
;
294 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
298 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
) {
301 /* set minimum possible */
309 if(*x
+ *w
+ 2 * c
->bw
< sx
)
311 if(*y
+ *h
+ 2 * c
->bw
< sy
)
318 if(resizehints
|| c
->isfloating
) {
319 /* see last two sentences in ICCCM 4.1.2.3 */
320 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
322 if(!baseismin
) { /* temporarily remove base dimensions */
327 /* adjust for aspect limits */
328 if(c
->mina
> 0 && c
->maxa
> 0) {
329 if(c
->maxa
< (float)*w
/ *h
)
331 else if(c
->mina
< (float)*h
/ *w
)
335 if(baseismin
) { /* increment calculation requires this */
340 /* adjust for increment value */
346 /* restore base dimensions */
350 *w
= MAX(*w
, c
->minw
);
351 *h
= MAX(*h
, c
->minh
);
354 *w
= MIN(*w
, c
->maxw
);
357 *h
= MIN(*h
, c
->maxh
);
359 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
366 /* optimise two loops into one, check focus(NULL) */
367 for(m
= mons
; m
; m
= m
->next
)
370 for(m
= mons
; m
; m
= m
->next
) {
371 if(lt
[m
->sellt
]->arrange
)
372 lt
[m
->sellt
]->arrange(m
);
379 c
->next
= selmon
->clients
;
384 attachstack(Client
*c
) {
385 c
->snext
= selmon
->stack
;
390 buttonpress(XEvent
*e
) {
391 unsigned int i
, x
, click
;
394 XButtonPressedEvent
*ev
= &e
->xbutton
;
397 if(ev
->window
== selmon
->barwin
&& ev
->x
>= selmon
->btx
) {
402 while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
403 if(i
< LENGTH(tags
)) {
407 else if(ev
->x
< x
+ blw
)
409 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
410 click
= ClkStatusText
;
414 else if((c
= getclient(ev
->window
))) {
416 click
= ClkClientWin
;
419 for(i
= 0; i
< LENGTH(buttons
); i
++)
420 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
421 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
422 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
428 xerrorxlib
= XSetErrorHandler(xerrorstart
);
430 /* this causes an error if some other window manager is running */
431 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
434 die("dwm: another window manager is already running\n");
435 XSetErrorHandler(xerror
);
442 Layout foo
= { "", NULL
};
446 lt
[selmon
->sellt
] = &foo
;
448 /* TODO: consider simplifying cleanup code of the stack, perhaps do that in cleanmons() ? */
449 for(m
= mons
; m
; m
= m
->next
)
453 XFreeFontSet(dpy
, dc
.font
.set
);
455 XFreeFont(dpy
, dc
.font
.xfont
);
456 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
457 XFreePixmap(dpy
, dc
.drawable
);
459 XFreeCursor(dpy
, cursor
[CurNormal
]);
460 XFreeCursor(dpy
, cursor
[CurResize
]);
461 XFreeCursor(dpy
, cursor
[CurMove
]);
464 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
473 XUnmapWindow(dpy
, mons
->barwin
);
474 XDestroyWindow(dpy
, mons
->barwin
);
481 clearurgent(Client
*c
) {
485 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
487 wmh
->flags
&= ~XUrgencyHint
;
488 XSetWMHints(dpy
, c
->win
, wmh
);
493 configure(Client
*c
) {
496 ce
.type
= ConfigureNotify
;
504 ce
.border_width
= c
->bw
;
506 ce
.override_redirect
= False
;
507 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
511 configurenotify(XEvent
*e
) {
513 XConfigureEvent
*ev
= &e
->xconfigure
;
515 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
520 XFreePixmap(dpy
, dc
.drawable
);
521 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
523 for(m
= mons
; m
; m
= m
->next
)
524 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
530 configurerequest(XEvent
*e
) {
532 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
535 if((c
= getclient(ev
->window
))) {
536 if(ev
->value_mask
& CWBorderWidth
)
537 c
->bw
= ev
->border_width
;
538 else if(c
->isfloating
|| !lt
[selmon
->sellt
]->arrange
) {
539 if(ev
->value_mask
& CWX
)
541 if(ev
->value_mask
& CWY
)
543 if(ev
->value_mask
& CWWidth
)
545 if(ev
->value_mask
& CWHeight
)
547 if((c
->x
- sx
+ c
->w
) > sw
&& c
->isfloating
)
548 c
->x
= sx
+ (sw
/ 2 - c
->w
/ 2); /* center in x direction */
549 if((c
->y
- sy
+ c
->h
) > sh
&& c
->isfloating
)
550 c
->y
= sy
+ (sh
/ 2 - c
->h
/ 2); /* center in y direction */
551 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
553 if(ISVISIBLE((c
->mon
), c
))
554 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
562 wc
.width
= ev
->width
;
563 wc
.height
= ev
->height
;
564 wc
.border_width
= ev
->border_width
;
565 wc
.sibling
= ev
->above
;
566 wc
.stack_mode
= ev
->detail
;
567 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
573 destroynotify(XEvent
*e
) {
575 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
577 if((c
= getclient(ev
->window
)))
585 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
590 detachstack(Client
*c
) {
593 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
598 die(const char *errstr
, ...) {
601 va_start(ap
, errstr
);
602 vfprintf(stderr
, errstr
, ap
);
608 drawbar(Monitor
*m
) {
610 unsigned int i
, occ
= 0, urg
= 0;
614 for(c
= m
->clients
; c
; c
= c
->next
) {
624 buf
[0] = m
->screen_number
+ '0';
627 drawtext(buf
, selmon
== m
? dc
.sel
: dc
.norm
, True
);
630 #endif /* XINERAMA */
632 for(i
= 0; i
< LENGTH(tags
); i
++) {
633 dc
.w
= TEXTW(tags
[i
]);
634 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
635 drawtext(tags
[i
], col
, urg
& 1 << i
);
636 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
637 occ
& 1 << i
, urg
& 1 << i
, col
);
642 drawtext(lt
[m
->sellt
]->symbol
, dc
.norm
, False
);
654 drawtext(stext
, dc
.norm
, False
);
655 if((dc
.w
= dc
.x
- x
) > bh
) {
658 drawtext(selmon
->sel
->name
, dc
.sel
, False
);
659 drawsquare(selmon
->sel
->isfixed
, selmon
->sel
->isfloating
, False
, dc
.sel
);
662 drawtext(NULL
, dc
.norm
, False
);
668 drawtext(NULL
, dc
.norm
, False
);
670 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
678 for(m
= mons
; m
; m
= m
->next
)
683 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
686 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
688 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
689 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
690 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
694 r
.width
= r
.height
= x
+ 1;
695 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
698 r
.width
= r
.height
= x
;
699 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
704 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
706 int i
, x
, y
, h
, len
, olen
;
707 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
709 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
710 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
714 h
= dc
.font
.ascent
+ dc
.font
.descent
;
715 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
717 /* shorten text if necessary */
718 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
721 memcpy(buf
, text
, len
);
723 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
724 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
726 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
728 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
732 enternotify(XEvent
*e
) {
734 XCrossingEvent
*ev
= &e
->xcrossing
;
736 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
738 if((c
= getclient(ev
->window
)))
747 XExposeEvent
*ev
= &e
->xexpose
;
750 for(m
= mons
; m
; m
= m
->next
)
751 if(ev
->window
== m
->barwin
) {
759 if(!c
|| !ISVISIBLE((c
->mon
), c
))
760 for(c
= selmon
->stack
; c
&& !ISVISIBLE(selmon
, c
); c
= c
->snext
);
761 if(selmon
->sel
&& selmon
->sel
!= c
) {
762 grabbuttons(selmon
->sel
, False
);
763 XSetWindowBorder(dpy
, selmon
->sel
->win
, dc
.norm
[ColBorder
]);
770 grabbuttons(c
, True
);
771 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
772 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
775 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
781 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
782 XFocusChangeEvent
*ev
= &e
->xfocus
;
784 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
785 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
790 focusmon(const Arg
*arg
) {
794 for(i
= 0, m
= mons
; m
; m
= m
->next
, i
++)
802 #endif /* XINERAMA */
805 focusstack(const Arg
*arg
) {
806 Client
*c
= NULL
, *i
;
811 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(selmon
, c
); c
= c
->next
);
813 for(c
= selmon
->clients
; c
&& !ISVISIBLE(selmon
, c
); c
= c
->next
);
816 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
817 if(ISVISIBLE(selmon
, i
))
820 for(; i
; i
= i
->next
)
821 if(ISVISIBLE(selmon
, i
))
831 getclient(Window w
) {
835 for(m
= mons
; m
; m
= m
->next
)
836 for(c
= m
->clients
; c
&& c
->win
!= w
; c
= c
->next
);
841 getcolor(const char *colstr
) {
842 Colormap cmap
= DefaultColormap(dpy
, screen
);
845 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
846 die("error, cannot allocate color '%s'\n", colstr
);
854 unsigned char *p
= NULL
;
855 unsigned long n
, extra
;
858 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
859 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
860 if(status
!= Success
)
869 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
874 if(!text
|| size
== 0)
877 XGetTextProperty(dpy
, w
, &name
, atom
);
880 if(name
.encoding
== XA_STRING
)
881 strncpy(text
, (char *)name
.value
, size
- 1);
883 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
885 strncpy(text
, *list
, size
- 1);
886 XFreeStringList(list
);
889 text
[size
- 1] = '\0';
895 grabbuttons(Client
*c
, Bool focused
) {
899 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
900 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
902 for(i
= 0; i
< LENGTH(buttons
); i
++)
903 if(buttons
[i
].click
== ClkClientWin
)
904 for(j
= 0; j
< LENGTH(modifiers
); j
++)
905 XGrabButton(dpy
, buttons
[i
].button
,
906 buttons
[i
].mask
| modifiers
[j
],
907 c
->win
, False
, BUTTONMASK
,
908 GrabModeAsync
, GrabModeSync
, None
, None
);
910 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
911 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
920 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
923 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
924 for(i
= 0; i
< LENGTH(keys
); i
++) {
925 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
926 for(j
= 0; j
< LENGTH(modifiers
); j
++)
927 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
928 True
, GrabModeAsync
, GrabModeAsync
);
934 initfont(const char *fontstr
) {
935 char *def
, **missing
;
939 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
942 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
943 XFreeStringList(missing
);
946 XFontSetExtents
*font_extents
;
947 XFontStruct
**xfonts
;
949 dc
.font
.ascent
= dc
.font
.descent
= 0;
950 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
951 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
952 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
953 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
954 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
959 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
960 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
961 die("error, cannot load font: '%s'\n", fontstr
);
962 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
963 dc
.font
.descent
= dc
.font
.xfont
->descent
;
965 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
969 isprotodel(Client
*c
) {
974 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
975 for(i
= 0; !ret
&& i
< n
; i
++)
976 if(protocols
[i
] == wmatom
[WMDelete
])
984 keypress(XEvent
*e
) {
990 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
991 for(i
= 0; i
< LENGTH(keys
); i
++)
992 if(keysym
== keys
[i
].keysym
993 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
995 keys
[i
].func(&(keys
[i
].arg
));
999 killclient(const Arg
*arg
) {
1004 if(isprotodel(selmon
->sel
)) {
1005 ev
.type
= ClientMessage
;
1006 ev
.xclient
.window
= selmon
->sel
->win
;
1007 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1008 ev
.xclient
.format
= 32;
1009 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1010 ev
.xclient
.data
.l
[1] = CurrentTime
;
1011 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1014 XKillClient(dpy
, selmon
->sel
->win
);
1018 manage(Window w
, XWindowAttributes
*wa
) {
1020 Client
*c
, *t
= NULL
;
1021 Window trans
= None
;
1024 if(!(c
= malloc(sizeof(Client
))))
1025 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1035 c
->oldbw
= wa
->border_width
;
1036 if(c
->w
== sw
&& c
->h
== sh
) {
1042 if(c
->x
+ WIDTH(c
) > sx
+ sw
)
1043 c
->x
= sx
+ sw
- WIDTH(c
);
1044 if(c
->y
+ HEIGHT(c
) > sy
+ sh
)
1045 c
->y
= sy
+ sh
- HEIGHT(c
);
1046 c
->x
= MAX(c
->x
, sx
);
1047 /* only fix client y-offset, if the client center might cover the bar */
1048 c
->y
= MAX(c
->y
, ((selmon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= selmon
->wx
)
1049 && (c
->x
+ (c
->w
/ 2) < selmon
->wx
+ selmon
->ww
)) ? bh
: sy
);
1053 wc
.border_width
= c
->bw
;
1054 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1055 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1056 configure(c
); /* propagates border_width, if size doesn't change */
1058 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1059 grabbuttons(c
, False
);
1061 if(XGetTransientForHint(dpy
, w
, &trans
))
1062 t
= getclient(trans
);
1068 c
->isfloating
= trans
!= None
|| c
->isfixed
;
1070 XRaiseWindow(dpy
, c
->win
);
1073 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1074 XMapWindow(dpy
, c
->win
);
1075 setclientstate(c
, NormalState
);
1080 mappingnotify(XEvent
*e
) {
1081 XMappingEvent
*ev
= &e
->xmapping
;
1083 XRefreshKeyboardMapping(ev
);
1084 if(ev
->request
== MappingKeyboard
)
1089 maprequest(XEvent
*e
) {
1090 static XWindowAttributes wa
;
1091 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1093 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1095 if(wa
.override_redirect
)
1097 if(!getclient(ev
->window
))
1098 manage(ev
->window
, &wa
);
1102 monocle(Monitor
*m
) {
1105 for(c
= nexttiled(m
, selmon
->clients
); c
; c
= nexttiled(m
, c
->next
))
1106 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
);
1110 movemouse(const Arg
*arg
) {
1111 int x
, y
, ocx
, ocy
, di
, nx
, ny
;
1117 if(!(c
= selmon
->sel
))
1122 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1123 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1125 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
);
1127 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1129 case ConfigureRequest
:
1132 handler
[ev
.type
](&ev
);
1135 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1136 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1137 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1138 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1139 if(abs(selmon
->wx
- nx
) < snap
)
1141 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1142 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1143 if(abs(selmon
->wy
- ny
) < snap
)
1145 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1146 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1147 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1148 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1149 togglefloating(NULL
);
1151 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1152 resize(c
, nx
, ny
, c
->w
, c
->h
);
1156 while(ev
.type
!= ButtonRelease
);
1157 XUngrabPointer(dpy
, CurrentTime
);
1161 nexttiled(Monitor
*m
, Client
*c
) {
1163 for(; c
&& (c
->isfloating
|| !ISVISIBLE(m
, c
)); c
= c
->next
);
1168 propertynotify(XEvent
*e
) {
1171 XPropertyEvent
*ev
= &e
->xproperty
;
1173 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1175 else if(ev
->state
== PropertyDelete
)
1176 return; /* ignore */
1177 else if((c
= getclient(ev
->window
))) {
1180 case XA_WM_TRANSIENT_FOR
:
1181 XGetTransientForHint(dpy
, c
->win
, &trans
);
1182 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1185 case XA_WM_NORMAL_HINTS
:
1193 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1195 if(c
== selmon
->sel
)
1202 quit(const Arg
*arg
) {
1207 resize(Client
*c
, int x
, int y
, int w
, int h
) {
1210 if(applysizehints(c
, &x
, &y
, &w
, &h
)) {
1213 c
->w
= wc
.width
= w
;
1214 c
->h
= wc
.height
= h
;
1215 wc
.border_width
= c
->bw
;
1216 XConfigureWindow(dpy
, c
->win
,
1217 CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1224 resizemouse(const Arg
*arg
) {
1230 if(!(c
= selmon
->sel
))
1235 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1236 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1238 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1240 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1242 case ConfigureRequest
:
1245 handler
[ev
.type
](&ev
);
1248 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1249 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1251 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1252 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
) {
1253 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1254 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1255 togglefloating(NULL
);
1257 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1258 resize(c
, c
->x
, c
->y
, nw
, nh
);
1262 while(ev
.type
!= ButtonRelease
);
1263 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1264 XUngrabPointer(dpy
, CurrentTime
);
1265 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1269 restack(Monitor
*m
) {
1277 if(m
== selmon
&& (selmon
->sel
->isfloating
|| !lt
[m
->sellt
]->arrange
))
1278 XRaiseWindow(dpy
, selmon
->sel
->win
);
1279 if(lt
[m
->sellt
]->arrange
) {
1280 wc
.stack_mode
= Below
;
1281 wc
.sibling
= m
->barwin
;
1282 for(c
= m
->stack
; c
; c
= c
->snext
)
1283 if(!c
->isfloating
&& ISVISIBLE(m
, c
)) {
1284 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1285 wc
.sibling
= c
->win
;
1289 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1296 /* main event loop */
1298 while(running
&& !XNextEvent(dpy
, &ev
)) {
1299 if(handler
[ev
.type
])
1300 (handler
[ev
.type
])(&ev
); /* call handler */
1306 unsigned int i
, num
;
1307 Window d1
, d2
, *wins
= NULL
;
1308 XWindowAttributes wa
;
1310 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1311 for(i
= 0; i
< num
; i
++) {
1312 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1313 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1315 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1316 manage(wins
[i
], &wa
);
1318 for(i
= 0; i
< num
; i
++) { /* now the transients */
1319 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1321 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1322 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1323 manage(wins
[i
], &wa
);
1331 setclientstate(Client
*c
, long state
) {
1332 long data
[] = {state
, None
};
1334 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1335 PropModeReplace
, (unsigned char *)data
, 2);
1339 setlayout(const Arg
*arg
) {
1340 if(!arg
|| !arg
->v
|| arg
->v
!= lt
[selmon
->sellt
])
1343 lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1350 /* arg > 1.0 will set mfact absolutly */
1352 setmfact(const Arg
*arg
) {
1355 if(!arg
|| !lt
[selmon
->sellt
]->arrange
)
1357 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1358 if(f
< 0.1 || f
> 0.9)
1368 XSetWindowAttributes wa
;
1371 screen
= DefaultScreen(dpy
);
1372 root
= RootWindow(dpy
, screen
);
1376 sw
= DisplayWidth(dpy
, screen
);
1377 sh
= DisplayHeight(dpy
, screen
);
1378 bh
= dc
.h
= dc
.font
.height
+ 2;
1379 lt
[0] = &layouts
[0];
1380 lt
[1] = &layouts
[1 % LENGTH(layouts
)];
1384 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1385 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1386 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1387 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1388 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1391 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1392 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1393 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1395 /* init appearance */
1396 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1397 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1398 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1399 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1400 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1401 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1402 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1403 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1404 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1406 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1409 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1410 w
= TEXTW(layouts
[i
].symbol
);
1416 /* EWMH support per view */
1417 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1418 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1420 /* select for events */
1421 wa
.cursor
= cursor
[CurNormal
];
1422 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1423 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1424 |PropertyChangeMask
;
1425 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1426 XSelectInput(dpy
, root
, wa
.event_mask
);
1432 showhide(Client
*c
) {
1435 if(ISVISIBLE((c
->mon
), c
)) { /* show clients top down */
1436 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1437 if(!lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1438 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1441 else { /* hide clients bottom up */
1443 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1449 sigchld(int signal
) {
1450 while(0 < waitpid(-1, NULL
, WNOHANG
));
1454 spawn(const Arg
*arg
) {
1455 signal(SIGCHLD
, sigchld
);
1458 close(ConnectionNumber(dpy
));
1460 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1461 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1468 tag(const Arg
*arg
) {
1469 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1470 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1477 tagmon(const Arg
*arg
) {
1481 for(i
= 0, m
= mons
; m
; m
= m
->next
, i
++)
1488 #endif /* XINERAMA */
1491 textnw(const char *text
, unsigned int len
) {
1495 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1498 return XTextWidth(dc
.font
.xfont
, text
, len
);
1507 for(n
= 0, c
= nexttiled(m
, m
->clients
); c
; c
= nexttiled(m
, c
->next
), n
++);
1512 c
= nexttiled(m
, m
->clients
);
1513 mw
= m
->mfact
* m
->ww
;
1514 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
);
1520 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1522 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1527 for(i
= 0, c
= nexttiled(m
, c
->next
); c
; c
= nexttiled(m
, c
->next
), i
++) {
1528 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1529 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
));
1531 y
= c
->y
+ HEIGHT(c
);
1536 togglebar(const Arg
*arg
) {
1537 selmon
->showbar
= !selmon
->showbar
;
1538 updatebarpos(selmon
);
1539 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1544 togglefloating(const Arg
*arg
) {
1547 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1548 if(selmon
->sel
->isfloating
)
1549 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
, selmon
->sel
->w
, selmon
->sel
->h
);
1554 toggletag(const Arg
*arg
) {
1560 mask
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1562 selmon
->sel
->tags
= mask
;
1568 toggleview(const Arg
*arg
) {
1569 unsigned int mask
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1572 selmon
->tagset
[selmon
->seltags
] = mask
;
1578 unmanage(Client
*c
) {
1581 wc
.border_width
= c
->oldbw
;
1582 /* The server grab construct avoids race conditions. */
1584 XSetErrorHandler(xerrordummy
);
1585 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1588 if(selmon
->sel
== c
)
1590 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1591 setclientstate(c
, WithdrawnState
);
1594 XSetErrorHandler(xerror
);
1600 unmapnotify(XEvent
*e
) {
1602 XUnmapEvent
*ev
= &e
->xunmap
;
1604 if((c
= getclient(ev
->window
)))
1611 XSetWindowAttributes wa
;
1613 wa
.override_redirect
= True
;
1614 wa
.background_pixmap
= ParentRelative
;
1615 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1617 for(m
= mons
; m
; m
= m
->next
) {
1618 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1620 CopyFromParent
, DefaultVisual(dpy
, screen
),
1621 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1622 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1623 XMapRaised(dpy
, m
->barwin
);
1628 updatebarpos(Monitor
*m
) {
1633 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1634 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1644 Monitor
*newmons
= NULL
, *m
, *tm
;
1647 XineramaScreenInfo
*info
= NULL
;
1649 if(XineramaIsActive(dpy
))
1650 info
= XineramaQueryScreens(dpy
, &n
);
1652 /* allocate monitor(s) for the new geometry setup */
1653 for(i
= 0; i
< n
; i
++) {
1654 m
= (Monitor
*)malloc(sizeof(Monitor
));
1659 /* initialise monitor(s) */
1661 if(XineramaIsActive(dpy
)) {
1662 for(i
= 0, m
= newmons
; m
; m
= m
->next
, i
++) {
1663 m
->screen_number
= info
[i
].screen_number
;
1664 m
->wx
= info
[i
].x_org
;
1665 m
->my
= m
->wy
= info
[i
].y_org
;
1666 m
->ww
= info
[i
].width
;
1667 m
->mh
= m
->wh
= info
[i
].height
;
1673 /* default monitor setup */
1675 m
->screen_number
= 0;
1682 /* bar geometry setup */
1683 for(m
= newmons
; m
; m
= m
->next
) {
1684 /* TODO: consider removing the following values from config.h */
1690 m
->tagset
[0] = m
->tagset
[1] = 1;
1692 m
->showbar
= showbar
;
1695 /* reassign all clients with same screen number */
1696 for(tm
= mons
; tm
; tm
= tm
->next
)
1697 if(tm
->screen_number
== m
->screen_number
) {
1698 m
->clients
= tm
->clients
;
1699 m
->stack
= tm
->stack
;
1702 for(c
= m
->clients
; c
; c
= c
->next
)
1707 /* reassign left over clients of disappeared monitors */
1708 for(tm
= mons
; tm
; tm
= tm
->next
) {
1709 while(tm
->clients
) {
1710 c
= tm
->clients
->next
;
1711 tm
->clients
->next
= newmons
->clients
;
1712 tm
->clients
->mon
= newmons
;
1713 newmons
->clients
= tm
->clients
;
1717 c
= tm
->stack
->snext
;
1718 tm
->stack
->snext
= newmons
->stack
;
1719 newmons
->stack
= tm
->stack
;
1724 /* select focused monitor */
1730 if(XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
1731 for(m
= newmons
; m
; m
= m
->next
)
1732 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
)) {
1738 /* final assignment of new monitors */
1744 updatenumlockmask(void) {
1746 XModifierKeymap
*modmap
;
1749 modmap
= XGetModifierMapping(dpy
);
1750 for(i
= 0; i
< 8; i
++)
1751 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1752 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1753 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1754 numlockmask
= (1 << i
);
1755 XFreeModifiermap(modmap
);
1759 updatesizehints(Client
*c
) {
1763 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1764 /* size is uninitialized, ensure that size.flags aren't used */
1766 if(size
.flags
& PBaseSize
) {
1767 c
->basew
= size
.base_width
;
1768 c
->baseh
= size
.base_height
;
1770 else if(size
.flags
& PMinSize
) {
1771 c
->basew
= size
.min_width
;
1772 c
->baseh
= size
.min_height
;
1775 c
->basew
= c
->baseh
= 0;
1776 if(size
.flags
& PResizeInc
) {
1777 c
->incw
= size
.width_inc
;
1778 c
->inch
= size
.height_inc
;
1781 c
->incw
= c
->inch
= 0;
1782 if(size
.flags
& PMaxSize
) {
1783 c
->maxw
= size
.max_width
;
1784 c
->maxh
= size
.max_height
;
1787 c
->maxw
= c
->maxh
= 0;
1788 if(size
.flags
& PMinSize
) {
1789 c
->minw
= size
.min_width
;
1790 c
->minh
= size
.min_height
;
1792 else if(size
.flags
& PBaseSize
) {
1793 c
->minw
= size
.base_width
;
1794 c
->minh
= size
.base_height
;
1797 c
->minw
= c
->minh
= 0;
1798 if(size
.flags
& PAspect
) {
1799 c
->mina
= (float)size
.min_aspect
.y
/ (float)size
.min_aspect
.x
;
1800 c
->maxa
= (float)size
.max_aspect
.x
/ (float)size
.max_aspect
.y
;
1803 c
->maxa
= c
->mina
= 0.0;
1804 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1805 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1809 updatetitle(Client
*c
) {
1810 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1811 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1816 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1817 strcpy(stext
, "dwm-"VERSION
);
1822 updatewmhints(Client
*c
) {
1825 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1826 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1827 wmh
->flags
&= ~XUrgencyHint
;
1828 XSetWMHints(dpy
, c
->win
, wmh
);
1831 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1838 view(const Arg
*arg
) {
1839 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1841 selmon
->seltags
^= 1; /* toggle sel tagset */
1842 if(arg
->ui
& TAGMASK
)
1843 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1847 /* There's no way to check accesses to destroyed windows, thus those cases are
1848 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1849 * default error handler, which may call exit. */
1851 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1852 if(ee
->error_code
== BadWindow
1853 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1854 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1855 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1856 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1857 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1858 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1859 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1860 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1862 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1863 ee
->request_code
, ee
->error_code
);
1864 return xerrorxlib(dpy
, ee
); /* may call exit */
1868 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1872 /* Startup Error handler to check if another window manager
1873 * is already running. */
1875 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1881 zoom(const Arg
*arg
) {
1882 Client
*c
= selmon
->sel
;
1884 if(!lt
[selmon
->sellt
]->arrange
|| lt
[selmon
->sellt
]->arrange
== monocle
|| (selmon
->sel
&& selmon
->sel
->isfloating
))
1886 if(c
== nexttiled(selmon
, selmon
->clients
))
1887 if(!c
|| !(c
= nexttiled(selmon
, c
->next
)))
1896 main(int argc
, char *argv
[]) {
1897 if(argc
== 2 && !strcmp("-v", argv
[1]))
1898 die("dwm-"VERSION
", © 2006-2009 dwm engineers, see LICENSE for details\n");
1900 die("usage: dwm [-v]\n");
1902 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1903 fputs("warning: no locale support\n", stderr
);
1905 if(!(dpy
= XOpenDisplay(NULL
)))
1906 die("dwm: cannot open display\n");