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>
22 int bx
, by
, bw
, bh
, bmw
, mw
, screen
, sx
, sy
, sw
, sh
;
23 unsigned int ntags
, numlockmask
;
24 Atom wmatom
[WMLast
], netatom
[NetLast
];
27 Bool maximized
= False
;
28 Client
*clients
= NULL
;
30 Cursor cursor
[CurLast
];
37 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
38 static Bool otherwm
, readin
;
45 resize(sel
, True
, TopLeft
);
49 XFreeFontSet(dpy
, dc
.font
.set
);
51 XFreeFont(dpy
, dc
.font
.xfont
);
52 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
53 XFreePixmap(dpy
, dc
.drawable
);
55 XDestroyWindow(dpy
, barwin
);
56 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
68 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
69 for(i
= 0; i
< num
; i
++) {
70 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
72 if(wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
74 if(wa
.map_state
== IsViewable
)
88 XModifierKeymap
*modmap
;
89 XSetWindowAttributes wa
;
92 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
93 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
94 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
95 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
96 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
97 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
100 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
101 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
102 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
104 modmap
= XGetModifierMapping(dpy
);
105 for (i
= 0; i
< 8; i
++) {
106 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
107 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
108 numlockmask
= (1 << i
);
113 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
| EnterWindowMask
| LeaveWindowMask
;
114 wa
.cursor
= cursor
[CurNormal
];
115 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
120 for(ntags
= 0; tags
[ntags
]; ntags
++);
121 seltag
= emallocz(sizeof(Bool
) * ntags
);
125 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
126 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
127 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
128 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
129 dc
.status
[ColBG
] = getcolor(STATUSBGCOLOR
);
130 dc
.status
[ColFG
] = getcolor(STATUSFGCOLOR
);
133 bmw
= textw(FLOATSYMBOL
) > textw(TILESYMBOL
) ? textw(FLOATSYMBOL
) : textw(TILESYMBOL
);
135 sw
= DisplayWidth(dpy
, screen
);
136 sh
= DisplayHeight(dpy
, screen
);
137 mw
= (sw
* MASTERW
) / 100;
141 dc
.h
= bh
= dc
.font
.height
+ 2;
142 wa
.override_redirect
= 1;
143 wa
.background_pixmap
= ParentRelative
;
144 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
145 barwin
= XCreateWindow(dpy
, root
, bx
, by
, bw
, bh
, 0, DefaultDepth(dpy
, screen
),
146 CopyFromParent
, DefaultVisual(dpy
, screen
),
147 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
148 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
149 XMapRaised(dpy
, barwin
);
151 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
152 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
153 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
155 issel
= XQueryPointer(dpy
, root
, &w
, &w
, &i
, &i
, &i
, &i
, &mask
);
156 strcpy(stext
, "dwm-"VERSION
);
160 * Startup Error handler to check if another window manager
161 * is already running.
164 xerrorstart(Display
*dsply
, XErrorEvent
*ee
)
175 int i
, format
, protos
, status
;
176 unsigned long extra
, res
;
177 Atom
*protocols
, real
;
180 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMProtocols
], 0L, 20L, False
,
181 XA_ATOM
, &real
, &format
, &res
, &extra
, (unsigned char **)&protocols
);
182 if(status
!= Success
|| protocols
== 0)
184 for(i
= 0; i
< res
; i
++)
185 if(protocols
[i
] == wmatom
[WMDelete
])
186 protos
|= PROTODELWIN
;
192 sendevent(Window w
, Atom a
, long value
)
196 e
.type
= ClientMessage
;
197 e
.xclient
.window
= w
;
198 e
.xclient
.message_type
= a
;
199 e
.xclient
.format
= 32;
200 e
.xclient
.data
.l
[0] = value
;
201 e
.xclient
.data
.l
[1] = CurrentTime
;
202 XSendEvent(dpy
, w
, False
, NoEventMask
, &e
);
209 readin
= running
= False
;
213 * There's no way to check accesses to destroyed windows, thus those cases are
214 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
215 * default error handler, which calls exit().
218 xerror(Display
*dpy
, XErrorEvent
*ee
)
220 if(ee
->error_code
== BadWindow
221 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
222 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
223 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
224 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
225 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
226 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
))
228 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
229 ee
->request_code
, ee
->error_code
);
230 return xerrorxlib(dpy
, ee
); /* may call exit() */
234 main(int argc
, char *argv
[])
239 if(argc
== 2 && !strncmp("-v", argv
[1], 3)) {
240 fputs("dwm-"VERSION
", (C)opyright MMVI Anselm R. Garbe\n", stdout
);
244 eprint("usage: dwm [-v]\n");
246 dpy
= XOpenDisplay(0);
248 eprint("dwm: cannot open display\n");
250 xfd
= ConnectionNumber(dpy
);
251 screen
= DefaultScreen(dpy
);
252 root
= RootWindow(dpy
, screen
);
255 XSetErrorHandler(xerrorstart
);
256 /* this causes an error if some other window manager is running */
257 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
261 eprint("dwm: another window manager is already running\n");
264 XSetErrorHandler(NULL
);
265 xerrorxlib
= XSetErrorHandler(xerror
);
272 /* main event loop, also reads status text from stdin */
279 FD_SET(STDIN_FILENO
, &rd
);
281 r
= select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
);
282 if((r
== -1) && (errno
== EINTR
))
285 if(readin
&& FD_ISSET(STDIN_FILENO
, &rd
)) {
286 readin
= NULL
!= fgets(stext
, sizeof(stext
), stdin
);
288 stext
[strlen(stext
) - 1] = 0;
290 strcpy(stext
, "broken pipe");
295 eprint("select failed\n");