Xinqi Bao's Git
2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
10 #include <X11/cursorfont.h>
11 #include <X11/Xatom.h>
12 #include <X11/Xproto.h>
19 Atom net_atom
[NetLast
];
20 Cursor cursor
[CurLast
];
21 XRectangle rect
, barrect
;
23 Client
*clients
= NULL
;
25 char *bartext
, tag
[256];
26 int screen
, sel_screen
;
31 enum { WM_PROTOCOL_DELWIN
= 1 };
33 static Bool other_wm_running
;
34 static int (*x_error_handler
) (Display
*, XErrorEvent
*);
35 static char version
[] = "gridwm - " VERSION
", (C)opyright MMVI Anselm R. Garbe\n";
40 fputs("usage: gridwm [-v]\n", stderr
);
52 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
53 for(i
= 0; i
< num
; i
++) {
54 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
56 if(wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
58 if(wa
.map_state
== IsViewable
)
59 manage(create_client(wins
[i
], &wa
));
67 * There's no way to check accesses to destroyed windows, thus
68 * those cases are ignored (especially on UnmapNotify's).
69 * Other types of errors call Xlib's default error handler, which
73 error_handler(Display
*dpy
, XErrorEvent
*error
)
75 if(error
->error_code
== BadWindow
76 || (error
->request_code
== X_SetInputFocus
77 && error
->error_code
== BadMatch
)
78 || (error
->request_code
== X_PolyText8
79 && error
->error_code
== BadDrawable
)
80 || (error
->request_code
== X_PolyFillRectangle
81 && error
->error_code
== BadDrawable
)
82 || (error
->request_code
== X_PolySegment
83 && error
->error_code
== BadDrawable
)
84 || (error
->request_code
== X_ConfigureWindow
85 && error
->error_code
== BadMatch
)
86 || (error
->request_code
== X_GrabKey
87 && error
->error_code
== BadAccess
))
89 fprintf(stderr
, "gridwm: fatal error: request code=%d, error code=%d\n",
90 error
->request_code
, error
->error_code
);
91 return x_error_handler(dpy
, error
); /* may call exit() */
95 * Startup Error handler to check if another window manager
99 startup_error_handler(Display
*dpy
, XErrorEvent
*error
)
101 other_wm_running
= True
;
110 for(c=client; c; c=c->next)
111 reparent_client(c, root, c->sel->rect.x, c->sel->rect.y);
112 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
117 main(int argc
, char *argv
[])
120 XSetWindowAttributes wa
;
125 /* command line args */
126 for(i
= 1; (i
< argc
) && (argv
[i
][0] == '-'); i
++) {
127 switch (argv
[i
][1]) {
129 fprintf(stdout
, "%s", version
);
138 dpy
= XOpenDisplay(0);
140 error("gridwm: cannot connect X server\n");
142 screen
= DefaultScreen(dpy
);
143 root
= RootWindow(dpy
, screen
);
145 /* check if another WM is already running */
146 other_wm_running
= False
;
147 XSetErrorHandler(startup_error_handler
);
148 /* this causes an error if some other WM is running */
149 XSelectInput(dpy
, root
, SubstructureRedirectMask
);
153 error("gridwm: another window manager is already running\n");
156 rect
.width
= DisplayWidth(dpy
, screen
);
157 rect
.height
= DisplayHeight(dpy
, screen
);
158 sel_screen
= XQueryPointer(dpy
, root
, &w
, &w
, &i
, &i
, &i
, &i
, &mask
);
161 x_error_handler
= XSetErrorHandler(error_handler
);
164 net_atom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
165 net_atom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
167 XChangeProperty(dpy
, root
, net_atom
[NetSupported
], XA_ATOM
, 32,
168 PropModeReplace
, (unsigned char *) net_atom
, NetLast
);
172 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
173 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
174 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
178 brush
.drawable
= XCreatePixmap(dpy
, root
, rect
.width
, rect
.height
,
179 DefaultDepth(dpy
, screen
));
180 brush
.gc
= XCreateGC(dpy
, root
, 0, 0);
183 loadcolors(dpy
, screen
, &brush
, BGCOLOR
, FGCOLOR
, BORDERCOLOR
);
184 loadfont(dpy
, &brush
.font
, FONT
);
186 wa
.override_redirect
= 1;
187 wa
.background_pixmap
= ParentRelative
;
188 wa
.event_mask
= ExposureMask
;
191 barrect
.height
= labelheight(&brush
.font
);
192 barrect
.y
= rect
.height
- barrect
.height
;
193 barwin
= XCreateWindow(dpy
, root
, barrect
.x
, barrect
.y
,
194 barrect
.width
, barrect
.height
, 0, DefaultDepth(dpy
, screen
),
195 CopyFromParent
, DefaultVisual(dpy
, screen
),
196 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
198 XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]);
199 XMapRaised(dpy
, barwin
);
202 wa
.event_mask
= SubstructureRedirectMask
| EnterWindowMask \
204 wa
.cursor
= cursor
[CurNormal
];
205 XChangeWindowAttributes(dpy
, root
, CWEventMask
| CWCursor
, &wa
);
210 XNextEvent(dpy
, &ev
);
212 (handler
[ev
.type
]) (&ev
); /* call handler */