Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
9 #include <sys/select.h>
10 #include <X11/cursorfont.h>
11 #include <X11/keysym.h>
12 #include <X11/Xatom.h>
13 #include <X11/Xproto.h>
18 int screen
, sx
, sy
, sw
, sh
, wax
, way
, waw
, wah
;
19 unsigned int bh
, ntags
;
20 unsigned int bpos
= BARPOS
;
21 unsigned int numlockmask
= 0;
22 Atom dwmtags
, wmatom
[WMLast
], netatom
[NetLast
];
24 Bool selscreen
= True
;
25 Client
*clients
= NULL
;
28 Cursor cursor
[CurLast
];
35 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
36 static Bool otherwm
, readin
;
37 static Bool running
= True
;
47 XFreeFontSet(dpy
, dc
.font
.set
);
49 XFreeFont(dpy
, dc
.font
.xfont
);
50 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
51 XFreePixmap(dpy
, dc
.drawable
);
53 XDestroyWindow(dpy
, barwin
);
54 XFreeCursor(dpy
, cursor
[CurNormal
]);
55 XFreeCursor(dpy
, cursor
[CurResize
]);
56 XFreeCursor(dpy
, cursor
[CurMove
]);
57 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
63 initcolor(const char *colstr
) {
64 Colormap cmap
= DefaultColormap(dpy
, screen
);
67 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
68 eprint("error, cannot allocate color '%s'\n", colstr
);
73 initfont(const char *fontstr
) {
79 XFreeFontSet(dpy
, dc
.font
.set
);
80 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
83 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
84 XFreeStringList(missing
);
87 XFontSetExtents
*font_extents
;
90 dc
.font
.ascent
= dc
.font
.descent
= 0;
91 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
92 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
93 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
94 if(dc
.font
.ascent
< (*xfonts
)->ascent
)
95 dc
.font
.ascent
= (*xfonts
)->ascent
;
96 if(dc
.font
.descent
< (*xfonts
)->descent
)
97 dc
.font
.descent
= (*xfonts
)->descent
;
103 XFreeFont(dpy
, dc
.font
.xfont
);
104 dc
.font
.xfont
= NULL
;
105 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
)))
106 eprint("error, cannot load font: '%s'\n", fontstr
);
107 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
108 dc
.font
.descent
= dc
.font
.xfont
->descent
;
110 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
116 Window
*wins
, d1
, d2
;
117 XWindowAttributes wa
;
120 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
121 for(i
= 0; i
< num
; i
++) {
122 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
123 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
125 if(wa
.map_state
== IsViewable
)
126 manage(wins
[i
], &wa
);
138 XModifierKeymap
*modmap
;
139 XSetWindowAttributes wa
;
142 dwmtags
= XInternAtom(dpy
, "__DWM_TAGS", False
);
143 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
144 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
145 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
146 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
147 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
148 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
149 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
151 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
152 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
153 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
154 /* init modifier map */
155 modmap
= XGetModifierMapping(dpy
);
156 for (i
= 0; i
< 8; i
++)
157 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
158 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
159 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
160 numlockmask
= (1 << i
);
162 XFreeModifiermap(modmap
);
163 /* select for events */
164 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
165 | EnterWindowMask
| LeaveWindowMask
| StructureNotifyMask
;
166 wa
.cursor
= cursor
[CurNormal
];
167 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
168 XSelectInput(dpy
, root
, wa
.event_mask
);
171 for(ntags
= 0; tags
[ntags
]; ntags
++);
172 seltag
= emallocz(sizeof(Bool
) * ntags
);
175 dc
.norm
[ColBorder
] = initcolor(NORMBORDERCOLOR
);
176 dc
.norm
[ColBG
] = initcolor(NORMBGCOLOR
);
177 dc
.norm
[ColFG
] = initcolor(NORMFGCOLOR
);
178 dc
.sel
[ColBorder
] = initcolor(SELBORDERCOLOR
);
179 dc
.sel
[ColBG
] = initcolor(SELBGCOLOR
);
180 dc
.sel
[ColFG
] = initcolor(SELFGCOLOR
);
184 sw
= DisplayWidth(dpy
, screen
);
185 sh
= DisplayHeight(dpy
, screen
);
188 dc
.h
= bh
= dc
.font
.height
+ 2;
189 wa
.override_redirect
= 1;
190 wa
.background_pixmap
= ParentRelative
;
191 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
192 barwin
= XCreateWindow(dpy
, root
, sx
, sy
, sw
, bh
, 0,
193 DefaultDepth(dpy
, screen
), CopyFromParent
, DefaultVisual(dpy
, screen
),
194 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
195 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
197 XMapRaised(dpy
, barwin
);
198 strcpy(stext
, "dwm-"VERSION
);
199 /* pixmap for everything */
200 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
201 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
202 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
204 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
205 /* multihead support */
206 selscreen
= XQueryPointer(dpy
, root
, &w
, &w
, &i
, &i
, &i
, &i
, &mask
);
210 * Startup Error handler to check if another window manager
211 * is already running.
214 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
222 quit(const char *arg
) {
223 readin
= running
= False
;
238 XMoveWindow(dpy
, barwin
, sx
, sy
);
242 XMoveWindow(dpy
, barwin
, sx
, sy
+ wah
);
245 XMoveWindow(dpy
, barwin
, sx
, sy
- bh
);
249 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
252 /* There's no way to check accesses to destroyed windows, thus those cases are
253 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
254 * default error handler, which may call exit.
257 xerror(Display
*dpy
, XErrorEvent
*ee
) {
258 if(ee
->error_code
== BadWindow
259 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
260 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
261 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
262 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
263 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
264 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
265 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
267 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
268 ee
->request_code
, ee
->error_code
);
269 return xerrorxlib(dpy
, ee
); /* may call exit */
273 main(int argc
, char *argv
[]) {
279 if(argc
== 2 && !strcmp("-v", argv
[1]))
280 eprint("dwm-"VERSION
", © 2006-2007 A. R. Garbe, S. van Dijk, J. Salmi, P. Hruby, S. Nagy\n");
282 eprint("usage: dwm [-v]\n");
283 setlocale(LC_CTYPE
, "");
284 if(!(dpy
= XOpenDisplay(0)))
285 eprint("dwm: cannot open display\n");
286 xfd
= ConnectionNumber(dpy
);
287 screen
= DefaultScreen(dpy
);
288 root
= RootWindow(dpy
, screen
);
290 XSetErrorHandler(xerrorstart
);
291 /* this causes an error if some other window manager is running */
292 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
295 eprint("dwm: another window manager is already running\n");
298 XSetErrorHandler(NULL
);
299 xerrorxlib
= XSetErrorHandler(xerror
);
305 /* main event loop, also reads status text from stdin */
311 FD_SET(STDIN_FILENO
, &rd
);
313 if(select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) {
316 eprint("select failed\n");
318 if(FD_ISSET(STDIN_FILENO
, &rd
)) {
319 switch(r
= read(STDIN_FILENO
, stext
, sizeof stext
- 1)) {
321 strncpy(stext
, strerror(errno
), sizeof stext
- 1);
322 stext
[sizeof stext
- 1] = '\0';
326 strncpy(stext
, "EOF", 4);
330 for(stext
[r
] = '\0', p
= stext
+ strlen(stext
) - 1; p
>= stext
&& *p
== '\n'; *p
-- = '\0');
331 for(; p
>= stext
&& *p
!= '\n'; --p
);
333 strncpy(stext
, p
+ 1, sizeof stext
);
337 while(XPending(dpy
)) {
338 XNextEvent(dpy
, &ev
);
340 (handler
[ev
.type
])(&ev
); /* call handler */