Xinqi Bao's Git
9e6f5e31e540cd31623b127f2576856b037d51c2
1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
12 #include <sys/select.h>
13 #include <X11/cursorfont.h>
14 #include <X11/keysym.h>
15 #include <X11/Xatom.h>
16 #include <X11/Xproto.h>
21 int screen
, sx
, sy
, sw
, sh
, wax
, way
, waw
, wah
;
22 unsigned int bh
, ntags
, numlockmask
;
23 Atom wmatom
[WMLast
], netatom
[NetLast
];
26 Bool selscreen
= True
;
27 Client
*clients
= NULL
;
30 Cursor cursor
[CurLast
];
37 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
38 static Bool otherwm
, readin
;
45 XMoveWindow(dpy
, stack
->win
, stack
->x
, stack
->y
);
49 XFreeFontSet(dpy
, dc
.font
.set
);
51 XFreeFont(dpy
, dc
.font
.xfont
);
52 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
53 XFreePixmap(dpy
, dc
.drawable
);
55 XDestroyWindow(dpy
, barwin
);
56 XFreeCursor(dpy
, cursor
[CurNormal
]);
57 XFreeCursor(dpy
, cursor
[CurResize
]);
58 XFreeCursor(dpy
, cursor
[CurMove
]);
59 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
65 textnw(const char *text
, unsigned int len
) {
69 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
72 return XTextWidth(dc
.font
.xfont
, text
, len
);
76 drawsquare(Bool filled
, Bool empty
, unsigned long col
[ColLast
]) {
79 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
81 gcv
.foreground
= col
[ColFG
];
82 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
83 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
87 r
.width
= r
.height
= x
+ 1;
88 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
91 r
.width
= r
.height
= x
;
92 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
97 getcolor(const char *colstr
) {
98 Colormap cmap
= DefaultColormap(dpy
, screen
);
101 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
102 eprint("error, cannot allocate color '%s'\n", colstr
);
107 isoccupied(unsigned int t
) {
110 for(c
= clients
; c
; c
= c
->next
)
119 Window
*wins
, d1
, d2
;
120 XWindowAttributes wa
;
123 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
124 for(i
= 0; i
< num
; i
++) {
125 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
126 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
128 if(wa
.map_state
== IsViewable
)
129 manage(wins
[i
], &wa
);
137 setfont(const char *fontstr
) {
138 char *def
, **missing
;
143 XFreeFontSet(dpy
, dc
.font
.set
);
144 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
147 fprintf(stderr
, "missing fontset: %s\n", missing
[n
]);
148 XFreeStringList(missing
);
151 XFontSetExtents
*font_extents
;
152 XFontStruct
**xfonts
;
154 dc
.font
.ascent
= dc
.font
.descent
= 0;
155 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
156 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
157 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
158 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
159 dc
.font
.ascent
= (*xfonts
)->ascent
;
160 if(dc
.font
.descent
< (*xfonts
)->descent
)
161 dc
.font
.descent
= (*xfonts
)->descent
;
167 XFreeFont(dpy
, dc
.font
.xfont
);
168 dc
.font
.xfont
= NULL
;
169 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
)))
170 eprint("error, cannot load font: '%s'\n", fontstr
);
171 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
172 dc
.font
.descent
= dc
.font
.xfont
->descent
;
174 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
182 XModifierKeymap
*modmap
;
183 XSetWindowAttributes wa
;
186 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
187 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
188 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
189 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
190 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
191 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
192 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
194 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
195 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
196 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
197 /* init modifier map */
199 modmap
= XGetModifierMapping(dpy
);
200 for (i
= 0; i
< 8; i
++)
201 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
202 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
203 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
204 numlockmask
= (1 << i
);
206 XFreeModifiermap(modmap
);
207 /* select for events */
208 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
209 | EnterWindowMask
| LeaveWindowMask
;
210 wa
.cursor
= cursor
[CurNormal
];
211 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
214 for(ntags
= 0; tags
[ntags
]; ntags
++);
215 seltag
= emallocz(sizeof(Bool
) * ntags
);
218 dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
);
219 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
220 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
221 dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
);
222 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
223 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
227 sw
= DisplayWidth(dpy
, screen
);
228 sh
= DisplayHeight(dpy
, screen
);
231 dc
.h
= bh
= dc
.font
.height
+ 2;
232 wa
.override_redirect
= 1;
233 wa
.background_pixmap
= ParentRelative
;
234 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
235 barwin
= XCreateWindow(dpy
, root
, sx
, sy
+ (TOPBAR
? 0 : sh
- bh
), sw
, bh
, 0,
236 DefaultDepth(dpy
, screen
), CopyFromParent
, DefaultVisual(dpy
, screen
),
237 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
238 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
239 XMapRaised(dpy
, barwin
);
240 strcpy(stext
, "dwm-"VERSION
);
243 way
= sy
+ (TOPBAR
? bh
: 0);
246 /* pixmap for everything */
247 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
248 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
249 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
250 /* multihead support */
251 selscreen
= XQueryPointer(dpy
, root
, &w
, &w
, &i
, &i
, &i
, &i
, &mask
);
255 * Startup Error handler to check if another window manager
256 * is already running.
259 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
271 for(i
= 0; i
< ntags
; i
++) {
272 dc
.w
= textw(tags
[i
]);
274 drawtext(tags
[i
], dc
.sel
);
275 drawsquare(sel
&& sel
->tags
[i
], isoccupied(i
), dc
.sel
);
278 drawtext(tags
[i
], dc
.norm
);
279 drawsquare(sel
&& sel
->tags
[i
], isoccupied(i
), dc
.norm
);
284 drawtext(lt
->symbol
, dc
.norm
);
292 drawtext(stext
, dc
.norm
);
293 if((dc
.w
= dc
.x
- x
) > bh
) {
295 drawtext(sel
? sel
->name
: NULL
, sel
? dc
.sel
: dc
.norm
);
297 XCopyArea(dpy
, dc
.drawable
, barwin
, dc
.gc
, 0, 0, sw
, bh
, 0, 0);
302 drawtext(const char *text
, unsigned long col
[ColLast
]) {
304 static char buf
[256];
305 unsigned int len
, olen
;
307 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
309 XSetForeground(dpy
, dc
.gc
, col
[ColBG
]);
310 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
314 olen
= len
= strlen(text
);
315 if(len
>= sizeof buf
)
316 len
= sizeof buf
- 1;
317 memcpy(buf
, text
, len
);
319 h
= dc
.font
.ascent
+ dc
.font
.descent
;
320 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
322 /* shorten text if necessary */
323 while(len
&& (w
= textnw(buf
, len
)) > dc
.w
- h
)
334 return; /* too long */
335 gcv
.foreground
= col
[ColFG
];
337 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
338 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
341 gcv
.font
= dc
.font
.xfont
->fid
;
342 XChangeGC(dpy
, dc
.gc
, GCForeground
| GCFont
, &gcv
);
343 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
348 sendevent(Window w
, Atom a
, long value
) {
351 e
.type
= ClientMessage
;
352 e
.xclient
.window
= w
;
353 e
.xclient
.message_type
= a
;
354 e
.xclient
.format
= 32;
355 e
.xclient
.data
.l
[0] = value
;
356 e
.xclient
.data
.l
[1] = CurrentTime
;
357 XSendEvent(dpy
, w
, False
, NoEventMask
, &e
);
362 textw(const char *text
) {
363 return textnw(text
, strlen(text
)) + dc
.font
.height
;
368 readin
= running
= False
;
371 /* There's no way to check accesses to destroyed windows, thus those cases are
372 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
373 * default error handler, which may call exit.
376 xerror(Display
*dpy
, XErrorEvent
*ee
) {
377 if(ee
->error_code
== BadWindow
378 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
379 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
380 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
381 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
382 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
383 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
384 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
386 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
387 ee
->request_code
, ee
->error_code
);
388 return xerrorxlib(dpy
, ee
); /* may call exit */
392 main(int argc
, char *argv
[]) {
398 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
399 eprint("dwm-"VERSION
", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
401 eprint("usage: dwm [-v]\n");
402 setlocale(LC_CTYPE
, "");
403 if(!(dpy
= XOpenDisplay(0)))
404 eprint("dwm: cannot open display\n");
405 xfd
= ConnectionNumber(dpy
);
406 screen
= DefaultScreen(dpy
);
407 root
= RootWindow(dpy
, screen
);
409 XSetErrorHandler(xerrorstart
);
410 /* this causes an error if some other window manager is running */
411 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
414 eprint("dwm: another window manager is already running\n");
417 XSetErrorHandler(NULL
);
418 xerrorxlib
= XSetErrorHandler(xerror
);
424 /* main event loop, also reads status text from stdin */
430 FD_SET(STDIN_FILENO
, &rd
);
432 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
435 eprint("select failed\n");
437 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
438 switch(r
= read(STDIN_FILENO
, stext
, sizeof stext
- 1)) {
440 strncpy(stext
, strerror(errno
), sizeof stext
- 1);
441 stext
[sizeof stext
- 1] = '\0';
445 strncpy(stext
, "EOF", 4);
449 for(stext
[r
] = '\0', p
= stext
+ strlen(stext
) - 1; p
>= stext
&& *p
== '\n'; *p
-- = '\0');
450 for(; p
>= stext
&& *p
!= '\n'; --p
);
452 strncpy(stext
, p
+ 1, sizeof stext
);
456 if(FD_ISSET(xfd
, &rd
))
457 while(XPending(dpy
)) {
458 XNextEvent(dpy
, &ev
);
460 (handler
[ev
.type
])(&ev
); /* call handler */