Xinqi Bao's Git
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_error_handler
) (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 send_message(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 error_handler(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_error_handler(dpy
, error
); /* may call exit() */
160 * Startup Error handler to check if another window manager
161 * is already running.
164 startup_error_handler(Display
*dpy
, XErrorEvent
*error
)
166 other_wm_running
= True
;
177 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
187 main(int argc
, char *argv
[])
191 XSetWindowAttributes wa
;
196 for(i
= 1; (i
< argc
) && (argv
[i
][0] == '-'); i
++) {
197 switch (argv
[i
][1]) {
199 fprintf(stdout
, "%s", version
);
208 dpy
= XOpenDisplay(0);
210 error("dwm: cannot connect X server\n");
212 screen
= DefaultScreen(dpy
);
213 root
= RootWindow(dpy
, screen
);
215 /* check if another WM is already running */
216 other_wm_running
= False
;
217 XSetErrorHandler(startup_error_handler
);
218 /* this causes an error if some other WM is running */
219 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
223 error("dwm: another window manager is already running\n");
226 x_error_handler
= XSetErrorHandler(error_handler
);
229 wm_atom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
230 wm_atom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
231 net_atom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
232 net_atom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
233 XChangeProperty(dpy
, root
, net_atom
[NetSupported
], XA_ATOM
, 32,
234 PropModeReplace
, (unsigned char *) net_atom
, NetLast
);
237 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
238 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
239 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
244 dc
.bg
= initcolor(BGCOLOR
);
245 dc
.fg
= initcolor(FGCOLOR
);
246 dc
.border
= initcolor(BORDERCOLOR
);
250 sw
= DisplayWidth(dpy
, screen
);
251 sh
= DisplayHeight(dpy
, screen
);
252 mw
= (sw
* MASTERW
) / 100;
254 wa
.override_redirect
= 1;
255 wa
.background_pixmap
= ParentRelative
;
256 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
260 dc
.h
= bh
= dc
.font
.height
+ 4;
261 barwin
= XCreateWindow(dpy
, root
, bx
, by
, bw
, bh
, 0, DefaultDepth(dpy
, screen
),
262 CopyFromParent
, DefaultVisual(dpy
, screen
),
263 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
264 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
265 XMapRaised(dpy
, barwin
);
267 issel
= XQueryPointer(dpy
, root
, &w
, &w
, &i
, &i
, &i
, &i
, &mask
);
269 wa
.event_mask
= SubstructureRedirectMask
| EnterWindowMask \
271 wa
.cursor
= cursor
[CurNormal
];
273 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
275 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
276 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
278 strcpy(stext
, "dwm-"VERSION
);
282 /* main event loop, reads status text from stdin as well */
286 FD_SET(ConnectionNumber(dpy
), &rd
);
288 i
= select(ConnectionNumber(dpy
) + 1, &rd
, 0, 0, 0);
289 if(i
== -1 && errno
== EINTR
)
292 error("select failed\n");
294 if(FD_ISSET(ConnectionNumber(dpy
), &rd
) && XPending(dpy
) > 0) {
295 XNextEvent(dpy
, &ev
);
297 (handler
[ev
.type
])(&ev
); /* call handler */
299 if(FD_ISSET(0, &rd
)) {
301 while((i
= getchar()) != '\n' && n
< sizeof(stext
) - 1)