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/types.h>
15 #include <X11/cursorfont.h>
16 #include <X11/Xatom.h>
17 #include <X11/Xproto.h>
24 Atom wm_atom
[WMLast
], net_atom
[NetLast
];
25 Cursor cursor
[CurLast
];
29 char statustext
[1024], tag
[256];
30 int screen
, sx
, sy
, sw
, sh
, bx
, by
, bw
, bh
;
33 Client
*clients
= NULL
;
36 static Bool other_wm_running
;
37 static const char version
[] = "gridwm - " VERSION
", (C)opyright MMVI Anselm R. Garbe\n";
38 static int (*x_error_handler
) (Display
*, XErrorEvent
*);
40 static const char *status
[] = {
41 "sh", "-c", "echo -n `date '+%Y-%m-%d %H:%M'`"
42 " `uptime | sed 's/.*://; s/,//g'`"
43 " `acpi | awk '{print $4}' | sed 's/,//'`", 0
49 fputs("usage: gridwm [-v]\n", stderr
);
61 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
62 for(i
= 0; i
< num
; i
++) {
63 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
65 if(wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
67 if(wa
.map_state
== IsViewable
)
76 win_property(Window w
, Atom a
, Atom t
, long l
, unsigned char **prop
)
80 unsigned long res
, extra
;
83 status
= XGetWindowProperty(dpy
, w
, a
, 0L, l
, False
, t
, &real
, &format
,
86 if(status
!= Success
|| *prop
== 0) {
103 res
= win_property(w
, wm_atom
[WMProtocols
], XA_ATOM
, 20L,
104 ((unsigned char **) &protocols
));
108 for(i
= 0; i
< res
; i
++) {
109 if(protocols
[i
] == wm_atom
[WMDelete
])
110 protos
|= WM_PROTOCOL_DELWIN
;
112 free((char *) protocols
);
117 send_message(Window w
, Atom a
, long value
)
121 e
.type
= ClientMessage
;
122 e
.xclient
.window
= w
;
123 e
.xclient
.message_type
= a
;
124 e
.xclient
.format
= 32;
125 e
.xclient
.data
.l
[0] = value
;
126 e
.xclient
.data
.l
[1] = CurrentTime
;
127 XSendEvent(dpy
, w
, False
, NoEventMask
, &e
);
132 * There's no way to check accesses to destroyed windows, thus
133 * those cases are ignored (especially on UnmapNotify's).
134 * Other types of errors call Xlib's default error handler, which
138 error_handler(Display
*dpy
, XErrorEvent
*error
)
140 if(error
->error_code
== BadWindow
141 || (error
->request_code
== X_SetInputFocus
142 && error
->error_code
== BadMatch
)
143 || (error
->request_code
== X_PolyText8
144 && error
->error_code
== BadDrawable
)
145 || (error
->request_code
== X_PolyFillRectangle
146 && error
->error_code
== BadDrawable
)
147 || (error
->request_code
== X_PolySegment
148 && error
->error_code
== BadDrawable
)
149 || (error
->request_code
== X_ConfigureWindow
150 && error
->error_code
== BadMatch
)
151 || (error
->request_code
== X_GrabKey
152 && error
->error_code
== BadAccess
))
154 fprintf(stderr
, "gridwm: fatal error: request code=%d, error code=%d\n",
155 error
->request_code
, error
->error_code
);
156 return x_error_handler(dpy
, error
); /* may call exit() */
160 * Startup Error handler to check if another window manager
161 * is already running.
164 startup_error_handler(Display
*dpy
, XErrorEvent
*error
)
166 other_wm_running
= True
;
175 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
179 main(int argc
, char *argv
[])
182 XSetWindowAttributes wa
;
187 struct timeval t
, timeout
= {
189 .tv_sec
= STATUSDELAY
,
192 /* command line args */
193 for(i
= 1; (i
< argc
) && (argv
[i
][0] == '-'); i
++) {
194 switch (argv
[i
][1]) {
196 fprintf(stdout
, "%s", version
);
205 dpy
= XOpenDisplay(0);
207 error("gridwm: cannot connect X server\n");
209 screen
= DefaultScreen(dpy
);
210 root
= RootWindow(dpy
, screen
);
212 /* check if another WM is already running */
213 other_wm_running
= False
;
214 XSetErrorHandler(startup_error_handler
);
215 /* this causes an error if some other WM is running */
216 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
220 error("gridwm: another window manager is already running\n");
223 sw
= DisplayWidth(dpy
, screen
);
224 sh
= DisplayHeight(dpy
, screen
);
225 sel_screen
= XQueryPointer(dpy
, root
, &w
, &w
, &i
, &i
, &i
, &i
, &mask
);
228 x_error_handler
= XSetErrorHandler(error_handler
);
231 wm_atom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
232 wm_atom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
233 net_atom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
234 net_atom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
236 XChangeProperty(dpy
, root
, net_atom
[NetSupported
], XA_ATOM
, 32,
237 PropModeReplace
, (unsigned char *) net_atom
, NetLast
);
241 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
242 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
243 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
248 loadcolors(dpy
, screen
, &brush
, BGCOLOR
, FGCOLOR
, BORDERCOLOR
);
249 loadfont(dpy
, &brush
.font
, FONT
);
251 wa
.override_redirect
= 1;
252 wa
.background_pixmap
= ParentRelative
;
253 wa
.event_mask
= ExposureMask
;
257 bh
= texth(&brush
.font
);
258 barwin
= XCreateWindow(dpy
, root
, bx
, by
, bw
, bh
, 0, DefaultDepth(dpy
, screen
),
259 CopyFromParent
, DefaultVisual(dpy
, screen
),
260 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
261 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
262 XMapRaised(dpy
, barwin
);
264 brush
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
265 brush
.gc
= XCreateGC(dpy
, root
, 0, 0);
267 pipe_spawn(statustext
, sizeof(statustext
), dpy
, (char **)status
);
270 wa
.event_mask
= SubstructureRedirectMask
| EnterWindowMask \
272 wa
.cursor
= cursor
[CurNormal
];
273 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
278 if(XPending(dpy
) > 0) {
279 XNextEvent(dpy
, &ev
);
281 (handler
[ev
.type
]) (&ev
); /* call handler */
285 FD_SET(ConnectionNumber(dpy
), &fds
);
287 if(select(ConnectionNumber(dpy
) + 1, &fds
, NULL
, NULL
, &t
) > 0)
289 else if(errno
!= EINTR
) {
290 pipe_spawn(statustext
, sizeof(statustext
), dpy
, (char **)status
);