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 * The event handlers of dwm are organized in an array which is accessed
10 * whenever a new event has been fetched. This allows event dispatching
13 * Each child of the root window is called a client, except windows which have
14 * set the override_redirect flag. Clients are organized in a linked client
15 * list on each monitor, the focus history is remembered through a stack list
16 * on each monitor. Each client contains a bit array to indicate the tags of a
19 * Keys and tagging rules are organized as arrays and defined in config.h.
21 * To understand everything else, start reading main().
31 #include <sys/types.h>
33 #include <X11/cursorfont.h>
34 #include <X11/keysym.h>
35 #include <X11/Xatom.h>
37 #include <X11/Xproto.h>
38 #include <X11/Xutil.h>
40 #include <X11/extensions/Xinerama.h>
42 #include <X11/Xft/Xft.h>
48 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
49 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
50 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
51 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
52 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
53 #define LENGTH(X) (sizeof X / sizeof X[0])
54 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
55 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
56 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
57 #define TAGMASK ((1 << LENGTH(tags)) - 1)
58 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
60 #define SYSTEM_TRAY_REQUEST_DOCK 0
63 #define XEMBED_EMBEDDED_NOTIFY 0
64 #define XEMBED_WINDOW_ACTIVATE 1
65 #define XEMBED_FOCUS_IN 4
66 #define XEMBED_MODALITY_ON 10
68 #define XEMBED_MAPPED (1 << 0)
69 #define XEMBED_WINDOW_ACTIVATE 1
70 #define XEMBED_WINDOW_DEACTIVATE 2
72 #define VERSION_MAJOR 0
73 #define VERSION_MINOR 0
74 #define XEMBED_EMBEDDED_VERSION (VERSION_MAJOR << 16) | VERSION_MINOR
77 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
78 enum { SchemeNorm
, SchemeSel
}; /* color schemes */
79 enum { NetSupported
, NetWMName
, NetWMState
, NetWMCheck
,
80 NetSystemTray
, NetSystemTrayOP
, NetSystemTrayOrientation
, NetSystemTrayOrientationHorz
,
81 NetWMFullscreen
, NetActiveWindow
, NetWMWindowType
,
82 NetWMWindowTypeDialog
, NetClientList
, NetLast
}; /* EWMH atoms */
83 enum { Manager
, Xembed
, XembedInfo
, XLast
}; /* Xembed atoms */
84 enum { WMProtocols
, WMDelete
, WMState
, WMTakeFocus
, WMLast
}; /* default atoms */
85 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
86 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
99 void (*func
)(const Arg
*arg
);
103 typedef struct Monitor Monitor
;
104 typedef struct Client Client
;
109 int sfx
, sfy
, sfw
, sfh
; /* stored float geometry, used on mode revert */
110 int oldx
, oldy
, oldw
, oldh
;
111 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
114 int isfixed
, isfloating
, isurgent
, neverfocus
, oldstate
, isfullscreen
;
124 void (*func
)(const Arg
*);
130 void (*arrange
)(Monitor
*);
133 typedef struct Pertag Pertag
;
139 int by
; /* bar geometry */
140 int mx
, my
, mw
, mh
; /* screen size */
141 int wx
, wy
, ww
, wh
; /* window area */
142 unsigned int seltags
;
144 unsigned int tagset
[2];
158 const char *instance
;
165 typedef struct Systray Systray
;
171 /* function declarations */
172 static void applyrules(Client
*c
);
173 static int applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
);
174 static void arrange(Monitor
*m
);
175 static void arrangemon(Monitor
*m
);
176 static void attach(Client
*c
);
177 static void attachstack(Client
*c
);
178 static void buttonpress(XEvent
*e
);
179 static void checkotherwm(void);
180 static void cleanup(void);
181 static void cleanupmon(Monitor
*mon
);
182 static void clientmessage(XEvent
*e
);
183 static void configure(Client
*c
);
184 static void configurenotify(XEvent
*e
);
185 static void configurerequest(XEvent
*e
);
186 static Monitor
*createmon(void);
187 static void destroynotify(XEvent
*e
);
188 static void detach(Client
*c
);
189 static void detachstack(Client
*c
);
190 static Monitor
*dirtomon(int dir
);
191 static void drawbar(Monitor
*m
);
192 static void drawbars(void);
193 static void enternotify(XEvent
*e
);
194 static void expose(XEvent
*e
);
195 static void focus(Client
*c
);
196 static void focusin(XEvent
*e
);
197 static void focusmon(const Arg
*arg
);
198 static void focusstack(const Arg
*arg
);
199 static Atom
getatomprop(Client
*c
, Atom prop
);
200 static int getrootptr(int *x
, int *y
);
201 static long getstate(Window w
);
202 static unsigned int getsystraywidth();
203 static int gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
204 static void grabbuttons(Client
*c
, int focused
);
205 static void grabkeys(void);
206 static void incnmaster(const Arg
*arg
);
207 static void keypress(XEvent
*e
);
208 static void killclient(const Arg
*arg
);
209 static void manage(Window w
, XWindowAttributes
*wa
);
210 static void mappingnotify(XEvent
*e
);
211 static void maprequest(XEvent
*e
);
212 static void monocle(Monitor
*m
);
213 static void motionnotify(XEvent
*e
);
214 static void movemouse(const Arg
*arg
);
215 static Client
*nexttiled(Client
*c
);
216 static void pop(Client
*);
217 static void propertynotify(XEvent
*e
);
218 static void quit(const Arg
*arg
);
219 static Monitor
*recttomon(int x
, int y
, int w
, int h
);
220 static void removesystrayicon(Client
*i
);
221 static void resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
);
222 static void resizebarwin(Monitor
*m
);
223 static void resizeclient(Client
*c
, int x
, int y
, int w
, int h
);
224 static void resizemouse(const Arg
*arg
);
225 static void resizerequest(XEvent
*e
);
226 static void restack(Monitor
*m
);
227 static void run(void);
228 static void scan(void);
229 static int sendevent(Window w
, Atom proto
, int m
, long d0
, long d1
, long d2
, long d3
, long d4
);
230 static void sendmon(Client
*c
, Monitor
*m
);
231 static void setclientstate(Client
*c
, long state
);
232 static void setfocus(Client
*c
);
233 static void setfullscreen(Client
*c
, int fullscreen
);
234 static void setlayout(const Arg
*arg
);
235 static void setmfact(const Arg
*arg
);
236 static void setup(void);
237 static void seturgent(Client
*c
, int urg
);
238 static void showhide(Client
*c
);
239 static void sigchld(int unused
);
240 static void spawn(const Arg
*arg
);
241 static Monitor
*systraytomon(Monitor
*m
);
242 static void tag(const Arg
*arg
);
243 static void tagmon(const Arg
*arg
);
244 static void tile(Monitor
*);
245 static void togglebar(const Arg
*arg
);
246 static void togglefloating(const Arg
*arg
);
247 static void toggletag(const Arg
*arg
);
248 static void toggleview(const Arg
*arg
);
249 static void unfocus(Client
*c
, int setfocus
);
250 static void unmanage(Client
*c
, int destroyed
);
251 static void unmapnotify(XEvent
*e
);
252 static void updatebarpos(Monitor
*m
);
253 static void updatebars(void);
254 static void updateclientlist(void);
255 static int updategeom(void);
256 static void updatenumlockmask(void);
257 static void updatesizehints(Client
*c
);
258 static void updatestatus(void);
259 static void updatesystray(void);
260 static void updatesystrayicongeom(Client
*i
, int w
, int h
);
261 static void updatesystrayiconstate(Client
*i
, XPropertyEvent
*ev
);
262 static void updatetitle(Client
*c
);
263 static void updatewindowtype(Client
*c
);
264 static void updatewmhints(Client
*c
);
265 static void view(const Arg
*arg
);
266 static Client
*wintoclient(Window w
);
267 static Monitor
*wintomon(Window w
);
268 static Client
*wintosystrayicon(Window w
);
269 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
270 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
271 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
272 static void zoom(const Arg
*arg
);
275 static Systray
*systray
= NULL
;
276 static const char broken
[] = "broken";
277 static char stext
[256];
279 static int sw
, sh
; /* X display screen geometry width, height */
280 static int bh
, blw
= 0; /* bar geometry */
281 static int lrpad
; /* sum of left and right padding for text */
282 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
283 static unsigned int numlockmask
= 0;
284 static void (*handler
[LASTEvent
]) (XEvent
*) = {
285 [ButtonPress
] = buttonpress
,
286 [ClientMessage
] = clientmessage
,
287 [ConfigureRequest
] = configurerequest
,
288 [ConfigureNotify
] = configurenotify
,
289 [DestroyNotify
] = destroynotify
,
290 [EnterNotify
] = enternotify
,
293 [KeyPress
] = keypress
,
294 [MappingNotify
] = mappingnotify
,
295 [MapRequest
] = maprequest
,
296 [MotionNotify
] = motionnotify
,
297 [PropertyNotify
] = propertynotify
,
298 [ResizeRequest
] = resizerequest
,
299 [UnmapNotify
] = unmapnotify
301 static Atom wmatom
[WMLast
], netatom
[NetLast
], xatom
[XLast
];
302 static int running
= 1;
303 static Cur
*cursor
[CurLast
];
307 static Monitor
*mons
, *selmon
;
308 static Window root
, wmcheckwin
;
310 /* configuration, allows nested code to access above variables */
314 unsigned int curtag
, prevtag
; /* current and previous tag */
315 int nmasters
[LENGTH(tags
) + 1]; /* number of windows in master area */
316 float mfacts
[LENGTH(tags
) + 1]; /* mfacts per tag */
317 unsigned int sellts
[LENGTH(tags
) + 1]; /* selected layouts */
318 const Layout
*ltidxs
[LENGTH(tags
) + 1][2]; /* matrix of tags and layouts indexes */
319 int showbars
[LENGTH(tags
) + 1]; /* display bar for the current tag */
322 /* compile-time check if all tags fit into an unsigned int bit array. */
323 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
325 /* function implementations */
327 applyrules(Client
*c
)
329 const char *class, *instance
;
333 XClassHint ch
= { NULL
, NULL
};
338 XGetClassHint(dpy
, c
->win
, &ch
);
339 class = ch
.res_class
? ch
.res_class
: broken
;
340 instance
= ch
.res_name
? ch
.res_name
: broken
;
342 for (i
= 0; i
< LENGTH(rules
); i
++) {
344 if ((!r
->title
|| strstr(c
->name
, r
->title
))
345 && (!r
->class || strstr(class, r
->class))
346 && (!r
->instance
|| strstr(instance
, r
->instance
)))
348 c
->isfloating
= r
->isfloating
;
350 for (m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
359 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
363 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
)
368 /* set minimum possible */
376 if (*x
+ *w
+ 2 * c
->bw
< 0)
378 if (*y
+ *h
+ 2 * c
->bw
< 0)
381 if (*x
>= m
->wx
+ m
->ww
)
382 *x
= m
->wx
+ m
->ww
- WIDTH(c
);
383 if (*y
>= m
->wy
+ m
->wh
)
384 *y
= m
->wy
+ m
->wh
- HEIGHT(c
);
385 if (*x
+ *w
+ 2 * c
->bw
<= m
->wx
)
387 if (*y
+ *h
+ 2 * c
->bw
<= m
->wy
)
394 if (resizehints
|| c
->isfloating
|| !c
->mon
->lt
[c
->mon
->sellt
]->arrange
) {
395 /* see last two sentences in ICCCM 4.1.2.3 */
396 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
397 if (!baseismin
) { /* temporarily remove base dimensions */
401 /* adjust for aspect limits */
402 if (c
->mina
> 0 && c
->maxa
> 0) {
403 if (c
->maxa
< (float)*w
/ *h
)
404 *w
= *h
* c
->maxa
+ 0.5;
405 else if (c
->mina
< (float)*h
/ *w
)
406 *h
= *w
* c
->mina
+ 0.5;
408 if (baseismin
) { /* increment calculation requires this */
412 /* adjust for increment value */
417 /* restore base dimensions */
418 *w
= MAX(*w
+ c
->basew
, c
->minw
);
419 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
421 *w
= MIN(*w
, c
->maxw
);
423 *h
= MIN(*h
, c
->maxh
);
425 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
433 else for (m
= mons
; m
; m
= m
->next
)
438 } else for (m
= mons
; m
; m
= m
->next
)
443 arrangemon(Monitor
*m
)
445 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
446 if (m
->lt
[m
->sellt
]->arrange
)
447 m
->lt
[m
->sellt
]->arrange(m
);
453 c
->next
= c
->mon
->clients
;
458 attachstack(Client
*c
)
460 c
->snext
= c
->mon
->stack
;
465 buttonpress(XEvent
*e
)
467 unsigned int i
, x
, click
, occ
= 0;
471 XButtonPressedEvent
*ev
= &e
->xbutton
;
474 /* focus monitor if necessary */
475 if ((m
= wintomon(ev
->window
)) && m
!= selmon
) {
476 unfocus(selmon
->sel
, 1);
480 if (ev
->window
== selmon
->barwin
) {
482 for (c
= m
->clients
; c
; c
= c
->next
)
483 occ
|= c
->tags
== 255 ? 0 : c
->tags
;
485 /* do not reserve space for vacant tags */
486 if (!(occ
& 1 << i
|| m
->tagset
[m
->seltags
] & 1 << i
))
489 } while (ev
->x
>= x
&& ++i
< LENGTH(tags
));
490 if (i
< LENGTH(tags
)) {
493 } else if (ev
->x
< x
+ blw
)
495 else if (ev
->x
> selmon
->ww
- (int)TEXTW(stext
))
496 click
= ClkStatusText
;
499 } else if ((c
= wintoclient(ev
->window
))) {
502 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
503 click
= ClkClientWin
;
505 for (i
= 0; i
< LENGTH(buttons
); i
++)
506 if (click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
507 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
508 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
514 xerrorxlib
= XSetErrorHandler(xerrorstart
);
515 /* this causes an error if some other window manager is running */
516 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
518 XSetErrorHandler(xerror
);
526 Layout foo
= { "", NULL
};
531 selmon
->lt
[selmon
->sellt
] = &foo
;
532 for (m
= mons
; m
; m
= m
->next
)
534 unmanage(m
->stack
, 0);
535 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
539 XUnmapWindow(dpy
, systray
->win
);
540 XDestroyWindow(dpy
, systray
->win
);
543 for (i
= 0; i
< CurLast
; i
++)
544 drw_cur_free(drw
, cursor
[i
]);
545 for (i
= 0; i
< LENGTH(colors
); i
++)
547 XDestroyWindow(dpy
, wmcheckwin
);
550 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
551 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
555 cleanupmon(Monitor
*mon
)
562 for (m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
565 XUnmapWindow(dpy
, mon
->barwin
);
566 XDestroyWindow(dpy
, mon
->barwin
);
571 clientmessage(XEvent
*e
)
573 XWindowAttributes wa
;
574 XSetWindowAttributes swa
;
575 XClientMessageEvent
*cme
= &e
->xclient
;
576 Client
*c
= wintoclient(cme
->window
);
578 if (showsystray
&& cme
->window
== systray
->win
&& cme
->message_type
== netatom
[NetSystemTrayOP
]) {
579 /* add systray icons */
580 if (cme
->data
.l
[1] == SYSTEM_TRAY_REQUEST_DOCK
) {
581 if (!(c
= (Client
*)calloc(1, sizeof(Client
))))
582 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
583 if (!(c
->win
= cme
->data
.l
[2])) {
588 c
->next
= systray
->icons
;
590 XGetWindowAttributes(dpy
, c
->win
, &wa
);
591 c
->x
= c
->oldx
= c
->y
= c
->oldy
= 0;
592 c
->w
= c
->oldw
= wa
.width
;
593 c
->h
= c
->oldh
= wa
.height
;
594 c
->oldbw
= wa
.border_width
;
596 c
->isfloating
= True
;
597 /* reuse tags field as mapped status */
600 updatesystrayicongeom(c
, wa
.width
, wa
.height
);
601 XAddToSaveSet(dpy
, c
->win
);
602 XSelectInput(dpy
, c
->win
, StructureNotifyMask
| PropertyChangeMask
| ResizeRedirectMask
);
603 XReparentWindow(dpy
, c
->win
, systray
->win
, 0, 0);
604 /* use parents background color */
605 swa
.background_pixel
= scheme
[SchemeNorm
][ColBg
].pixel
;
606 XChangeWindowAttributes(dpy
, c
->win
, CWBackPixel
, &swa
);
607 sendevent(c
->win
, netatom
[Xembed
], StructureNotifyMask
, CurrentTime
, XEMBED_EMBEDDED_NOTIFY
, 0 , systray
->win
, XEMBED_EMBEDDED_VERSION
);
608 /* FIXME not sure if I have to send these events, too */
609 sendevent(c
->win
, netatom
[Xembed
], StructureNotifyMask
, CurrentTime
, XEMBED_FOCUS_IN
, 0 , systray
->win
, XEMBED_EMBEDDED_VERSION
);
610 sendevent(c
->win
, netatom
[Xembed
], StructureNotifyMask
, CurrentTime
, XEMBED_WINDOW_ACTIVATE
, 0 , systray
->win
, XEMBED_EMBEDDED_VERSION
);
611 sendevent(c
->win
, netatom
[Xembed
], StructureNotifyMask
, CurrentTime
, XEMBED_MODALITY_ON
, 0 , systray
->win
, XEMBED_EMBEDDED_VERSION
);
613 resizebarwin(selmon
);
615 setclientstate(c
, NormalState
);
621 if (cme
->message_type
== netatom
[NetWMState
]) {
622 if (cme
->data
.l
[1] == netatom
[NetWMFullscreen
]
623 || cme
->data
.l
[2] == netatom
[NetWMFullscreen
])
624 setfullscreen(c
, (cme
->data
.l
[0] == 1 /* _NET_WM_STATE_ADD */
625 || (cme
->data
.l
[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c
->isfullscreen
)));
626 } else if (cme
->message_type
== netatom
[NetActiveWindow
]) {
627 if (c
!= selmon
->sel
&& !c
->isurgent
)
637 ce
.type
= ConfigureNotify
;
645 ce
.border_width
= c
->bw
;
647 ce
.override_redirect
= False
;
648 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
652 configurenotify(XEvent
*e
)
656 XConfigureEvent
*ev
= &e
->xconfigure
;
659 /* TODO: updategeom handling sucks, needs to be simplified */
660 if (ev
->window
== root
) {
661 dirty
= (sw
!= ev
->width
|| sh
!= ev
->height
);
664 if (updategeom() || dirty
) {
665 drw_resize(drw
, sw
, bh
);
667 for (m
= mons
; m
; m
= m
->next
) {
668 for (c
= m
->clients
; c
; c
= c
->next
)
670 resizeclient(c
, m
->mx
, m
->my
, m
->mw
, m
->mh
);
680 configurerequest(XEvent
*e
)
684 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
687 if ((c
= wintoclient(ev
->window
))) {
688 if (ev
->value_mask
& CWBorderWidth
)
689 c
->bw
= ev
->border_width
;
690 else if (c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
692 if (ev
->value_mask
& CWX
) {
694 c
->x
= m
->mx
+ ev
->x
;
696 if (ev
->value_mask
& CWY
) {
698 c
->y
= m
->my
+ ev
->y
;
700 if (ev
->value_mask
& CWWidth
) {
704 if (ev
->value_mask
& CWHeight
) {
708 if ((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
709 c
->x
= m
->mx
+ (m
->mw
/ 2 - WIDTH(c
) / 2); /* center in x direction */
710 if ((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
711 c
->y
= m
->my
+ (m
->mh
/ 2 - HEIGHT(c
) / 2); /* center in y direction */
712 if ((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
715 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
721 wc
.width
= ev
->width
;
722 wc
.height
= ev
->height
;
723 wc
.border_width
= ev
->border_width
;
724 wc
.sibling
= ev
->above
;
725 wc
.stack_mode
= ev
->detail
;
726 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
737 m
= ecalloc(1, sizeof(Monitor
));
738 m
->tagset
[0] = m
->tagset
[1] = 1;
740 m
->nmaster
= nmaster
;
741 m
->showbar
= showbar
;
743 m
->lt
[0] = &layouts
[0];
744 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
745 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
746 m
->pertag
= ecalloc(1, sizeof(Pertag
));
747 m
->pertag
->curtag
= m
->pertag
->prevtag
= 1;
749 for (i
= 0; i
<= LENGTH(tags
); i
++) {
750 m
->pertag
->nmasters
[i
] = m
->nmaster
;
751 m
->pertag
->mfacts
[i
] = m
->mfact
;
753 m
->pertag
->ltidxs
[i
][0] = m
->lt
[0];
754 m
->pertag
->ltidxs
[i
][1] = m
->lt
[1];
755 m
->pertag
->sellts
[i
] = m
->sellt
;
757 m
->pertag
->showbars
[i
] = m
->showbar
;
764 destroynotify(XEvent
*e
)
767 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
769 if ((c
= wintoclient(ev
->window
)))
771 else if ((c
= wintosystrayicon(ev
->window
))) {
772 removesystrayicon(c
);
773 resizebarwin(selmon
);
783 for (tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
788 detachstack(Client
*c
)
792 for (tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
795 if (c
== c
->mon
->sel
) {
796 for (t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
807 if (!(m
= selmon
->next
))
809 } else if (selmon
== mons
)
810 for (m
= mons
; m
->next
; m
= m
->next
);
812 for (m
= mons
; m
->next
!= selmon
; m
= m
->next
);
819 int x
, w
, sw
= 0, stw
= 0;
820 int boxs
= drw
->fonts
->h
/ 9;
821 int boxw
= drw
->fonts
->h
/ 6 + 2;
822 unsigned int i
, occ
= 0, urg
= 0;
825 if(showsystray
&& m
== systraytomon(m
))
826 stw
= getsystraywidth();
828 /* draw status first so it can be overdrawn by tags later */
829 if (m
== selmon
|| 1) { /* status is only drawn on selected monitor */
830 drw_setscheme(drw
, scheme
[SchemeNorm
]);
831 sw
= TEXTW(stext
) - lrpad
/ 2 + 2; /* 2px right padding */
832 drw_text(drw
, m
->ww
- sw
- stw
, 0, sw
, bh
, lrpad
/ 2 - 2, stext
, 0);
836 for (c
= m
->clients
; c
; c
= c
->next
) {
837 occ
|= c
->tags
== 255 ? 0 : c
->tags
;
842 for (i
= 0; i
< LENGTH(tags
); i
++) {
843 /* do not draw vacant tags */
844 if (!(occ
& 1 << i
|| m
->tagset
[m
->seltags
] & 1 << i
))
848 drw_setscheme(drw
, scheme
[m
->tagset
[m
->seltags
] & 1 << i
? SchemeSel
: SchemeNorm
]);
849 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, tags
[i
], urg
& 1 << i
);
852 w
= blw
= TEXTW(m
->ltsymbol
);
853 drw_setscheme(drw
, scheme
[SchemeNorm
]);
854 x
= drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->ltsymbol
, 0);
856 if ((w
= m
->ww
- sw
- stw
- x
) > bh
) {
858 drw_setscheme(drw
, scheme
[m
== selmon
? SchemeSel
: SchemeNorm
]);
859 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->sel
->name
, 0);
860 if (m
->sel
->isfloating
)
861 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
, m
->sel
->isfixed
, 0);
863 drw_setscheme(drw
, scheme
[SchemeNorm
]);
864 drw_rect(drw
, x
, 0, w
, bh
, 1, 1);
867 drw_map(drw
, m
->barwin
, 0, 0, m
->ww
- stw
, bh
);
875 for (m
= mons
; m
; m
= m
->next
)
880 enternotify(XEvent
*e
)
884 XCrossingEvent
*ev
= &e
->xcrossing
;
886 if ((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
888 c
= wintoclient(ev
->window
);
889 m
= c
? c
->mon
: wintomon(ev
->window
);
891 unfocus(selmon
->sel
, 1);
893 } else if (!c
|| c
== selmon
->sel
)
902 XExposeEvent
*ev
= &e
->xexpose
;
904 if (ev
->count
== 0 && (m
= wintomon(ev
->window
))) {
914 if (!c
|| !ISVISIBLE(c
))
915 for (c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
916 if (selmon
->sel
&& selmon
->sel
!= c
)
917 unfocus(selmon
->sel
, 0);
919 if (c
->mon
!= selmon
)
926 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeSel
][ColBorder
].pixel
);
929 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
930 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
936 /* there are some broken focus acquiring clients needing extra handling */
940 XFocusChangeEvent
*ev
= &e
->xfocus
;
942 if (selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
943 setfocus(selmon
->sel
);
947 focusmon(const Arg
*arg
)
953 if ((m
= dirtomon(arg
->i
)) == selmon
)
955 unfocus(selmon
->sel
, 0);
961 focusstack(const Arg
*arg
)
963 Client
*c
= NULL
, *i
;
968 for (c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
970 for (c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
972 for (i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
976 for (; i
; i
= i
->next
)
987 getatomprop(Client
*c
, Atom prop
)
991 unsigned char *p
= NULL
;
992 Atom da
, atom
= None
;
993 /* FIXME getatomprop should return the number of items and a pointer to
994 * the stored data instead of this workaround */
996 if (prop
== xatom
[XembedInfo
])
997 req
= xatom
[XembedInfo
];
999 if (XGetWindowProperty(dpy
, c
->win
, prop
, 0L, sizeof atom
, False
, req
,
1000 &da
, &di
, &dl
, &dl
, &p
) == Success
&& p
) {
1002 if (da
== xatom
[XembedInfo
] && dl
== 2)
1003 atom
= ((Atom
*)p
)[1];
1010 getrootptr(int *x
, int *y
)
1016 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
1024 unsigned char *p
= NULL
;
1025 unsigned long n
, extra
;
1028 if (XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
1029 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
1043 for(i
= systray
->icons
; i
; w
+= i
->w
+ systrayspacing
, i
= i
->next
) ;
1044 return w
? w
+ systrayspacing
: 1;
1048 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
)
1054 if (!text
|| size
== 0)
1057 if (!XGetTextProperty(dpy
, w
, &name
, atom
) || !name
.nitems
)
1059 if (name
.encoding
== XA_STRING
)
1060 strncpy(text
, (char *)name
.value
, size
- 1);
1062 if (XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
1063 strncpy(text
, *list
, size
- 1);
1064 XFreeStringList(list
);
1067 text
[size
- 1] = '\0';
1073 grabbuttons(Client
*c
, int focused
)
1075 updatenumlockmask();
1078 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1079 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1081 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
1082 BUTTONMASK
, GrabModeSync
, GrabModeSync
, None
, None
);
1083 for (i
= 0; i
< LENGTH(buttons
); i
++)
1084 if (buttons
[i
].click
== ClkClientWin
)
1085 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1086 XGrabButton(dpy
, buttons
[i
].button
,
1087 buttons
[i
].mask
| modifiers
[j
],
1088 c
->win
, False
, BUTTONMASK
,
1089 GrabModeAsync
, GrabModeSync
, None
, None
);
1096 updatenumlockmask();
1099 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1102 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
1103 for (i
= 0; i
< LENGTH(keys
); i
++)
1104 if ((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
1105 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1106 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
1107 True
, GrabModeAsync
, GrabModeAsync
);
1112 incnmaster(const Arg
*arg
)
1114 selmon
->nmaster
= selmon
->pertag
->nmasters
[selmon
->pertag
->curtag
] = MAX(selmon
->nmaster
+ arg
->i
, 0);
1120 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
)
1123 if (unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
1124 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
1128 #endif /* XINERAMA */
1138 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1139 for (i
= 0; i
< LENGTH(keys
); i
++)
1140 if (keysym
== keys
[i
].keysym
1141 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1143 keys
[i
].func(&(keys
[i
].arg
));
1147 killclient(const Arg
*arg
)
1151 if (!sendevent(selmon
->sel
->win
, wmatom
[WMDelete
], NoEventMask
, wmatom
[WMDelete
], CurrentTime
, 0 , 0, 0)) {
1153 XSetErrorHandler(xerrordummy
);
1154 XSetCloseDownMode(dpy
, DestroyAll
);
1155 XKillClient(dpy
, selmon
->sel
->win
);
1157 XSetErrorHandler(xerror
);
1163 manage(Window w
, XWindowAttributes
*wa
)
1165 Client
*c
, *t
= NULL
;
1166 Window trans
= None
;
1169 c
= ecalloc(1, sizeof(Client
));
1172 c
->x
= c
->oldx
= wa
->x
;
1173 c
->y
= c
->oldy
= wa
->y
;
1174 c
->w
= c
->oldw
= wa
->width
;
1175 c
->h
= c
->oldh
= wa
->height
;
1176 c
->oldbw
= wa
->border_width
;
1179 if (XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1187 if (c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1188 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1189 if (c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1190 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1191 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1192 /* only fix client y-offset, if the client center might cover the bar */
1193 c
->y
= MAX(c
->y
, ((c
->mon
->by
== c
->mon
->my
) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1194 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1197 wc
.border_width
= c
->bw
;
1198 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1199 XSetWindowBorder(dpy
, w
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1200 configure(c
); /* propagates border_width, if size doesn't change */
1201 updatewindowtype(c
);
1208 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1211 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1213 XRaiseWindow(dpy
, c
->win
);
1216 XChangeProperty(dpy
, root
, netatom
[NetClientList
], XA_WINDOW
, 32, PropModeAppend
,
1217 (unsigned char *) &(c
->win
), 1);
1218 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1219 setclientstate(c
, NormalState
);
1220 if (c
->mon
== selmon
)
1221 unfocus(selmon
->sel
, 0);
1224 XMapWindow(dpy
, c
->win
);
1229 mappingnotify(XEvent
*e
)
1231 XMappingEvent
*ev
= &e
->xmapping
;
1233 XRefreshKeyboardMapping(ev
);
1234 if (ev
->request
== MappingKeyboard
)
1239 maprequest(XEvent
*e
)
1241 static XWindowAttributes wa
;
1242 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1244 if ((i
= wintosystrayicon(ev
->window
))) {
1245 sendevent(i
->win
, netatom
[Xembed
], StructureNotifyMask
, CurrentTime
, XEMBED_WINDOW_ACTIVATE
, 0, systray
->win
, XEMBED_EMBEDDED_VERSION
);
1246 resizebarwin(selmon
);
1250 if (!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1252 if (wa
.override_redirect
)
1254 if (!wintoclient(ev
->window
))
1255 manage(ev
->window
, &wa
);
1264 for (c
= m
->clients
; c
; c
= c
->next
)
1267 if (n
> 0) /* override layout symbol */
1268 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "%s/%d", layouts
[2].symbol
, n
);
1269 for (c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1270 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, 0);
1274 motionnotify(XEvent
*e
)
1276 static Monitor
*mon
= NULL
;
1278 XMotionEvent
*ev
= &e
->xmotion
;
1280 if (ev
->window
!= root
)
1282 if ((m
= recttomon(ev
->x_root
, ev
->y_root
, 1, 1)) != mon
&& mon
) {
1283 unfocus(selmon
->sel
, 1);
1291 movemouse(const Arg
*arg
)
1293 int x
, y
, ocx
, ocy
, nx
, ny
;
1299 if (!(c
= selmon
->sel
))
1301 if (c
->isfullscreen
) /* no support moving fullscreen windows by mouse */
1306 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1307 None
, cursor
[CurMove
]->cursor
, CurrentTime
) != GrabSuccess
)
1309 if (!getrootptr(&x
, &y
))
1312 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1314 case ConfigureRequest
:
1317 handler
[ev
.type
](&ev
);
1320 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1322 lasttime
= ev
.xmotion
.time
;
1324 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1325 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1326 if (abs(selmon
->wx
- nx
) < snap
)
1328 else if (abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1329 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1330 if (abs(selmon
->wy
- ny
) < snap
)
1332 else if (abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1333 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1334 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1335 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1336 togglefloating(NULL
);
1337 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1338 resize(c
, nx
, ny
, c
->w
, c
->h
, 1);
1341 } while (ev
.type
!= ButtonRelease
);
1342 XUngrabPointer(dpy
, CurrentTime
);
1343 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1351 nexttiled(Client
*c
)
1353 for (; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1367 propertynotify(XEvent
*e
)
1371 XPropertyEvent
*ev
= &e
->xproperty
;
1373 if ((c
= wintosystrayicon(ev
->window
))) {
1374 if (ev
->atom
== XA_WM_NORMAL_HINTS
) {
1376 updatesystrayicongeom(c
, c
->w
, c
->h
);
1379 updatesystrayiconstate(c
, ev
);
1380 resizebarwin(selmon
);
1383 if ((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1385 else if (ev
->state
== PropertyDelete
)
1386 return; /* ignore */
1387 else if ((c
= wintoclient(ev
->window
))) {
1390 case XA_WM_TRANSIENT_FOR
:
1391 if (!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1392 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1395 case XA_WM_NORMAL_HINTS
:
1403 if (ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1405 if (c
== c
->mon
->sel
)
1408 if (ev
->atom
== netatom
[NetWMWindowType
])
1409 updatewindowtype(c
);
1414 quit(const Arg
*arg
)
1420 recttomon(int x
, int y
, int w
, int h
)
1422 Monitor
*m
, *r
= selmon
;
1425 for (m
= mons
; m
; m
= m
->next
)
1426 if ((a
= INTERSECT(x
, y
, w
, h
, m
)) > area
) {
1434 removesystrayicon(Client
*i
)
1438 if (!showsystray
|| !i
)
1440 for (ii
= &systray
->icons
; *ii
&& *ii
!= i
; ii
= &(*ii
)->next
);
1448 resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
)
1450 if (applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1451 resizeclient(c
, x
, y
, w
, h
);
1455 resizebarwin(Monitor
*m
) {
1456 unsigned int w
= m
->ww
;
1457 if (showsystray
&& m
== systraytomon(m
))
1458 w
-= getsystraywidth();
1459 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, w
, bh
);
1463 resizeclient(Client
*c
, int x
, int y
, int w
, int h
)
1467 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1468 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1469 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1470 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1471 wc
.border_width
= c
->bw
;
1472 if (((nexttiled(c
->mon
->clients
) == c
&& !nexttiled(c
->next
))
1473 || &monocle
== c
->mon
->lt
[c
->mon
->sellt
]->arrange
)
1474 && !c
->isfullscreen
) {
1475 c
->w
= wc
.width
+= c
->bw
* 2;
1476 c
->h
= wc
.height
+= c
->bw
* 2;
1477 wc
.border_width
= 0;
1479 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1485 resizemouse(const Arg
*arg
)
1487 int ocx
, ocy
, nw
, nh
;
1493 if (!(c
= selmon
->sel
))
1495 if (c
->isfullscreen
) /* no support resizing fullscreen windows by mouse */
1500 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1501 None
, cursor
[CurResize
]->cursor
, CurrentTime
) != GrabSuccess
)
1503 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1505 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1507 case ConfigureRequest
:
1510 handler
[ev
.type
](&ev
);
1513 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1515 lasttime
= ev
.xmotion
.time
;
1517 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1518 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1519 if (c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1520 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1522 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1523 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1524 togglefloating(NULL
);
1526 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1527 resize(c
, c
->x
, c
->y
, nw
, nh
, 1);
1530 } while (ev
.type
!= ButtonRelease
);
1531 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1532 XUngrabPointer(dpy
, CurrentTime
);
1533 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1534 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1542 resizerequest(XEvent
*e
)
1544 XResizeRequestEvent
*ev
= &e
->xresizerequest
;
1547 if ((i
= wintosystrayicon(ev
->window
))) {
1548 updatesystrayicongeom(i
, ev
->width
, ev
->height
);
1549 resizebarwin(selmon
);
1564 if (m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1565 XRaiseWindow(dpy
, m
->sel
->win
);
1566 if (m
->lt
[m
->sellt
]->arrange
) {
1567 wc
.stack_mode
= Below
;
1568 wc
.sibling
= m
->barwin
;
1569 for (c
= m
->stack
; c
; c
= c
->snext
)
1570 if (!c
->isfloating
&& ISVISIBLE(c
)) {
1571 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1572 wc
.sibling
= c
->win
;
1576 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1583 /* main event loop */
1585 while (running
&& !XNextEvent(dpy
, &ev
))
1586 if (handler
[ev
.type
])
1587 handler
[ev
.type
](&ev
); /* call handler */
1593 unsigned int i
, num
;
1594 Window d1
, d2
, *wins
= NULL
;
1595 XWindowAttributes wa
;
1597 if (XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1598 for (i
= 0; i
< num
; i
++) {
1599 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1600 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1602 if (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1603 manage(wins
[i
], &wa
);
1605 for (i
= 0; i
< num
; i
++) { /* now the transients */
1606 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1608 if (XGetTransientForHint(dpy
, wins
[i
], &d1
)
1609 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1610 manage(wins
[i
], &wa
);
1618 sendmon(Client
*c
, Monitor
*m
)
1626 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1634 setclientstate(Client
*c
, long state
)
1636 long data
[] = { state
, None
};
1638 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1639 PropModeReplace
, (unsigned char *)data
, 2);
1643 sendevent(Window w
, Atom proto
, int mask
, long d0
, long d1
, long d2
, long d3
, long d4
)
1646 Atom
*protocols
, mt
;
1650 if (proto
== wmatom
[WMTakeFocus
] || proto
== wmatom
[WMDelete
]) {
1651 mt
= wmatom
[WMProtocols
];
1652 if (XGetWMProtocols(dpy
, w
, &protocols
, &n
)) {
1653 while (!exists
&& n
--)
1654 exists
= protocols
[n
] == proto
;
1663 ev
.type
= ClientMessage
;
1664 ev
.xclient
.window
= w
;
1665 ev
.xclient
.message_type
= mt
;
1666 ev
.xclient
.format
= 32;
1667 ev
.xclient
.data
.l
[0] = d0
;
1668 ev
.xclient
.data
.l
[1] = d1
;
1669 ev
.xclient
.data
.l
[2] = d2
;
1670 ev
.xclient
.data
.l
[3] = d3
;
1671 ev
.xclient
.data
.l
[4] = d4
;
1672 XSendEvent(dpy
, w
, False
, mask
, &ev
);
1680 if (!c
->neverfocus
) {
1681 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
1682 XChangeProperty(dpy
, root
, netatom
[NetActiveWindow
],
1683 XA_WINDOW
, 32, PropModeReplace
,
1684 (unsigned char *) &(c
->win
), 1);
1686 sendevent(c
->win
, wmatom
[WMTakeFocus
], NoEventMask
, wmatom
[WMTakeFocus
], CurrentTime
, 0, 0, 0);
1690 setfullscreen(Client
*c
, int fullscreen
)
1692 if (fullscreen
&& !c
->isfullscreen
) {
1693 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1694 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1695 c
->isfullscreen
= 1;
1696 c
->oldstate
= c
->isfloating
;
1700 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1701 XRaiseWindow(dpy
, c
->win
);
1702 } else if (!fullscreen
&& c
->isfullscreen
){
1703 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1704 PropModeReplace
, (unsigned char*)0, 0);
1705 c
->isfullscreen
= 0;
1706 c
->isfloating
= c
->oldstate
;
1712 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1718 setlayout(const Arg
*arg
)
1720 if (!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1721 selmon
->sellt
= selmon
->pertag
->sellts
[selmon
->pertag
->curtag
] ^= 1;
1723 selmon
->lt
[selmon
->sellt
] = selmon
->pertag
->ltidxs
[selmon
->pertag
->curtag
][selmon
->sellt
] = (Layout
*)arg
->v
;
1724 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1731 /* arg > 1.0 will set mfact absolutely */
1733 setmfact(const Arg
*arg
)
1737 if (!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1739 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1740 if (f
< 0.05 || f
> 0.95)
1742 selmon
->mfact
= selmon
->pertag
->mfacts
[selmon
->pertag
->curtag
] = f
;
1750 XSetWindowAttributes wa
;
1753 /* clean up any zombies immediately */
1757 screen
= DefaultScreen(dpy
);
1758 sw
= DisplayWidth(dpy
, screen
);
1759 sh
= DisplayHeight(dpy
, screen
);
1760 root
= RootWindow(dpy
, screen
);
1761 drw
= drw_create(dpy
, screen
, root
, sw
, sh
);
1762 if (!drw_fontset_create(drw
, fonts
, LENGTH(fonts
)))
1763 die("no fonts could be loaded.");
1764 lrpad
= drw
->fonts
->h
;
1765 bh
= drw
->fonts
->h
+ 2;
1768 utf8string
= XInternAtom(dpy
, "UTF8_STRING", False
);
1769 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1770 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1771 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1772 wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
);
1773 netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
);
1774 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1775 netatom
[NetSystemTray
] = XInternAtom(dpy
, "_NET_SYSTEM_TRAY_S0", False
);
1776 netatom
[NetSystemTrayOP
] = XInternAtom(dpy
, "_NET_SYSTEM_TRAY_OPCODE", False
);
1777 netatom
[NetSystemTrayOrientation
] = XInternAtom(dpy
, "_NET_SYSTEM_TRAY_ORIENTATION", False
);
1778 netatom
[NetSystemTrayOrientationHorz
] = XInternAtom(dpy
, "_NET_SYSTEM_TRAY_ORIENTATION_HORZ", False
);
1779 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1780 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1781 netatom
[NetWMCheck
] = XInternAtom(dpy
, "_NET_SUPPORTING_WM_CHECK", False
);
1782 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1783 netatom
[NetWMWindowType
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE", False
);
1784 netatom
[NetWMWindowTypeDialog
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
1785 netatom
[NetClientList
] = XInternAtom(dpy
, "_NET_CLIENT_LIST", False
);
1786 xatom
[Manager
] = XInternAtom(dpy
, "MANAGER", False
);
1787 xatom
[Xembed
] = XInternAtom(dpy
, "_XEMBED", False
);
1788 xatom
[XembedInfo
] = XInternAtom(dpy
, "_XEMBED_INFO", False
);
1790 cursor
[CurNormal
] = drw_cur_create(drw
, XC_left_ptr
);
1791 cursor
[CurResize
] = drw_cur_create(drw
, XC_sizing
);
1792 cursor
[CurMove
] = drw_cur_create(drw
, XC_fleur
);
1793 /* init appearance */
1794 scheme
= ecalloc(LENGTH(colors
), sizeof(Clr
*));
1795 for (i
= 0; i
< LENGTH(colors
); i
++)
1796 scheme
[i
] = drw_scm_create(drw
, colors
[i
], 3);
1797 /* init system tray */
1802 /* supporting window for NetWMCheck */
1803 wmcheckwin
= XCreateSimpleWindow(dpy
, root
, 0, 0, 1, 1, 0, 0, 0);
1804 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1805 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1806 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMName
], utf8string
, 8,
1807 PropModeReplace
, (unsigned char *) "dwm", 3);
1808 XChangeProperty(dpy
, root
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1809 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1810 /* EWMH support per view */
1811 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1812 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1813 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1815 wa
.cursor
= cursor
[CurNormal
]->cursor
;
1816 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
1817 |ButtonPressMask
|PointerMotionMask
|EnterWindowMask
1818 |LeaveWindowMask
|StructureNotifyMask
|PropertyChangeMask
;
1819 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1820 XSelectInput(dpy
, root
, wa
.event_mask
);
1827 seturgent(Client
*c
, int urg
)
1832 if (!(wmh
= XGetWMHints(dpy
, c
->win
)))
1834 wmh
->flags
= urg
? (wmh
->flags
| XUrgencyHint
) : (wmh
->flags
& ~XUrgencyHint
);
1835 XSetWMHints(dpy
, c
->win
, wmh
);
1845 /* show clients top down */
1846 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1847 if ((!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
) && !c
->isfullscreen
)
1848 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, 0);
1851 /* hide clients bottom up */
1853 XMoveWindow(dpy
, c
->win
, WIDTH(c
) * -2, c
->y
);
1860 if (signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1861 die("can't install SIGCHLD handler:");
1862 while (0 < waitpid(-1, NULL
, WNOHANG
));
1866 spawn(const Arg
*arg
)
1868 if (arg
->v
== dmenucmd
)
1869 dmenumon
[0] = '0' + selmon
->num
;
1872 close(ConnectionNumber(dpy
));
1874 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1875 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1884 if (selmon
->sel
&& arg
->ui
& TAGMASK
) {
1885 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1892 tagmon(const Arg
*arg
)
1894 if (!selmon
->sel
|| !mons
->next
)
1896 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1901 unsigned int i
, n
, h
, r
, g
= 0, mw
, my
, ty
;
1904 for (n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1909 mw
= m
->nmaster
? (m
->ww
- (g
= gappx
)) * m
->mfact
: 0;
1912 for(i
= my
= ty
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), i
++)
1913 if(i
< m
->nmaster
) {
1914 r
= MIN(n
, m
->nmaster
) - i
;
1915 h
= (m
->wh
- my
- gappx
* (r
- 1)) / r
;
1916 resize(c
, m
->wx
, m
->wy
+ my
, mw
- (2*c
->bw
), h
- (2*c
->bw
), False
);
1917 my
+= HEIGHT(c
) + gappx
;
1921 h
= (m
->wh
- ty
- gappx
* (r
- 1)) / r
;
1922 resize(c
, m
->wx
+ mw
+ g
, m
->wy
+ ty
, m
->ww
- mw
- g
- (2*c
->bw
), h
- (2*c
->bw
), False
);
1923 ty
+= HEIGHT(c
) + gappx
;
1928 togglebar(const Arg
*arg
)
1930 selmon
->showbar
= selmon
->pertag
->showbars
[selmon
->pertag
->curtag
] = !selmon
->showbar
;
1931 updatebarpos(selmon
);
1932 resizebarwin(selmon
);
1935 if (!selmon
->showbar
)
1937 else if (selmon
->showbar
) {
1939 if (!selmon
->topbar
)
1940 wc
.y
= selmon
->mh
- bh
;
1942 XConfigureWindow(dpy
, systray
->win
, CWY
, &wc
);
1948 togglefloating(const Arg
*arg
)
1952 if (selmon
->sel
->isfullscreen
) /* no support for fullscreen windows */
1954 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1955 if (selmon
->sel
->isfloating
)
1956 /* restore last known float dimensions */
1957 resize(selmon
->sel
, selmon
->sel
->sfx
, selmon
->sel
->sfy
,
1958 selmon
->sel
->sfw
, selmon
->sel
->sfh
, False
);
1960 /* save last known float dimensions */
1961 selmon
->sel
->sfx
= selmon
->sel
->x
;
1962 selmon
->sel
->sfy
= selmon
->sel
->y
;
1963 selmon
->sel
->sfw
= selmon
->sel
->w
;
1964 selmon
->sel
->sfh
= selmon
->sel
->h
;
1970 toggletag(const Arg
*arg
)
1972 unsigned int newtags
;
1976 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1978 selmon
->sel
->tags
= newtags
;
1985 toggleview(const Arg
*arg
)
1987 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1991 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1993 if (newtagset
== ~0) {
1994 selmon
->pertag
->prevtag
= selmon
->pertag
->curtag
;
1995 selmon
->pertag
->curtag
= 0;
1998 /* test if the user did not select the same tag */
1999 if (!(newtagset
& 1 << (selmon
->pertag
->curtag
- 1))) {
2000 selmon
->pertag
->prevtag
= selmon
->pertag
->curtag
;
2001 for (i
= 0; !(newtagset
& 1 << i
); i
++) ;
2002 selmon
->pertag
->curtag
= i
+ 1;
2005 /* apply settings for this view */
2006 selmon
->nmaster
= selmon
->pertag
->nmasters
[selmon
->pertag
->curtag
];
2007 selmon
->mfact
= selmon
->pertag
->mfacts
[selmon
->pertag
->curtag
];
2008 selmon
->sellt
= selmon
->pertag
->sellts
[selmon
->pertag
->curtag
];
2009 selmon
->lt
[selmon
->sellt
] = selmon
->pertag
->ltidxs
[selmon
->pertag
->curtag
][selmon
->sellt
];
2010 selmon
->lt
[selmon
->sellt
^1] = selmon
->pertag
->ltidxs
[selmon
->pertag
->curtag
][selmon
->sellt
^1];
2012 if (selmon
->showbar
!= selmon
->pertag
->showbars
[selmon
->pertag
->curtag
])
2021 unfocus(Client
*c
, int setfocus
)
2026 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeNorm
][ColBorder
].pixel
);
2028 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
2029 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
2034 unmanage(Client
*c
, int destroyed
)
2036 Monitor
*m
= c
->mon
;
2042 wc
.border_width
= c
->oldbw
;
2043 XGrabServer(dpy
); /* avoid race conditions */
2044 XSetErrorHandler(xerrordummy
);
2045 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
2046 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
2047 setclientstate(c
, WithdrawnState
);
2049 XSetErrorHandler(xerror
);
2059 unmapnotify(XEvent
*e
)
2062 XUnmapEvent
*ev
= &e
->xunmap
;
2064 if ((c
= wintoclient(ev
->window
))) {
2066 setclientstate(c
, WithdrawnState
);
2070 else if ((c
= wintosystrayicon(ev
->window
))) {
2071 /* KLUDGE! sometimes icons occasionally unmap their windows, but do
2072 * _not_ destroy them. We map those windows back */
2073 XMapRaised(dpy
, c
->win
);
2083 XSetWindowAttributes wa
= {
2084 .override_redirect
= True
,
2085 .background_pixmap
= ParentRelative
,
2086 .event_mask
= ButtonPressMask
|ExposureMask
2088 XClassHint ch
= {"dwm", "dwm"};
2089 for (m
= mons
; m
; m
= m
->next
) {
2093 if (showsystray
&& m
== systraytomon(m
))
2094 w
-= getsystraywidth();
2095 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, w
, bh
, 0, DefaultDepth(dpy
, screen
),
2096 CopyFromParent
, DefaultVisual(dpy
, screen
),
2097 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
2098 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]->cursor
);
2099 if (showsystray
&& m
== systraytomon(m
))
2100 XMapRaised(dpy
, systray
->win
);
2101 XMapRaised(dpy
, m
->barwin
);
2102 XSetClassHint(dpy
, m
->barwin
, &ch
);
2107 updatebarpos(Monitor
*m
)
2113 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
2114 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
2125 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
2126 for (m
= mons
; m
; m
= m
->next
)
2127 for (c
= m
->clients
; c
; c
= c
->next
)
2128 XChangeProperty(dpy
, root
, netatom
[NetClientList
],
2129 XA_WINDOW
, 32, PropModeAppend
,
2130 (unsigned char *) &(c
->win
), 1);
2139 if (XineramaIsActive(dpy
)) {
2143 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
2144 XineramaScreenInfo
*unique
= NULL
;
2146 for (n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
2147 /* only consider unique geometries as separate screens */
2148 unique
= ecalloc(nn
, sizeof(XineramaScreenInfo
));
2149 for (i
= 0, j
= 0; i
< nn
; i
++)
2150 if (isuniquegeom(unique
, j
, &info
[i
]))
2151 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
2154 if (n
<= nn
) { /* new monitors available */
2155 for (i
= 0; i
< (nn
- n
); i
++) {
2156 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
2158 m
->next
= createmon();
2162 for (i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
2164 || unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
2165 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
)
2169 m
->mx
= m
->wx
= unique
[i
].x_org
;
2170 m
->my
= m
->wy
= unique
[i
].y_org
;
2171 m
->mw
= m
->ww
= unique
[i
].width
;
2172 m
->mh
= m
->wh
= unique
[i
].height
;
2175 } else { /* less monitors available nn < n */
2176 for (i
= nn
; i
< n
; i
++) {
2177 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
2178 while ((c
= m
->clients
)) {
2180 m
->clients
= c
->next
;
2193 #endif /* XINERAMA */
2194 { /* default monitor setup */
2197 if (mons
->mw
!= sw
|| mons
->mh
!= sh
) {
2199 mons
->mw
= mons
->ww
= sw
;
2200 mons
->mh
= mons
->wh
= sh
;
2206 selmon
= wintomon(root
);
2212 updatenumlockmask(void)
2215 XModifierKeymap
*modmap
;
2218 modmap
= XGetModifierMapping(dpy
);
2219 for (i
= 0; i
< 8; i
++)
2220 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
2221 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
2222 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
2223 numlockmask
= (1 << i
);
2224 XFreeModifiermap(modmap
);
2228 updatesizehints(Client
*c
)
2233 if (!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
2234 /* size is uninitialized, ensure that size.flags aren't used */
2236 if (size
.flags
& PBaseSize
) {
2237 c
->basew
= size
.base_width
;
2238 c
->baseh
= size
.base_height
;
2239 } else if (size
.flags
& PMinSize
) {
2240 c
->basew
= size
.min_width
;
2241 c
->baseh
= size
.min_height
;
2243 c
->basew
= c
->baseh
= 0;
2244 if (size
.flags
& PResizeInc
) {
2245 c
->incw
= size
.width_inc
;
2246 c
->inch
= size
.height_inc
;
2248 c
->incw
= c
->inch
= 0;
2249 if (size
.flags
& PMaxSize
) {
2250 c
->maxw
= size
.max_width
;
2251 c
->maxh
= size
.max_height
;
2253 c
->maxw
= c
->maxh
= 0;
2254 if (size
.flags
& PMinSize
) {
2255 c
->minw
= size
.min_width
;
2256 c
->minh
= size
.min_height
;
2257 } else if (size
.flags
& PBaseSize
) {
2258 c
->minw
= size
.base_width
;
2259 c
->minh
= size
.base_height
;
2261 c
->minw
= c
->minh
= 0;
2262 if (size
.flags
& PAspect
) {
2263 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
2264 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
2266 c
->maxa
= c
->mina
= 0.0;
2267 c
->isfixed
= (c
->maxw
&& c
->maxh
&& c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
2274 if (!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
2275 strcpy(stext
, "dwm-"VERSION
);
2276 for(m
= mons
; m
; m
= m
->next
)
2281 updatesystrayicongeom(Client
*i
, int w
, int h
)
2290 i
->w
= (int) ((float)bh
* ((float)w
/ (float)h
));
2291 applysizehints(i
, &(i
->x
), &(i
->y
), &(i
->w
), &(i
->h
), False
);
2292 /* force icons into the systray dimenons if they don't want to */
2297 i
->w
= (int) ((float)bh
* ((float)i
->w
/ (float)i
->h
));
2304 updatesystrayiconstate(Client
*i
, XPropertyEvent
*ev
)
2309 if (!showsystray
|| !i
|| ev
->atom
!= xatom
[XembedInfo
] ||
2310 !(flags
= getatomprop(i
, xatom
[XembedInfo
])))
2313 if (flags
& XEMBED_MAPPED
&& !i
->tags
) {
2315 code
= XEMBED_WINDOW_ACTIVATE
;
2316 XMapRaised(dpy
, i
->win
);
2317 setclientstate(i
, NormalState
);
2319 else if (!(flags
& XEMBED_MAPPED
) && i
->tags
) {
2321 code
= XEMBED_WINDOW_DEACTIVATE
;
2322 XUnmapWindow(dpy
, i
->win
);
2323 setclientstate(i
, WithdrawnState
);
2327 sendevent(i
->win
, xatom
[Xembed
], StructureNotifyMask
, CurrentTime
, code
, 0,
2328 systray
->win
, XEMBED_EMBEDDED_VERSION
);
2334 XSetWindowAttributes wa
;
2337 Monitor
*m
= systraytomon(NULL
);
2338 unsigned int x
= m
->mx
+ m
->mw
;
2345 if (!(systray
= (Systray
*)calloc(1, sizeof(Systray
))))
2346 die("fatal: could not malloc() %u bytes\n", sizeof(Systray
));
2347 systray
->win
= XCreateSimpleWindow(dpy
, root
, x
, m
->by
, w
, bh
, 0, 0, scheme
[SchemeSel
][ColBg
].pixel
);
2348 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
2349 wa
.override_redirect
= True
;
2350 wa
.background_pixel
= scheme
[SchemeNorm
][ColBg
].pixel
;
2351 XSelectInput(dpy
, systray
->win
, SubstructureNotifyMask
);
2352 XChangeProperty(dpy
, systray
->win
, netatom
[NetSystemTrayOrientation
], XA_CARDINAL
, 32,
2353 PropModeReplace
, (unsigned char *)&netatom
[NetSystemTrayOrientationHorz
], 1);
2354 XChangeWindowAttributes(dpy
, systray
->win
, CWEventMask
|CWOverrideRedirect
|CWBackPixel
, &wa
);
2355 XMapRaised(dpy
, systray
->win
);
2356 XSetSelectionOwner(dpy
, netatom
[NetSystemTray
], systray
->win
, CurrentTime
);
2357 if (XGetSelectionOwner(dpy
, netatom
[NetSystemTray
]) == systray
->win
) {
2358 sendevent(root
, xatom
[Manager
], StructureNotifyMask
, CurrentTime
, netatom
[NetSystemTray
], systray
->win
, 0, 0);
2362 fprintf(stderr
, "dwm: unable to obtain system tray.\n");
2368 for (w
= 0, i
= systray
->icons
; i
; i
= i
->next
) {
2369 /* make sure the background color stays the same */
2370 wa
.background_pixel
= scheme
[SchemeNorm
][ColBg
].pixel
;
2371 XChangeWindowAttributes(dpy
, i
->win
, CWBackPixel
, &wa
);
2372 XMapRaised(dpy
, i
->win
);
2373 w
+= systrayspacing
;
2375 XMoveResizeWindow(dpy
, i
->win
, i
->x
, 0, i
->w
, i
->h
);
2380 w
= w
? w
+ systrayspacing
: 1;
2382 XMoveResizeWindow(dpy
, systray
->win
, x
, m
->by
, w
, bh
);
2383 wc
.x
= x
; wc
.y
= m
->by
; wc
.width
= w
; wc
.height
= bh
;
2384 wc
.stack_mode
= Above
; wc
.sibling
= m
->barwin
;
2385 XConfigureWindow(dpy
, systray
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWSibling
|CWStackMode
, &wc
);
2386 XMapWindow(dpy
, systray
->win
);
2387 XMapSubwindows(dpy
, systray
->win
);
2388 /* redraw background */
2389 XSetForeground(dpy
, drw
->gc
, scheme
[SchemeNorm
][ColBg
].pixel
);
2390 XFillRectangle(dpy
, systray
->win
, drw
->gc
, 0, 0, w
, bh
);
2395 updatetitle(Client
*c
)
2397 if (!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
2398 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
2399 if (c
->name
[0] == '\0') /* hack to mark broken clients */
2400 strcpy(c
->name
, broken
);
2404 updatewindowtype(Client
*c
)
2406 Atom state
= getatomprop(c
, netatom
[NetWMState
]);
2407 Atom wtype
= getatomprop(c
, netatom
[NetWMWindowType
]);
2409 if (state
== netatom
[NetWMFullscreen
])
2410 setfullscreen(c
, 1);
2411 if (wtype
== netatom
[NetWMWindowTypeDialog
])
2416 updatewmhints(Client
*c
)
2420 if ((wmh
= XGetWMHints(dpy
, c
->win
))) {
2421 if (c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
2422 wmh
->flags
&= ~XUrgencyHint
;
2423 XSetWMHints(dpy
, c
->win
, wmh
);
2425 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? 1 : 0;
2426 if (wmh
->flags
& InputHint
)
2427 c
->neverfocus
= !wmh
->input
;
2435 view(const Arg
*arg
)
2438 unsigned int tmptag
;
2440 if ((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
2442 selmon
->seltags
^= 1; /* toggle sel tagset */
2443 if (arg
->ui
& TAGMASK
) {
2444 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
2445 selmon
->pertag
->prevtag
= selmon
->pertag
->curtag
;
2448 selmon
->pertag
->curtag
= 0;
2450 for (i
= 0; !(arg
->ui
& 1 << i
); i
++) ;
2451 selmon
->pertag
->curtag
= i
+ 1;
2454 tmptag
= selmon
->pertag
->prevtag
;
2455 selmon
->pertag
->prevtag
= selmon
->pertag
->curtag
;
2456 selmon
->pertag
->curtag
= tmptag
;
2459 selmon
->nmaster
= selmon
->pertag
->nmasters
[selmon
->pertag
->curtag
];
2460 selmon
->mfact
= selmon
->pertag
->mfacts
[selmon
->pertag
->curtag
];
2461 selmon
->sellt
= selmon
->pertag
->sellts
[selmon
->pertag
->curtag
];
2462 selmon
->lt
[selmon
->sellt
] = selmon
->pertag
->ltidxs
[selmon
->pertag
->curtag
][selmon
->sellt
];
2463 selmon
->lt
[selmon
->sellt
^1] = selmon
->pertag
->ltidxs
[selmon
->pertag
->curtag
][selmon
->sellt
^1];
2465 if (selmon
->showbar
!= selmon
->pertag
->showbars
[selmon
->pertag
->curtag
])
2473 wintoclient(Window w
)
2478 for (m
= mons
; m
; m
= m
->next
)
2479 for (c
= m
->clients
; c
; c
= c
->next
)
2486 wintosystrayicon(Window w
) {
2489 if (!showsystray
|| !w
)
2491 for (i
= systray
->icons
; i
&& i
->win
!= w
; i
= i
->next
) ;
2502 if (w
== root
&& getrootptr(&x
, &y
))
2503 return recttomon(x
, y
, 1, 1);
2504 for (m
= mons
; m
; m
= m
->next
)
2507 if ((c
= wintoclient(w
)))
2512 /* There's no way to check accesses to destroyed windows, thus those cases are
2513 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2514 * default error handler, which may call exit. */
2516 xerror(Display
*dpy
, XErrorEvent
*ee
)
2518 if (ee
->error_code
== BadWindow
2519 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2520 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2521 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2522 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2523 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2524 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2525 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2526 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2528 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2529 ee
->request_code
, ee
->error_code
);
2530 return xerrorxlib(dpy
, ee
); /* may call exit */
2534 xerrordummy(Display
*dpy
, XErrorEvent
*ee
)
2539 /* Startup Error handler to check if another window manager
2540 * is already running. */
2542 xerrorstart(Display
*dpy
, XErrorEvent
*ee
)
2544 die("dwm: another window manager is already running");
2549 systraytomon(Monitor
*m
) {
2552 if(!systraypinning
) {
2555 return m
== selmon
? m
: NULL
;
2557 for(n
= 1, t
= mons
; t
&& t
->next
; n
++, t
= t
->next
) ;
2558 for(i
= 1, t
= mons
; t
&& t
->next
&& i
< systraypinning
; i
++, t
= t
->next
) ;
2559 if(systraypinningfailfirst
&& n
< systraypinning
)
2565 zoom(const Arg
*arg
)
2567 Client
*c
= selmon
->sel
;
2569 if (!selmon
->lt
[selmon
->sellt
]->arrange
2570 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2572 if (c
== nexttiled(selmon
->clients
))
2573 if (!c
|| !(c
= nexttiled(c
->next
)))
2579 main(int argc
, char *argv
[])
2581 if (argc
== 2 && !strcmp("-v", argv
[1]))
2584 die("usage: dwm [-v]");
2585 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2586 fputs("warning: no locale support\n", stderr
);
2587 if (!(dpy
= XOpenDisplay(NULL
)))
2588 die("dwm: cannot open display");
2592 if (pledge("stdio rpath proc exec", NULL
) == -1)
2594 #endif /* __OpenBSD__ */
2599 return EXIT_SUCCESS
;