Xinqi Bao's Git
2d132a5d84d50922843a6acdeea10afb7cda3259
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
13 #include <X11/cursorfont.h>
14 #include <X11/Xatom.h>
15 #include <X11/Xproto.h>
19 Atom wmatom
[WMLast
], netatom
[NetLast
];
20 Cursor cursor
[CurLast
];
24 int tsel
= Tdev
; /* default tag */
25 int screen
, sx
, sy
, sw
, sh
, bx
, by
, bw
, bh
, mw
;
29 Client
*clients
= NULL
;
33 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
35 /* static functions */
44 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
55 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
56 for(i
= 0; i
< num
; i
++) {
57 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
59 if(wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
61 if(wa
.map_state
== IsViewable
)
70 win_property(Window w
, Atom a
, Atom t
, long l
, unsigned char **prop
)
74 unsigned long res
, extra
;
77 status
= XGetWindowProperty(dpy
, w
, a
, 0L, l
, False
, t
, &real
, &format
,
80 if(status
!= Success
|| *prop
== 0) {
90 * Startup Error handler to check if another window manager
94 xerrorstart(Display
*dsply
, XErrorEvent
*ee
)
100 /* extern functions */
105 unsigned char *protocols
;
110 res
= win_property(w
, wmatom
[WMProtocols
], XA_ATOM
, 20L, &protocols
);
114 for(i
= 0; i
< res
; i
++) {
115 if(protocols
[i
] == wmatom
[WMDelete
])
116 protos
|= WM_PROTOCOL_DELWIN
;
118 free((char *) protocols
);
123 sendevent(Window w
, Atom a
, long value
)
127 e
.type
= ClientMessage
;
128 e
.xclient
.window
= w
;
129 e
.xclient
.message_type
= a
;
130 e
.xclient
.format
= 32;
131 e
.xclient
.data
.l
[0] = value
;
132 e
.xclient
.data
.l
[1] = CurrentTime
;
133 XSendEvent(dpy
, w
, False
, NoEventMask
, &e
);
144 * There's no way to check accesses to destroyed windows, thus
145 * those cases are ignored (especially on UnmapNotify's).
146 * Other types of errors call Xlib's default error handler, which
150 xerror(Display
*dpy
, XErrorEvent
*ee
)
152 if(ee
->error_code
== BadWindow
153 || (ee
->request_code
== X_SetInputFocus
154 && ee
->error_code
== BadMatch
)
155 || (ee
->request_code
== X_PolyText8
156 && ee
->error_code
== BadDrawable
)
157 || (ee
->request_code
== X_PolyFillRectangle
158 && ee
->error_code
== BadDrawable
)
159 || (ee
->request_code
== X_PolySegment
160 && ee
->error_code
== BadDrawable
)
161 || (ee
->request_code
== X_ConfigureWindow
162 && ee
->error_code
== BadMatch
)
163 || (ee
->request_code
== X_GrabKey
164 && ee
->error_code
== BadAccess
))
166 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
167 ee
->request_code
, ee
->error_code
);
168 return xerrorxlib(dpy
, ee
); /* may call exit() */
172 main(int argc
, char *argv
[])
176 XSetWindowAttributes wa
;
178 Bool readstdin
= True
;
182 for(i
= 1; (i
< argc
) && (argv
[i
][0] == '-'); i
++) {
183 switch (argv
[i
][1]) {
185 fprintf(stdout
, "%s",
186 "dwm-"VERSION
", (C)opyright MMVI Anselm R. Garbe\n");
190 eprint("usage: dwm [-v]\n");
195 dpy
= XOpenDisplay(0);
197 eprint("dwm: cannot connect X server\n");
199 screen
= DefaultScreen(dpy
);
200 root
= RootWindow(dpy
, screen
);
202 /* check if another WM is already running */
204 XSetErrorHandler(xerrorstart
);
205 /* this causes an error if some other WM is running */
206 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
210 eprint("dwm: another window manager is already running\n");
212 XSetErrorHandler(NULL
);
213 xerrorxlib
= XSetErrorHandler(xerror
);
216 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
217 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
218 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
219 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
220 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
221 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
224 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
225 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
226 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
231 dc
.bg
= getcolor(BGCOLOR
);
232 dc
.fg
= getcolor(FGCOLOR
);
233 dc
.border
= getcolor(BORDERCOLOR
);
237 sw
= DisplayWidth(dpy
, screen
);
238 sh
= DisplayHeight(dpy
, screen
);
239 mw
= (sw
* MASTERW
) / 100;
241 wa
.override_redirect
= 1;
242 wa
.background_pixmap
= ParentRelative
;
243 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
247 dc
.h
= bh
= dc
.font
.height
+ 4;
248 barwin
= XCreateWindow(dpy
, root
, bx
, by
, bw
, bh
, 0, DefaultDepth(dpy
, screen
),
249 CopyFromParent
, DefaultVisual(dpy
, screen
),
250 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
251 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
252 XMapRaised(dpy
, barwin
);
254 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
255 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
258 issel
= XQueryPointer(dpy
, root
, &w
, &w
, &i
, &i
, &i
, &i
, &mask
);
260 wa
.event_mask
= SubstructureRedirectMask
| EnterWindowMask \
262 wa
.cursor
= cursor
[CurNormal
];
264 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
266 strcpy(stext
, "dwm-"VERSION
);
269 /* main event loop, reads status text from stdin as well */
274 FD_SET(STDIN_FILENO
, &rd
);
275 FD_SET(ConnectionNumber(dpy
), &rd
);
277 i
= select(ConnectionNumber(dpy
) + 1, &rd
, 0, 0, 0);
278 if(i
== -1 && errno
== EINTR
)
281 eprint("select failed\n");
283 if(FD_ISSET(ConnectionNumber(dpy
), &rd
)) {
284 while(XPending(dpy
)) {
285 XNextEvent(dpy
, &ev
);
287 (handler
[ev
.type
])(&ev
); /* call handler */
290 if(readstdin
&& FD_ISSET(STDIN_FILENO
, &rd
)) {
293 if((i
= getchar()) == EOF
) {
294 /* broken pipe/end of producer */
296 strcpy(stext
, "broken pipe");
299 if(i
== '\n' || n
>= sizeof(stext
) - 1)