Xinqi Bao's Git
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
10 #include <X11/cursorfont.h>
11 #include <X11/Xatom.h>
12 #include <X11/Xproto.h>
16 /********** CUSTOMIZE **********/
19 [Tscratch
] = "scratch",
26 /********** CUSTOMIZE **********/
31 Atom wm_atom
[WMLast
], net_atom
[NetLast
];
32 Cursor cursor
[CurLast
];
37 int tsel
= Tdev
; /* default tag */
38 int screen
, sx
, sy
, sw
, sh
, th
;
41 Client
*clients
= NULL
;
44 static Bool other_wm_running
;
45 static const char version
[] =
46 "dwm - " VERSION
", (C)opyright MMVI Anselm R. Garbe\n";
47 static int (*x_error_handler
) (Display
*, XErrorEvent
*);
50 usage() { error("usage: dwm [-v]\n"); }
60 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
61 for(i
= 0; i
< num
; i
++) {
62 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
64 if(wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
66 if(wa
.map_state
== IsViewable
)
75 win_property(Window w
, Atom a
, Atom t
, long l
, unsigned char **prop
)
79 unsigned long res
, extra
;
82 status
= XGetWindowProperty(dpy
, w
, a
, 0L, l
, False
, t
, &real
, &format
,
85 if(status
!= Success
|| *prop
== 0) {
97 unsigned char *protocols
;
102 res
= win_property(w
, wm_atom
[WMProtocols
], XA_ATOM
, 20L, &protocols
);
106 for(i
= 0; i
< res
; i
++) {
107 if(protocols
[i
] == wm_atom
[WMDelete
])
108 protos
|= WM_PROTOCOL_DELWIN
;
110 free((char *) protocols
);
115 send_message(Window w
, Atom a
, long value
)
119 e
.type
= ClientMessage
;
120 e
.xclient
.window
= w
;
121 e
.xclient
.message_type
= a
;
122 e
.xclient
.format
= 32;
123 e
.xclient
.data
.l
[0] = value
;
124 e
.xclient
.data
.l
[1] = CurrentTime
;
125 XSendEvent(dpy
, w
, False
, NoEventMask
, &e
);
130 * There's no way to check accesses to destroyed windows, thus
131 * those cases are ignored (especially on UnmapNotify's).
132 * Other types of errors call Xlib's default error handler, which
136 error_handler(Display
*dpy
, XErrorEvent
*error
)
138 if(error
->error_code
== BadWindow
139 || (error
->request_code
== X_SetInputFocus
140 && error
->error_code
== BadMatch
)
141 || (error
->request_code
== X_PolyText8
142 && error
->error_code
== BadDrawable
)
143 || (error
->request_code
== X_PolyFillRectangle
144 && error
->error_code
== BadDrawable
)
145 || (error
->request_code
== X_PolySegment
146 && error
->error_code
== BadDrawable
)
147 || (error
->request_code
== X_ConfigureWindow
148 && error
->error_code
== BadMatch
)
149 || (error
->request_code
== X_GrabKey
150 && error
->error_code
== BadAccess
))
152 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
153 error
->request_code
, error
->error_code
);
154 return x_error_handler(dpy
, error
); /* may call exit() */
158 * Startup Error handler to check if another window manager
159 * is already running.
162 startup_error_handler(Display
*dpy
, XErrorEvent
*error
)
164 other_wm_running
= True
;
173 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
183 main(int argc
, char *argv
[])
186 XSetWindowAttributes wa
;
191 /* command line args */
192 for(i
= 1; (i
< argc
) && (argv
[i
][0] == '-'); i
++) {
193 switch (argv
[i
][1]) {
195 fprintf(stdout
, "%s", version
);
204 dpy
= XOpenDisplay(0);
206 error("dwm: cannot connect X server\n");
208 screen
= DefaultScreen(dpy
);
209 root
= RootWindow(dpy
, screen
);
211 /* check if another WM is already running */
212 other_wm_running
= False
;
213 XSetErrorHandler(startup_error_handler
);
214 /* this causes an error if some other WM is running */
215 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
219 error("dwm: another window manager is already running\n");
222 sw
= DisplayWidth(dpy
, screen
);
223 sh
= DisplayHeight(dpy
, screen
);
224 issel
= XQueryPointer(dpy
, root
, &w
, &w
, &i
, &i
, &i
, &i
, &mask
);
227 x_error_handler
= XSetErrorHandler(error_handler
);
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
);
235 XChangeProperty(dpy
, root
, net_atom
[NetSupported
], XA_ATOM
, 32,
236 PropModeReplace
, (unsigned char *) net_atom
, NetLast
);
240 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
241 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
242 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
247 initcolors(BGCOLOR
, FGCOLOR
, BORDERCOLOR
);
248 initfont(&dc
.font
, FONT
);
250 th
= texth(&dc
.font
);
252 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, th
, DefaultDepth(dpy
, screen
));
253 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
255 wa
.event_mask
= SubstructureRedirectMask
| EnterWindowMask \
257 wa
.cursor
= cursor
[CurNormal
];
258 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
263 XNextEvent(dpy
, &ev
);
265 (handler
[ev
.type
])(&ev
); /* call handler */