Xinqi Bao's Git
a1cf245e34e4006d4194cb346f4f35876599bda6
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 /********** CUSTOMIZE **********/
22 [Tscratch
] = "scratch",
28 /********** CUSTOMIZE **********/
33 Atom wm_atom
[WMLast
], net_atom
[NetLast
];
34 Cursor cursor
[CurLast
];
38 int tsel
= Tdev
; /* default tag */
39 int screen
, sx
, sy
, sw
, sh
, bx
, by
, bw
, bh
, mw
;
43 Client
*clients
= NULL
;
46 static Bool other_wm_running
;
47 static const char version
[] =
48 "dwm-" VERSION
", (C)opyright MMVI Anselm R. Garbe\n";
49 static int (*x_xerror
) (Display
*, XErrorEvent
*);
52 usage() { error("usage: dwm [-v]\n"); }
62 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
63 for(i
= 0; i
< num
; i
++) {
64 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
66 if(wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
68 if(wa
.map_state
== IsViewable
)
77 win_property(Window w
, Atom a
, Atom t
, long l
, unsigned char **prop
)
81 unsigned long res
, extra
;
84 status
= XGetWindowProperty(dpy
, w
, a
, 0L, l
, False
, t
, &real
, &format
,
87 if(status
!= Success
|| *prop
== 0) {
99 unsigned char *protocols
;
104 res
= win_property(w
, wm_atom
[WMProtocols
], XA_ATOM
, 20L, &protocols
);
108 for(i
= 0; i
< res
; i
++) {
109 if(protocols
[i
] == wm_atom
[WMDelete
])
110 protos
|= WM_PROTOCOL_DELWIN
;
112 free((char *) protocols
);
117 sendevent(Window w
, Atom a
, long value
)
121 e
.type
= ClientMessage
;
122 e
.xclient
.window
= w
;
123 e
.xclient
.message_type
= a
;
124 e
.xclient
.format
= 32;
125 e
.xclient
.data
.l
[0] = value
;
126 e
.xclient
.data
.l
[1] = CurrentTime
;
127 XSendEvent(dpy
, w
, False
, NoEventMask
, &e
);
132 * There's no way to check accesses to destroyed windows, thus
133 * those cases are ignored (especially on UnmapNotify's).
134 * Other types of errors call Xlib's default error handler, which
138 xerror(Display
*dpy
, XErrorEvent
*error
)
140 if(error
->error_code
== BadWindow
141 || (error
->request_code
== X_SetInputFocus
142 && error
->error_code
== BadMatch
)
143 || (error
->request_code
== X_PolyText8
144 && error
->error_code
== BadDrawable
)
145 || (error
->request_code
== X_PolyFillRectangle
146 && error
->error_code
== BadDrawable
)
147 || (error
->request_code
== X_PolySegment
148 && error
->error_code
== BadDrawable
)
149 || (error
->request_code
== X_ConfigureWindow
150 && error
->error_code
== BadMatch
)
151 || (error
->request_code
== X_GrabKey
152 && error
->error_code
== BadAccess
))
154 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
155 error
->request_code
, error
->error_code
);
156 return x_xerror(dpy
, error
); /* may call exit() */
160 * Startup Error handler to check if another window manager
161 * is already running.
164 startup_xerror(Display
*dpy
, XErrorEvent
*error
)
166 other_wm_running
= True
;
177 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
187 main(int argc
, char *argv
[])
191 XSetWindowAttributes wa
;
193 Bool readstdin
= True
;
197 for(i
= 1; (i
< argc
) && (argv
[i
][0] == '-'); i
++) {
198 switch (argv
[i
][1]) {
200 fprintf(stdout
, "%s", version
);
209 dpy
= XOpenDisplay(0);
211 error("dwm: cannot connect X server\n");
213 screen
= DefaultScreen(dpy
);
214 root
= RootWindow(dpy
, screen
);
216 /* check if another WM is already running */
217 other_wm_running
= False
;
218 XSetErrorHandler(startup_xerror
);
219 /* this causes an error if some other WM is running */
220 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
224 error("dwm: another window manager is already running\n");
227 x_xerror
= XSetErrorHandler(xerror
);
230 wm_atom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
231 wm_atom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
232 net_atom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
233 net_atom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
234 XChangeProperty(dpy
, root
, net_atom
[NetSupported
], XA_ATOM
, 32,
235 PropModeReplace
, (unsigned char *) net_atom
, NetLast
);
238 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
239 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
240 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
245 dc
.bg
= getcolor(BGCOLOR
);
246 dc
.fg
= getcolor(FGCOLOR
);
247 dc
.border
= getcolor(BORDERCOLOR
);
251 sw
= DisplayWidth(dpy
, screen
);
252 sh
= DisplayHeight(dpy
, screen
);
253 mw
= (sw
* MASTERW
) / 100;
255 wa
.override_redirect
= 1;
256 wa
.background_pixmap
= ParentRelative
;
257 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
261 dc
.h
= bh
= dc
.font
.height
+ 4;
262 barwin
= XCreateWindow(dpy
, root
, bx
, by
, bw
, bh
, 0, DefaultDepth(dpy
, screen
),
263 CopyFromParent
, DefaultVisual(dpy
, screen
),
264 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
265 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
266 XMapRaised(dpy
, barwin
);
268 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
269 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
272 issel
= XQueryPointer(dpy
, root
, &w
, &w
, &i
, &i
, &i
, &i
, &mask
);
274 wa
.event_mask
= SubstructureRedirectMask
| EnterWindowMask \
276 wa
.cursor
= cursor
[CurNormal
];
278 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
280 strcpy(stext
, "dwm-"VERSION
);
283 /* main event loop, reads status text from stdin as well */
288 FD_SET(STDIN_FILENO
, &rd
);
289 FD_SET(ConnectionNumber(dpy
), &rd
);
291 i
= select(ConnectionNumber(dpy
) + 1, &rd
, 0, 0, 0);
292 if(i
== -1 && errno
== EINTR
)
295 error("select failed\n");
297 if(FD_ISSET(ConnectionNumber(dpy
), &rd
)) {
298 while(XPending(dpy
)) {
299 XNextEvent(dpy
, &ev
);
301 (handler
[ev
.type
])(&ev
); /* call handler */
304 if(readstdin
&& FD_ISSET(STDIN_FILENO
, &rd
)) {
307 if((i
= getchar()) == EOF
) {
308 /* broken pipe/end of producer */
310 strcpy(stext
, "broken pipe");
313 if(i
== '\n' || n
>= sizeof(stext
) - 1)