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
);
31 XFreeFontSet(dpy
, dc
.font
.set
);
33 XFreeFont(dpy
, dc
.font
.xfont
);
34 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
35 XDestroyWindow(dpy
, barwin
);
36 XFreePixmap(dpy
, dc
.drawable
);
38 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
50 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
51 for(i
= 0; i
< num
; i
++) {
52 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
54 if(wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
56 if(wa
.map_state
== IsViewable
)
65 win_property(Window w
, Atom a
, Atom t
, long l
, unsigned char **prop
)
68 unsigned long res
, extra
;
71 status
= XGetWindowProperty(dpy
, w
, a
, 0L, l
, False
, t
, &real
, &format
,
74 if(status
!= Success
|| *prop
== 0) {
84 * Startup Error handler to check if another window manager
88 xerrorstart(Display
*dsply
, XErrorEvent
*ee
)
98 int screen
, sx
, sy
, sw
, sh
, bx
, by
, bw
, bh
, mw
;
99 unsigned int ntags
, numlockmask
;
100 Atom wmatom
[WMLast
], netatom
[NetLast
];
103 Client
*clients
= NULL
;
105 Cursor cursor
[CurLast
];
118 res
= win_property(w
, wmatom
[WMProtocols
], XA_ATOM
, 20L,
119 ((unsigned char **)&protocols
));
123 for(i
= 0; i
< res
; i
++) {
124 if(protocols
[i
] == wmatom
[WMDelete
])
125 protos
|= PROTODELWIN
;
127 free((char *) protocols
);
132 sendevent(Window w
, Atom a
, long value
)
136 e
.type
= ClientMessage
;
137 e
.xclient
.window
= w
;
138 e
.xclient
.message_type
= a
;
139 e
.xclient
.format
= 32;
140 e
.xclient
.data
.l
[0] = value
;
141 e
.xclient
.data
.l
[1] = CurrentTime
;
142 XSendEvent(dpy
, w
, False
, NoEventMask
, &e
);
153 * There's no way to check accesses to destroyed windows, thus those cases are
154 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
155 * default error handler, which calls exit().
158 xerror(Display
*dpy
, XErrorEvent
*ee
)
160 if(ee
->error_code
== BadWindow
161 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
162 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
163 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
164 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
165 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
166 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
))
168 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
169 ee
->request_code
, ee
->error_code
);
170 return xerrorxlib(dpy
, ee
); /* may call exit() */
174 main(int argc
, char *argv
[])
181 XModifierKeymap
*modmap
;
182 XSetWindowAttributes wa
;
184 if(argc
== 2 && !strncmp("-v", argv
[1], 3)) {
185 fputs("dwm-"VERSION
", (C)opyright MMVI Anselm R. Garbe\n", stdout
);
189 eprint("usage: dwm [-v]\n");
191 dpy
= XOpenDisplay(0);
193 eprint("dwm: cannot open display\n");
195 xfd
= ConnectionNumber(dpy
);
196 screen
= DefaultScreen(dpy
);
197 root
= RootWindow(dpy
, screen
);
200 XSetErrorHandler(xerrorstart
);
201 /* this causes an error if some other window manager is running */
202 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
206 eprint("dwm: another window manager is already running\n");
209 XSetErrorHandler(NULL
);
210 xerrorxlib
= XSetErrorHandler(xerror
);
214 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
215 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
216 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
217 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
218 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
219 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
222 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
223 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
224 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
226 modmap
= XGetModifierMapping(dpy
);
227 for (i
= 0; i
< 8; i
++) {
228 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
229 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
230 numlockmask
= (1 << i
);
235 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
| EnterWindowMask
| LeaveWindowMask
;
236 wa
.cursor
= cursor
[CurNormal
];
237 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
242 for(ntags
= 0; tags
[ntags
]; ntags
++);
243 seltag
= emallocz(sizeof(Bool
) * ntags
);
244 seltag
[DEFTAG
] = True
;
247 dc
.bg
= getcolor(BGCOLOR
);
248 dc
.fg
= getcolor(FGCOLOR
);
249 dc
.border
= getcolor(BORDERCOLOR
);
253 sw
= DisplayWidth(dpy
, screen
);
254 sh
= DisplayHeight(dpy
, screen
);
255 mw
= (sw
* MASTERW
) / 100;
259 dc
.h
= bh
= dc
.font
.height
+ 4;
260 wa
.override_redirect
= 1;
261 wa
.background_pixmap
= ParentRelative
;
262 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
263 barwin
= XCreateWindow(dpy
, root
, bx
, by
, bw
, bh
, 0, DefaultDepth(dpy
, screen
),
264 CopyFromParent
, DefaultVisual(dpy
, screen
),
265 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
266 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
267 XMapRaised(dpy
, barwin
);
269 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
270 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
272 strcpy(stext
, "dwm-"VERSION
);
275 issel
= XQueryPointer(dpy
, root
, &w
, &w
, &i
, &i
, &i
, &i
, &mask
);
279 /* main event loop, also reads status text from stdin */
285 FD_SET(STDIN_FILENO
, &rd
);
287 i
= select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
);
288 if(i
== -1 && errno
== EINTR
)
291 eprint("select failed\n");
293 if(readin
&& FD_ISSET(STDIN_FILENO
, &rd
)) {
294 readin
= NULL
!= fgets(stext
, sizeof(stext
), stdin
);
296 stext
[strlen(stext
) - 1] = 0;
298 strcpy(stext
, "broken pipe");