Xinqi Bao's Git
096895c4617019542c64886f0d25a8c659e84f49
1 //#define XINULATOR /* debug, simulates dual head */
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(C) ((C->tags & C->mon->tagset[C->mon->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(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 unfocus(Client
*c
);
215 static void unmanage(Client
*c
);
216 static void unmapnotify(XEvent
*e
);
217 static void updategeom(void);
218 static void updatebarpos(Monitor
*m
);
219 static void updatebars(void);
220 static void updatenumlockmask(void);
221 static void updatesizehints(Client
*c
);
222 static void updatestatus(void);
223 static void updatetitle(Client
*c
);
224 static void updatewmhints(Client
*c
);
225 static void view(const Arg
*arg
);
226 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
227 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
228 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
229 static void zoom(const Arg
*arg
);
231 static void focusmon(const Arg
*arg
);
232 static void tagmon(const Arg
*arg
);
233 #endif /* XINERAMA */
236 static char stext
[256];
238 static int sx
, sy
, sw
, sh
; /* X display screen geometry x, y, width, height */
239 static int bh
, blw
= 0; /* bar geometry */
240 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
241 static unsigned int numlockmask
= 0;
242 static void (*handler
[LASTEvent
]) (XEvent
*) = {
243 [ButtonPress
] = buttonpress
,
244 [ConfigureRequest
] = configurerequest
,
245 [ConfigureNotify
] = configurenotify
,
246 [DestroyNotify
] = destroynotify
,
247 [EnterNotify
] = enternotify
,
250 [KeyPress
] = keypress
,
251 [MappingNotify
] = mappingnotify
,
252 [MapRequest
] = maprequest
,
253 [PropertyNotify
] = propertynotify
,
254 [UnmapNotify
] = unmapnotify
256 static Atom wmatom
[WMLast
], netatom
[NetLast
];
258 static Bool running
= True
;
259 static Cursor cursor
[CurLast
];
262 static Layout
*lt
[] = { NULL
, NULL
};
263 static Monitor
*mons
= NULL
, *selmon
= NULL
;
265 /* configuration, allows nested code to access above variables */
268 /* compile-time check if all tags fit into an unsigned int bit array. */
269 struct NumTags
{ char limitexceeded
[sizeof(unsigned int) * 8 < LENGTH(tags
) ? -1 : 1]; };
271 /* function implementations */
273 applyrules(Client
*c
) {
276 XClassHint ch
= { 0 };
279 c
->isfloating
= c
->tags
= 0;
280 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
281 for(i
= 0; i
< LENGTH(rules
); i
++) {
283 if((!r
->title
|| strstr(c
->name
, r
->title
))
284 && (!r
->class || (ch
.res_class
&& strstr(ch
.res_class
, r
->class)))
285 && (!r
->instance
|| (ch
.res_name
&& strstr(ch
.res_name
, r
->instance
)))) {
286 c
->isfloating
= r
->isfloating
;
295 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
299 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
) {
302 /* set minimum possible */
310 if(*x
+ *w
+ 2 * c
->bw
< sx
)
312 if(*y
+ *h
+ 2 * c
->bw
< sy
)
319 if(resizehints
|| c
->isfloating
) {
320 /* see last two sentences in ICCCM 4.1.2.3 */
321 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
323 if(!baseismin
) { /* temporarily remove base dimensions */
328 /* adjust for aspect limits */
329 if(c
->mina
> 0 && c
->maxa
> 0) {
330 if(c
->maxa
< (float)*w
/ *h
)
332 else if(c
->mina
< (float)*h
/ *w
)
336 if(baseismin
) { /* increment calculation requires this */
341 /* adjust for increment value */
347 /* restore base dimensions */
351 *w
= MAX(*w
, c
->minw
);
352 *h
= MAX(*h
, c
->minh
);
355 *w
= MIN(*w
, c
->maxw
);
358 *h
= MIN(*h
, c
->maxh
);
360 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
367 /* optimise two loops into one, check focus(NULL) */
368 for(m
= mons
; m
; m
= m
->next
)
371 for(m
= mons
; m
; m
= m
->next
) {
372 if(lt
[m
->sellt
]->arrange
)
373 lt
[m
->sellt
]->arrange(m
);
380 c
->next
= c
->mon
->clients
;
385 attachstack(Client
*c
) {
386 c
->snext
= c
->mon
->stack
;
391 buttonpress(XEvent
*e
) {
392 unsigned int i
, x
, click
;
396 XButtonPressedEvent
*ev
= &e
->xbutton
;
399 /* focus monitor if necessary */
400 for(m
= mons
; m
; m
= m
->next
)
401 if(ev
->window
== m
->barwin
) {
403 unfocus(selmon
->stack
);
409 if(ev
->window
== selmon
->barwin
&& ev
->x
>= selmon
->btx
) {
414 while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
415 if(i
< LENGTH(tags
)) {
419 else if(ev
->x
< x
+ blw
)
421 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
422 click
= ClkStatusText
;
426 else if((c
= getclient(ev
->window
))) {
428 click
= ClkClientWin
;
431 for(i
= 0; i
< LENGTH(buttons
); i
++)
432 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
433 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
434 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
440 xerrorxlib
= XSetErrorHandler(xerrorstart
);
442 /* this causes an error if some other window manager is running */
443 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
446 die("dwm: another window manager is already running\n");
447 XSetErrorHandler(xerror
);
454 Layout foo
= { "", NULL
};
458 lt
[selmon
->sellt
] = &foo
;
460 /* TODO: consider simplifying cleanup code of the stack, perhaps do that in cleanmons() ? */
461 for(m
= mons
; m
; m
= m
->next
)
465 XFreeFontSet(dpy
, dc
.font
.set
);
467 XFreeFont(dpy
, dc
.font
.xfont
);
468 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
469 XFreePixmap(dpy
, dc
.drawable
);
471 XFreeCursor(dpy
, cursor
[CurNormal
]);
472 XFreeCursor(dpy
, cursor
[CurResize
]);
473 XFreeCursor(dpy
, cursor
[CurMove
]);
476 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
485 XUnmapWindow(dpy
, mons
->barwin
);
486 XDestroyWindow(dpy
, mons
->barwin
);
493 clearurgent(Client
*c
) {
497 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
499 wmh
->flags
&= ~XUrgencyHint
;
500 XSetWMHints(dpy
, c
->win
, wmh
);
505 configure(Client
*c
) {
508 ce
.type
= ConfigureNotify
;
516 ce
.border_width
= c
->bw
;
518 ce
.override_redirect
= False
;
519 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
523 configurenotify(XEvent
*e
) {
525 XConfigureEvent
*ev
= &e
->xconfigure
;
527 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
532 XFreePixmap(dpy
, dc
.drawable
);
533 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
535 for(m
= mons
; m
; m
= m
->next
)
536 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
542 configurerequest(XEvent
*e
) {
544 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
547 if((c
= getclient(ev
->window
))) {
548 if(ev
->value_mask
& CWBorderWidth
)
549 c
->bw
= ev
->border_width
;
550 else if(c
->isfloating
|| !lt
[selmon
->sellt
]->arrange
) {
551 if(ev
->value_mask
& CWX
)
553 if(ev
->value_mask
& CWY
)
555 if(ev
->value_mask
& CWWidth
)
557 if(ev
->value_mask
& CWHeight
)
559 if((c
->x
- sx
+ c
->w
) > sw
&& c
->isfloating
)
560 c
->x
= sx
+ (sw
/ 2 - c
->w
/ 2); /* center in x direction */
561 if((c
->y
- sy
+ c
->h
) > sh
&& c
->isfloating
)
562 c
->y
= sy
+ (sh
/ 2 - c
->h
/ 2); /* center in y direction */
563 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
566 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
574 wc
.width
= ev
->width
;
575 wc
.height
= ev
->height
;
576 wc
.border_width
= ev
->border_width
;
577 wc
.sibling
= ev
->above
;
578 wc
.stack_mode
= ev
->detail
;
579 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
585 destroynotify(XEvent
*e
) {
587 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
589 if((c
= getclient(ev
->window
)))
597 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
602 detachstack(Client
*c
) {
605 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
610 die(const char *errstr
, ...) {
613 va_start(ap
, errstr
);
614 vfprintf(stderr
, errstr
, ap
);
620 drawbar(Monitor
*m
) {
622 unsigned int i
, occ
= 0, urg
= 0;
626 for(c
= m
->clients
; c
; c
= c
->next
) {
636 buf
[0] = m
->screen_number
+ '0';
639 drawtext(buf
, selmon
== m
? dc
.sel
: dc
.norm
, True
);
642 #endif /* XINERAMA */
644 for(i
= 0; i
< LENGTH(tags
); i
++) {
645 dc
.w
= TEXTW(tags
[i
]);
646 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
647 drawtext(tags
[i
], col
, urg
& 1 << i
);
648 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
649 occ
& 1 << i
, urg
& 1 << i
, col
);
654 drawtext(lt
[m
->sellt
]->symbol
, dc
.norm
, False
);
659 if(m
== selmon
) { /* status is only drawn on selected monitor */
666 drawtext(stext
, dc
.norm
, False
);
671 if((dc
.w
= dc
.x
- x
) > bh
) {
674 col
= m
== selmon
? dc
.sel
: dc
.norm
;
675 drawtext(m
->sel
->name
, col
, False
);
676 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
679 drawtext(NULL
, dc
.norm
, False
);
681 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
689 for(m
= mons
; m
; m
= m
->next
)
694 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
697 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
699 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
700 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
701 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
705 r
.width
= r
.height
= x
+ 1;
706 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
709 r
.width
= r
.height
= x
;
710 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
715 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
717 int i
, x
, y
, h
, len
, olen
;
718 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
720 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
721 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
725 h
= dc
.font
.ascent
+ dc
.font
.descent
;
726 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
728 /* shorten text if necessary */
729 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
732 memcpy(buf
, text
, len
);
734 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
735 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
737 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
739 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
743 enternotify(XEvent
*e
) {
745 XCrossingEvent
*ev
= &e
->xcrossing
;
747 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
749 if((c
= getclient(ev
->window
)))
758 XExposeEvent
*ev
= &e
->xexpose
;
761 for(m
= mons
; m
; m
= m
->next
)
762 if(ev
->window
== m
->barwin
) {
770 if(!c
|| !ISVISIBLE(c
))
771 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
773 unfocus(selmon
->sel
);
781 grabbuttons(c
, True
);
782 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
783 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
786 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
792 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
793 XFocusChangeEvent
*ev
= &e
->xfocus
;
795 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
796 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
801 focusmon(const Arg
*arg
) {
805 for(i
= 0, m
= mons
; m
; m
= m
->next
, i
++)
809 unfocus(selmon
->sel
);
816 #endif /* XINERAMA */
819 focusstack(const Arg
*arg
) {
820 Client
*c
= NULL
, *i
;
825 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
827 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
830 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
834 for(; i
; i
= i
->next
)
845 getclient(Window w
) {
849 for(m
= mons
; m
; m
= m
->next
)
850 for(c
= m
->clients
; c
; c
= c
->next
)
857 getcolor(const char *colstr
) {
858 Colormap cmap
= DefaultColormap(dpy
, screen
);
861 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
862 die("error, cannot allocate color '%s'\n", colstr
);
870 unsigned char *p
= NULL
;
871 unsigned long n
, extra
;
874 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
875 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
876 if(status
!= Success
)
885 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
890 if(!text
|| size
== 0)
893 XGetTextProperty(dpy
, w
, &name
, atom
);
896 if(name
.encoding
== XA_STRING
)
897 strncpy(text
, (char *)name
.value
, size
- 1);
899 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
901 strncpy(text
, *list
, size
- 1);
902 XFreeStringList(list
);
905 text
[size
- 1] = '\0';
911 grabbuttons(Client
*c
, Bool focused
) {
915 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
916 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
918 for(i
= 0; i
< LENGTH(buttons
); i
++)
919 if(buttons
[i
].click
== ClkClientWin
)
920 for(j
= 0; j
< LENGTH(modifiers
); j
++)
921 XGrabButton(dpy
, buttons
[i
].button
,
922 buttons
[i
].mask
| modifiers
[j
],
923 c
->win
, False
, BUTTONMASK
,
924 GrabModeAsync
, GrabModeSync
, None
, None
);
926 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
927 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
936 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
939 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
940 for(i
= 0; i
< LENGTH(keys
); i
++) {
941 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
942 for(j
= 0; j
< LENGTH(modifiers
); j
++)
943 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
944 True
, GrabModeAsync
, GrabModeAsync
);
950 initfont(const char *fontstr
) {
951 char *def
, **missing
;
955 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
958 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
959 XFreeStringList(missing
);
962 XFontSetExtents
*font_extents
;
963 XFontStruct
**xfonts
;
965 dc
.font
.ascent
= dc
.font
.descent
= 0;
966 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
967 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
968 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
969 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
970 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
975 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
976 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
977 die("error, cannot load font: '%s'\n", fontstr
);
978 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
979 dc
.font
.descent
= dc
.font
.xfont
->descent
;
981 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
985 isprotodel(Client
*c
) {
990 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
991 for(i
= 0; !ret
&& i
< n
; i
++)
992 if(protocols
[i
] == wmatom
[WMDelete
])
1000 keypress(XEvent
*e
) {
1006 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1007 for(i
= 0; i
< LENGTH(keys
); i
++)
1008 if(keysym
== keys
[i
].keysym
1009 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1011 keys
[i
].func(&(keys
[i
].arg
));
1015 killclient(const Arg
*arg
) {
1020 if(isprotodel(selmon
->sel
)) {
1021 ev
.type
= ClientMessage
;
1022 ev
.xclient
.window
= selmon
->sel
->win
;
1023 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1024 ev
.xclient
.format
= 32;
1025 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1026 ev
.xclient
.data
.l
[1] = CurrentTime
;
1027 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1030 XKillClient(dpy
, selmon
->sel
->win
);
1034 manage(Window w
, XWindowAttributes
*wa
) {
1036 Client
*c
, *t
= NULL
;
1037 Window trans
= None
;
1040 if(!(c
= malloc(sizeof(Client
))))
1041 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1051 c
->oldbw
= wa
->border_width
;
1052 if(c
->w
== sw
&& c
->h
== sh
) {
1058 if(c
->x
+ WIDTH(c
) > sx
+ sw
)
1059 c
->x
= sx
+ sw
- WIDTH(c
);
1060 if(c
->y
+ HEIGHT(c
) > sy
+ sh
)
1061 c
->y
= sy
+ sh
- HEIGHT(c
);
1062 c
->x
= MAX(c
->x
, sx
);
1063 /* only fix client y-offset, if the client center might cover the bar */
1064 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1065 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: sy
);
1069 wc
.border_width
= c
->bw
;
1070 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1071 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1072 configure(c
); /* propagates border_width, if size doesn't change */
1074 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1075 grabbuttons(c
, False
);
1077 if(XGetTransientForHint(dpy
, w
, &trans
))
1078 t
= getclient(trans
);
1084 c
->isfloating
= trans
!= None
|| c
->isfixed
;
1086 XRaiseWindow(dpy
, c
->win
);
1089 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1090 XMapWindow(dpy
, c
->win
);
1091 setclientstate(c
, NormalState
);
1096 mappingnotify(XEvent
*e
) {
1097 XMappingEvent
*ev
= &e
->xmapping
;
1099 XRefreshKeyboardMapping(ev
);
1100 if(ev
->request
== MappingKeyboard
)
1105 maprequest(XEvent
*e
) {
1106 static XWindowAttributes wa
;
1107 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1109 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1111 if(wa
.override_redirect
)
1113 if(!getclient(ev
->window
))
1114 manage(ev
->window
, &wa
);
1118 monocle(Monitor
*m
) {
1121 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1122 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
);
1126 movemouse(const Arg
*arg
) {
1127 int x
, y
, ocx
, ocy
, di
, nx
, ny
;
1133 if(!(c
= selmon
->sel
))
1138 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1139 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1141 XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
);
1143 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1145 case ConfigureRequest
:
1148 handler
[ev
.type
](&ev
);
1151 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1152 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1153 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1154 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1155 if(abs(selmon
->wx
- nx
) < snap
)
1157 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1158 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1159 if(abs(selmon
->wy
- ny
) < snap
)
1161 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1162 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1163 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1164 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1165 togglefloating(NULL
);
1167 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1168 resize(c
, nx
, ny
, c
->w
, c
->h
);
1172 while(ev
.type
!= ButtonRelease
);
1173 XUngrabPointer(dpy
, CurrentTime
);
1177 nexttiled(Client
*c
) {
1178 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1183 propertynotify(XEvent
*e
) {
1186 XPropertyEvent
*ev
= &e
->xproperty
;
1188 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1190 else if(ev
->state
== PropertyDelete
)
1191 return; /* ignore */
1192 else if((c
= getclient(ev
->window
))) {
1195 case XA_WM_TRANSIENT_FOR
:
1196 XGetTransientForHint(dpy
, c
->win
, &trans
);
1197 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1200 case XA_WM_NORMAL_HINTS
:
1208 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1210 if(c
== selmon
->sel
)
1217 quit(const Arg
*arg
) {
1222 resize(Client
*c
, int x
, int y
, int w
, int h
) {
1225 if(applysizehints(c
, &x
, &y
, &w
, &h
)) {
1228 c
->w
= wc
.width
= w
;
1229 c
->h
= wc
.height
= h
;
1230 wc
.border_width
= c
->bw
;
1231 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1238 resizemouse(const Arg
*arg
) {
1244 if(!(c
= selmon
->sel
))
1249 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1250 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1252 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1254 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1256 case ConfigureRequest
:
1259 handler
[ev
.type
](&ev
);
1262 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1263 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1265 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1266 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
) {
1267 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1268 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1269 togglefloating(NULL
);
1271 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1272 resize(c
, c
->x
, c
->y
, nw
, nh
);
1276 while(ev
.type
!= ButtonRelease
);
1277 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1278 XUngrabPointer(dpy
, CurrentTime
);
1279 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1283 restack(Monitor
*m
) {
1291 if(m
->sel
->isfloating
|| !lt
[m
->sellt
]->arrange
)
1292 XRaiseWindow(dpy
, m
->sel
->win
);
1293 if(lt
[m
->sellt
]->arrange
) {
1294 wc
.stack_mode
= Below
;
1295 wc
.sibling
= m
->barwin
;
1296 for(c
= m
->stack
; c
; c
= c
->snext
)
1297 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1298 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1299 wc
.sibling
= c
->win
;
1303 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1310 /* main event loop */
1312 while(running
&& !XNextEvent(dpy
, &ev
)) {
1313 if(handler
[ev
.type
])
1314 (handler
[ev
.type
])(&ev
); /* call handler */
1320 unsigned int i
, num
;
1321 Window d1
, d2
, *wins
= NULL
;
1322 XWindowAttributes wa
;
1324 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1325 for(i
= 0; i
< num
; i
++) {
1326 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1327 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1329 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1330 manage(wins
[i
], &wa
);
1332 for(i
= 0; i
< num
; i
++) { /* now the transients */
1333 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1335 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1336 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1337 manage(wins
[i
], &wa
);
1345 setclientstate(Client
*c
, long state
) {
1346 long data
[] = {state
, None
};
1348 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1349 PropModeReplace
, (unsigned char *)data
, 2);
1353 setlayout(const Arg
*arg
) {
1354 if(!arg
|| !arg
->v
|| arg
->v
!= lt
[selmon
->sellt
])
1357 lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1364 /* arg > 1.0 will set mfact absolutly */
1366 setmfact(const Arg
*arg
) {
1369 if(!arg
|| !lt
[selmon
->sellt
]->arrange
)
1371 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1372 if(f
< 0.1 || f
> 0.9)
1382 XSetWindowAttributes wa
;
1385 screen
= DefaultScreen(dpy
);
1386 root
= RootWindow(dpy
, screen
);
1390 sw
= DisplayWidth(dpy
, screen
);
1391 sh
= DisplayHeight(dpy
, screen
);
1392 bh
= dc
.h
= dc
.font
.height
+ 2;
1393 lt
[0] = &layouts
[0];
1394 lt
[1] = &layouts
[1 % LENGTH(layouts
)];
1398 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1399 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1400 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1401 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1402 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1405 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1406 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1407 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1409 /* init appearance */
1410 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1411 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1412 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1413 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1414 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1415 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1416 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1417 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1418 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1420 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1423 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1424 w
= TEXTW(layouts
[i
].symbol
);
1430 /* EWMH support per view */
1431 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1432 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1434 /* select for events */
1435 wa
.cursor
= cursor
[CurNormal
];
1436 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1437 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1438 |PropertyChangeMask
;
1439 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1440 XSelectInput(dpy
, root
, wa
.event_mask
);
1446 showhide(Client
*c
) {
1449 if(ISVISIBLE(c
)) { /* show clients top down */
1450 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1451 if(!lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1452 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1455 else { /* hide clients bottom up */
1457 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1463 sigchld(int signal
) {
1464 while(0 < waitpid(-1, NULL
, WNOHANG
));
1468 spawn(const Arg
*arg
) {
1469 signal(SIGCHLD
, sigchld
);
1472 close(ConnectionNumber(dpy
));
1474 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1475 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1482 tag(const Arg
*arg
) {
1483 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1484 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1491 tagmon(const Arg
*arg
) {
1496 if(!(c
= selmon
->sel
))
1498 for(i
= 0, m
= mons
; m
; m
= m
->next
, i
++)
1503 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1511 #endif /* XINERAMA */
1514 textnw(const char *text
, unsigned int len
) {
1518 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1521 return XTextWidth(dc
.font
.xfont
, text
, len
);
1530 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1535 c
= nexttiled(m
->clients
);
1536 mw
= m
->mfact
* m
->ww
;
1537 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
);
1543 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1545 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1550 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1551 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1552 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
));
1554 y
= c
->y
+ HEIGHT(c
);
1559 togglebar(const Arg
*arg
) {
1560 selmon
->showbar
= !selmon
->showbar
;
1561 updatebarpos(selmon
);
1562 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1567 togglefloating(const Arg
*arg
) {
1570 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1571 if(selmon
->sel
->isfloating
)
1572 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
, selmon
->sel
->w
, selmon
->sel
->h
);
1577 toggletag(const Arg
*arg
) {
1583 mask
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1585 selmon
->sel
->tags
= mask
;
1591 toggleview(const Arg
*arg
) {
1592 unsigned int mask
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1595 selmon
->tagset
[selmon
->seltags
] = mask
;
1601 unfocus(Client
*c
) {
1604 grabbuttons(c
, False
);
1605 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1606 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1610 unmanage(Client
*c
) {
1613 wc
.border_width
= c
->oldbw
;
1614 /* The server grab construct avoids race conditions. */
1616 XSetErrorHandler(xerrordummy
);
1617 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1620 if(c
->mon
->sel
== c
) {
1621 /* TODO: consider separate the next code into a function or into detachstack? */
1623 for(tc
= c
->mon
->stack
; tc
&& !ISVISIBLE(tc
); tc
= tc
->snext
);
1627 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1628 setclientstate(c
, WithdrawnState
);
1631 XSetErrorHandler(xerror
);
1637 unmapnotify(XEvent
*e
) {
1639 XUnmapEvent
*ev
= &e
->xunmap
;
1641 if((c
= getclient(ev
->window
)))
1648 XSetWindowAttributes wa
;
1650 wa
.override_redirect
= True
;
1651 wa
.background_pixmap
= ParentRelative
;
1652 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1654 for(m
= mons
; m
; m
= m
->next
) {
1655 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1657 CopyFromParent
, DefaultVisual(dpy
, screen
),
1658 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1659 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1660 XMapRaised(dpy
, m
->barwin
);
1665 updatebarpos(Monitor
*m
) {
1670 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1671 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1679 int i
, di
, n
= 1, x
, y
;
1682 Monitor
*newmons
= NULL
, *m
, *tm
;
1687 #elif defined(XINERAMA)
1688 XineramaScreenInfo
*info
= NULL
;
1690 if(XineramaIsActive(dpy
))
1691 info
= XineramaQueryScreens(dpy
, &n
);
1693 /* allocate monitor(s) for the new geometry setup */
1694 for(i
= 0; i
< n
; i
++) {
1695 m
= (Monitor
*)malloc(sizeof(Monitor
));
1700 /* initialise monitor(s) */
1704 m
->screen_number
= 0;
1708 m
->mh
= m
->wh
= sh
/ 2;
1710 m
->screen_number
= 1;
1712 m
->my
= m
->wy
= sy
+ sh
/ 2;
1714 m
->mh
= m
->wh
= sh
/ 2;
1717 #elif defined(XINERAMA)
1718 if(XineramaIsActive(dpy
)) {
1719 for(i
= 0, m
= newmons
; m
; m
= m
->next
, i
++) {
1720 m
->screen_number
= info
[i
].screen_number
;
1721 m
->wx
= info
[i
].x_org
;
1722 m
->my
= m
->wy
= info
[i
].y_org
;
1723 m
->ww
= info
[i
].width
;
1724 m
->mh
= m
->wh
= info
[i
].height
;
1730 /* default monitor setup */
1732 m
->screen_number
= 0;
1739 /* bar geometry setup */
1740 for(m
= newmons
; m
; m
= m
->next
) {
1741 /* TODO: consider removing the following values from config.h */
1747 m
->tagset
[0] = m
->tagset
[1] = 1;
1749 m
->showbar
= showbar
;
1754 /* reassign left over clients of disappeared monitors */
1755 for(tm
= mons
; tm
; tm
= tm
->next
)
1756 while(tm
->clients
) {
1758 tm
->clients
= c
->next
;
1765 /* select focused monitor */
1767 if(XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
1768 for(m
= newmons
; m
; m
= m
->next
)
1769 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
)) {
1774 /* final assignment of new monitors */
1780 updatenumlockmask(void) {
1782 XModifierKeymap
*modmap
;
1785 modmap
= XGetModifierMapping(dpy
);
1786 for(i
= 0; i
< 8; i
++)
1787 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1788 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1789 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1790 numlockmask
= (1 << i
);
1791 XFreeModifiermap(modmap
);
1795 updatesizehints(Client
*c
) {
1799 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1800 /* size is uninitialized, ensure that size.flags aren't used */
1802 if(size
.flags
& PBaseSize
) {
1803 c
->basew
= size
.base_width
;
1804 c
->baseh
= size
.base_height
;
1806 else if(size
.flags
& PMinSize
) {
1807 c
->basew
= size
.min_width
;
1808 c
->baseh
= size
.min_height
;
1811 c
->basew
= c
->baseh
= 0;
1812 if(size
.flags
& PResizeInc
) {
1813 c
->incw
= size
.width_inc
;
1814 c
->inch
= size
.height_inc
;
1817 c
->incw
= c
->inch
= 0;
1818 if(size
.flags
& PMaxSize
) {
1819 c
->maxw
= size
.max_width
;
1820 c
->maxh
= size
.max_height
;
1823 c
->maxw
= c
->maxh
= 0;
1824 if(size
.flags
& PMinSize
) {
1825 c
->minw
= size
.min_width
;
1826 c
->minh
= size
.min_height
;
1828 else if(size
.flags
& PBaseSize
) {
1829 c
->minw
= size
.base_width
;
1830 c
->minh
= size
.base_height
;
1833 c
->minw
= c
->minh
= 0;
1834 if(size
.flags
& PAspect
) {
1835 c
->mina
= (float)size
.min_aspect
.y
/ (float)size
.min_aspect
.x
;
1836 c
->maxa
= (float)size
.max_aspect
.x
/ (float)size
.max_aspect
.y
;
1839 c
->maxa
= c
->mina
= 0.0;
1840 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1841 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1845 updatetitle(Client
*c
) {
1846 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1847 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1852 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1853 strcpy(stext
, "dwm-"VERSION
);
1858 updatewmhints(Client
*c
) {
1861 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1862 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1863 wmh
->flags
&= ~XUrgencyHint
;
1864 XSetWMHints(dpy
, c
->win
, wmh
);
1867 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1874 view(const Arg
*arg
) {
1875 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1877 selmon
->seltags
^= 1; /* toggle sel tagset */
1878 if(arg
->ui
& TAGMASK
)
1879 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1883 /* There's no way to check accesses to destroyed windows, thus those cases are
1884 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1885 * default error handler, which may call exit. */
1887 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1888 if(ee
->error_code
== BadWindow
1889 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1890 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1891 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1892 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1893 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1894 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1895 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1896 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1898 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1899 ee
->request_code
, ee
->error_code
);
1900 return xerrorxlib(dpy
, ee
); /* may call exit */
1904 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1908 /* Startup Error handler to check if another window manager
1909 * is already running. */
1911 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1917 zoom(const Arg
*arg
) {
1918 Client
*c
= selmon
->sel
;
1920 if(!lt
[selmon
->sellt
]->arrange
|| lt
[selmon
->sellt
]->arrange
== monocle
|| (selmon
->sel
&& selmon
->sel
->isfloating
))
1922 if(c
== nexttiled(selmon
->clients
))
1923 if(!c
|| !(c
= nexttiled(c
->next
)))
1932 main(int argc
, char *argv
[]) {
1933 if(argc
== 2 && !strcmp("-v", argv
[1]))
1934 die("dwm-"VERSION
", © 2006-2009 dwm engineers, see LICENSE for details\n");
1936 die("usage: dwm [-v]\n");
1938 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1939 fputs("warning: no locale support\n", stderr
);
1941 if(!(dpy
= XOpenDisplay(NULL
)))
1942 die("dwm: cannot open display\n");