Xinqi Bao's Git
1 /* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
11 #include <sys/select.h>
12 #include <X11/cursorfont.h>
13 #include <X11/keysym.h>
14 #include <X11/Xatom.h>
15 #include <X11/Xproto.h>
21 int bx
, by
, bw
, bh
, bmw
, masterd
, screen
, sx
, sy
, sw
, sh
;
22 unsigned int master
, ntags
, numlockmask
;
23 Atom wmatom
[WMLast
], netatom
[NetLast
];
26 Client
*clients
= NULL
;
29 Cursor cursor
[CurLast
];
36 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
37 static Bool otherwm
, readin
;
43 resize(sel
, True
, TopLeft
);
47 XFreeFontSet(dpy
, dc
.font
.set
);
49 XFreeFont(dpy
, dc
.font
.xfont
);
50 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
51 XFreePixmap(dpy
, dc
.drawable
);
53 XDestroyWindow(dpy
, barwin
);
54 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
66 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
67 for(i
= 0; i
< num
; i
++) {
68 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
70 if(wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
72 if(wa
.map_state
== IsViewable
)
85 XModifierKeymap
*modmap
;
86 XSetWindowAttributes wa
;
89 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
90 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
91 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
92 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
93 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
94 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
96 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
97 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
98 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
99 /* init modifier map */
100 modmap
= XGetModifierMapping(dpy
);
101 for (i
= 0; i
< 8; i
++) {
102 for (j
= 0; j
< modmap
->max_keypermod
; j
++) {
103 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
))
104 numlockmask
= (1 << i
);
108 /* select for events */
109 wa
.event_mask
= SubstructureRedirectMask
| SubstructureNotifyMask
110 | EnterWindowMask
| LeaveWindowMask
;
111 wa
.cursor
= cursor
[CurNormal
];
112 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
115 for(ntags
= 0; tags
[ntags
]; ntags
++);
116 seltag
= emallocz(sizeof(Bool
) * ntags
);
119 dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
);
120 dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
);
121 dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
);
122 dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
);
123 dc
.status
[ColBG
] = getcolor(STATUSBGCOLOR
);
124 dc
.status
[ColFG
] = getcolor(STATUSFGCOLOR
);
127 bmw
= textw(TILESYMBOL
) > textw(FLOATSYMBOL
) ? textw(TILESYMBOL
) : textw(FLOATSYMBOL
);
129 sw
= DisplayWidth(dpy
, screen
);
130 sh
= DisplayHeight(dpy
, screen
);
135 dc
.h
= bh
= dc
.font
.height
+ 2;
136 wa
.override_redirect
= 1;
137 wa
.background_pixmap
= ParentRelative
;
138 wa
.event_mask
= ButtonPressMask
| ExposureMask
;
139 barwin
= XCreateWindow(dpy
, root
, bx
, by
, bw
, bh
, 0, DefaultDepth(dpy
, screen
),
140 CopyFromParent
, DefaultVisual(dpy
, screen
),
141 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
142 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
143 XMapRaised(dpy
, barwin
);
144 strcpy(stext
, "dwm-"VERSION
);
145 /* pixmap for everything */
146 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
147 dc
.gc
= XCreateGC(dpy
, root
, 0, 0);
148 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
149 /* multihead support */
150 issel
= XQueryPointer(dpy
, root
, &w
, &w
, &i
, &i
, &i
, &i
, &mask
);
154 * Startup Error handler to check if another window manager
155 * is already running.
158 xerrorstart(Display
*dsply
, XErrorEvent
*ee
) {
167 int i
, format
, protos
, status
;
168 unsigned long extra
, res
;
169 Atom
*protocols
, real
;
172 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMProtocols
], 0L, 20L, False
,
173 XA_ATOM
, &real
, &format
, &res
, &extra
, (unsigned char **)&protocols
);
174 if(status
!= Success
|| protocols
== 0)
176 for(i
= 0; i
< res
; i
++)
177 if(protocols
[i
] == wmatom
[WMDelete
])
178 protos
|= PROTODELWIN
;
184 sendevent(Window w
, Atom a
, long value
) {
187 e
.type
= ClientMessage
;
188 e
.xclient
.window
= w
;
189 e
.xclient
.message_type
= a
;
190 e
.xclient
.format
= 32;
191 e
.xclient
.data
.l
[0] = value
;
192 e
.xclient
.data
.l
[1] = CurrentTime
;
193 XSendEvent(dpy
, w
, False
, NoEventMask
, &e
);
199 readin
= running
= False
;
202 /* There's no way to check accesses to destroyed windows, thus those cases are
203 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
204 * default error handler, which may call exit.
207 xerror(Display
*dpy
, XErrorEvent
*ee
) {
208 if(ee
->error_code
== BadWindow
209 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
210 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
211 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
212 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
213 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
214 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
215 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
217 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
218 ee
->request_code
, ee
->error_code
);
219 return xerrorxlib(dpy
, ee
); /* may call exit */
223 main(int argc
, char *argv
[]) {
227 if(argc
== 2 && !strncmp("-v", argv
[1], 3)) {
228 fputs("dwm-"VERSION
", (C)opyright MMVI Anselm R. Garbe\n", stdout
);
232 eprint("usage: dwm [-v]\n");
233 dpy
= XOpenDisplay(0);
235 eprint("dwm: cannot open display\n");
236 xfd
= ConnectionNumber(dpy
);
237 screen
= DefaultScreen(dpy
);
238 root
= RootWindow(dpy
, screen
);
240 XSetErrorHandler(xerrorstart
);
241 /* this causes an error if some other window manager is running */
242 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
245 eprint("dwm: another window manager is already running\n");
248 XSetErrorHandler(NULL
);
249 xerrorxlib
= XSetErrorHandler(xerror
);
255 /* main event loop, also reads status text from stdin */
262 FD_SET(STDIN_FILENO
, &rd
);
264 r
= select(xfd
+ 1, &rd
, NULL
, NULL
, NULL
);
265 if((r
== -1) && (errno
== EINTR
))
268 if(readin
&& FD_ISSET(STDIN_FILENO
, &rd
)) {
269 readin
= NULL
!= fgets(stext
, sizeof(stext
), stdin
);
271 stext
[strlen(stext
) - 1] = 0;
273 strcpy(stext
, "broken pipe");
278 eprint("select failed\n");