Xinqi Bao's Git
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
12 #include <sys/select.h>
13 #include <X11/cursorfont.h>
14 #include <X11/Xatom.h>
15 #include <X11/Xproto.h>
19 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
26 resize(sel
, True
, TopLeft
);
29 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
39 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
40 for(i
= 0; i
< num
; i
++) {
41 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
43 if(wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
45 if(wa
.map_state
== IsViewable
)
54 win_property(Window w
, Atom a
, Atom t
, long l
, unsigned char **prop
)
57 unsigned long res
, extra
;
60 status
= XGetWindowProperty(dpy
, w
, a
, 0L, l
, False
, t
, &real
, &format
,
63 if(status
!= Success
|| *prop
== 0) {
73 * Startup Error handler to check if another window manager
77 xerrorstart(Display
*dsply
, XErrorEvent
*ee
)
87 int screen
, sx
, sy
, sw
, sh
, bx
, by
, bw
, bh
, mw
;
89 Atom wmatom
[WMLast
], netatom
[NetLast
];
92 Client
*clients
= NULL
;
94 Cursor cursor
[CurLast
];
107 res
= win_property(w
, wmatom
[WMProtocols
], XA_ATOM
, 20L,
108 ((unsigned char **)&protocols
));
112 for(i
= 0; i
< res
; i
++) {
113 if(protocols
[i
] == wmatom
[WMDelete
])
114 protos
|= PROTODELWIN
;
116 free((char *) protocols
);
121 sendevent(Window w
, Atom a
, long value
)
125 e
.type
= ClientMessage
;
126 e
.xclient
.window
= w
;
127 e
.xclient
.message_type
= a
;
128 e
.xclient
.format
= 32;
129 e
.xclient
.data
.l
[0] = value
;
130 e
.xclient
.data
.l
[1] = CurrentTime
;
131 XSendEvent(dpy
, w
, False
, NoEventMask
, &e
);
142 * There's no way to check accesses to destroyed windows, thus those cases are
143 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
144 * default error handler, which calls exit().
147 xerror(Display
*dpy
, XErrorEvent
*ee
)
149 if(ee
->error_code
== BadWindow
150 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
151 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
152 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
153 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
154 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
155 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
))
157 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
158 ee
->request_code
, ee
->error_code
);
159 return xerrorxlib(dpy
, ee
); /* may call exit() */
163 main(int argc
, char *argv
[])
171 XSetWindowAttributes wa
;
173 if(argc
== 2 && !strncmp("-v", argv
[1], 3)) {
174 fputs("dwm-"VERSION
", (C)opyright MMVI Anselm R. Garbe\n", stdout
);
178 eprint("usage: dwm [-v]\n");
180 dpy
= XOpenDisplay(0);
182 eprint("dwm: cannot open display\n");
184 screen
= DefaultScreen(dpy
);
185 root
= RootWindow(dpy
, screen
);
188 XSetErrorHandler(xerrorstart
);
189 /* this causes an error if some other window manager is running */
190 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
194 eprint("dwm: another window manager is already running\n");
196 XSetErrorHandler(NULL
);
197 xerrorxlib
= XSetErrorHandler(xerror
);
200 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
201 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
202 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
203 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
204 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
205 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
208 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
209 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
210 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
215 for(ntags
= 0; tags
[ntags
]; ntags
++);
218 dc
.bg
= getcolor(BGCOLOR
);
219 dc
.fg
= getcolor(FGCOLOR
);
220 dc
.border
= getcolor(BORDERCOLOR
);
224 sw
= DisplayWidth(dpy
, screen
);
225 sh
= DisplayHeight(dpy
, screen
);
226 mw
= (sw
* MASTERW
) / 100;
228 wa
.override_redirect
= 1;
229 wa
.background_pixmap
= ParentRelative
;
230 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
234 dc
.h
= bh
= dc
.font
.height
+ 4;
235 barwin
= XCreateWindow(dpy
, root
, bx
, by
, bw
, bh
, 0, DefaultDepth(dpy
, screen
),
236 CopyFromParent
, DefaultVisual(dpy
, screen
),
237 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
238 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
239 XMapRaised(dpy
, barwin
);
241 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
242 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
244 strcpy(stext
, "dwm-"VERSION
);
247 issel
= XQueryPointer(dpy
, root
, &w
, &w
, &i
, &i
, &i
, &i
, &mask
);
249 wa
.event_mask
= SubstructureRedirectMask
| EnterWindowMask
| LeaveWindowMask
;
250 wa
.cursor
= cursor
[CurNormal
];
251 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
255 /* main event loop, also reads status text from stdin */
259 FD_SET(STDIN_FILENO
, &rd
);
260 FD_SET(ConnectionNumber(dpy
), &rd
);
262 i
= select(ConnectionNumber(dpy
) + 1, &rd
, 0, 0, 0);
263 if(i
== -1 && errno
== EINTR
)
266 eprint("select failed\n");
268 if(FD_ISSET(ConnectionNumber(dpy
), &rd
)) {
269 while(XPending(dpy
)) {
270 XNextEvent(dpy
, &ev
);
272 (handler
[ev
.type
])(&ev
); /* call handler */
275 if(readin
&& FD_ISSET(STDIN_FILENO
, &rd
)) {
276 readin
= NULL
!= fgets(stext
, sizeof(stext
), stdin
);
278 stext
[strlen(stext
) - 1] = 0;
280 strcpy(stext
, "broken pipe");