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/keysym.h>
15 #include <X11/Xatom.h>
16 #include <X11/Xproto.h>
20 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
27 resize(sel
, True
, TopLeft
);
30 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
31 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
43 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
44 for(i
= 0; i
< num
; i
++) {
45 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
47 if(wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
49 if(wa
.map_state
== IsViewable
)
58 win_property(Window w
, Atom a
, Atom t
, long l
, unsigned char **prop
)
61 unsigned long res
, extra
;
64 status
= XGetWindowProperty(dpy
, w
, a
, 0L, l
, False
, t
, &real
, &format
,
67 if(status
!= Success
|| *prop
== 0) {
77 * Startup Error handler to check if another window manager
81 xerrorstart(Display
*dsply
, XErrorEvent
*ee
)
91 int screen
, sx
, sy
, sw
, sh
, bx
, by
, bw
, bh
, mw
;
92 unsigned int ntags
, numlockmask
;
93 Atom wmatom
[WMLast
], netatom
[NetLast
];
96 Client
*clients
= NULL
;
98 Cursor cursor
[CurLast
];
111 res
= win_property(w
, wmatom
[WMProtocols
], XA_ATOM
, 20L,
112 ((unsigned char **)&protocols
));
116 for(i
= 0; i
< res
; i
++) {
117 if(protocols
[i
] == wmatom
[WMDelete
])
118 protos
|= PROTODELWIN
;
120 free((char *) protocols
);
125 sendevent(Window w
, Atom a
, long value
)
129 e
.type
= ClientMessage
;
130 e
.xclient
.window
= w
;
131 e
.xclient
.message_type
= a
;
132 e
.xclient
.format
= 32;
133 e
.xclient
.data
.l
[0] = value
;
134 e
.xclient
.data
.l
[1] = CurrentTime
;
135 XSendEvent(dpy
, w
, False
, NoEventMask
, &e
);
146 * There's no way to check accesses to destroyed windows, thus those cases are
147 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
148 * default error handler, which calls exit().
151 xerror(Display
*dpy
, XErrorEvent
*ee
)
153 if(ee
->error_code
== BadWindow
154 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
155 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
156 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
157 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
158 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
159 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
))
161 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
162 ee
->request_code
, ee
->error_code
);
163 return xerrorxlib(dpy
, ee
); /* may call exit() */
167 main(int argc
, char *argv
[])
174 XModifierKeymap
*modmap
;
175 XSetWindowAttributes wa
;
177 if(argc
== 2 && !strncmp("-v", argv
[1], 3)) {
178 fputs("dwm-"VERSION
", (C)opyright MMVI Anselm R. Garbe\n", stdout
);
182 eprint("usage: dwm [-v]\n");
184 dpy
= XOpenDisplay(0);
186 eprint("dwm: cannot open display\n");
188 xfd
= ConnectionNumber(dpy
);
189 screen
= DefaultScreen(dpy
);
190 root
= RootWindow(dpy
, screen
);
193 XSetErrorHandler(xerrorstart
);
194 /* this causes an error if some other window manager is running */
195 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
199 eprint("dwm: another window manager is already running\n");
202 XSetErrorHandler(NULL
);
203 xerrorxlib
= XSetErrorHandler(xerror
);
207 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
208 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
209 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
210 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
211 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
212 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
215 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
216 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
217 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
219 modmap
= XGetModifierMapping(dpy
);
220 for (i
= 0; i
< 8; i
++) {
221 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
222 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
223 numlockmask
= (1 << i
);
228 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
| EnterWindowMask
| LeaveWindowMask
;
229 wa
.cursor
= cursor
[CurNormal
];
230 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
235 for(ntags
= 0; tags
[ntags
]; ntags
++);
236 seltag
= emallocz(sizeof(Bool
) * ntags
);
237 seltag
[DEFTAG
] = True
;
240 dc
.bg
= getcolor(BGCOLOR
);
241 dc
.fg
= getcolor(FGCOLOR
);
242 dc
.border
= getcolor(BORDERCOLOR
);
246 sw
= DisplayWidth(dpy
, screen
);
247 sh
= DisplayHeight(dpy
, screen
);
248 mw
= (sw
* MASTERW
) / 100;
252 dc
.h
= bh
= dc
.font
.height
+ 4;
253 wa
.override_redirect
= 1;
254 wa
.background_pixmap
= ParentRelative
;
255 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
256 barwin
= XCreateWindow(dpy
, root
, bx
, by
, bw
, bh
, 0, DefaultDepth(dpy
, screen
),
257 CopyFromParent
, DefaultVisual(dpy
, screen
),
258 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
259 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
260 XMapRaised(dpy
, barwin
);
262 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
263 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
265 strcpy(stext
, "dwm-"VERSION
);
268 issel
= XQueryPointer(dpy
, root
, &w
, &w
, &i
, &i
, &i
, &i
, &mask
);
272 /* main event loop, also reads status text from stdin */
278 FD_SET(STDIN_FILENO
, &rd
);
280 i
= select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
);
281 if(i
== -1 && errno
== EINTR
)
284 eprint("select failed\n");
286 if(readin
&& FD_ISSET(STDIN_FILENO
, &rd
)) {
287 readin
= NULL
!= fgets(stext
, sizeof(stext
), stdin
);
289 stext
[strlen(stext
) - 1] = 0;
291 strcpy(stext
, "broken pipe");