2005-11-18 18:54:58 +03:00
|
|
|
/*
|
2006-01-20 17:20:24 +03:00
|
|
|
* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
2005-11-18 18:54:58 +03:00
|
|
|
* See LICENSE file for license details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
2006-02-02 00:24:07 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <X11/cursorfont.h>
|
2005-11-18 18:54:58 +03:00
|
|
|
#include <X11/Xproto.h>
|
2005-12-21 18:18:11 +03:00
|
|
|
#include <X11/keysym.h>
|
2005-12-05 04:15:25 +03:00
|
|
|
#include <X11/Xatom.h>
|
|
|
|
#include <X11/Xproto.h>
|
2005-11-18 18:54:58 +03:00
|
|
|
|
|
|
|
#include "wm.h"
|
|
|
|
|
2005-12-05 01:45:59 +03:00
|
|
|
static int other_wm_running;
|
|
|
|
static int (*x_error_handler) (Display *, XErrorEvent *);
|
2005-11-18 18:54:58 +03:00
|
|
|
|
2006-02-04 17:31:52 +03:00
|
|
|
static char version[] = "wmiiwm - " VERSION ", (C)opyright MMIV-MMVI Anselm R. Garbe\n";
|
2005-11-18 18:54:58 +03:00
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
static void
|
|
|
|
usage()
|
|
|
|
{
|
2006-02-04 17:31:52 +03:00
|
|
|
fprintf(stderr, "%s", "usage: wmiiwm -a <address> [-c] [-v]\n");
|
2005-12-21 18:18:11 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Client *
|
2006-03-01 09:51:04 +03:00
|
|
|
win2client(Window w)
|
2005-12-21 18:18:11 +03:00
|
|
|
{
|
2006-03-01 13:55:46 +03:00
|
|
|
unsigned int i;
|
2006-01-25 20:39:08 +03:00
|
|
|
|
2006-01-26 21:58:30 +03:00
|
|
|
for(i = 0; (i < clientsz) && client[i]; i++)
|
2006-01-26 17:24:34 +03:00
|
|
|
if(client[i]->win == w)
|
|
|
|
return client[i];
|
2006-01-25 20:39:08 +03:00
|
|
|
return nil;
|
2005-12-21 18:18:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
scan_wins()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned int num;
|
|
|
|
Window *wins;
|
|
|
|
XWindowAttributes wa;
|
|
|
|
Window d1, d2;
|
|
|
|
|
|
|
|
if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
|
|
|
|
for(i = 0; i < num; i++) {
|
|
|
|
if(!XGetWindowAttributes(dpy, wins[i], &wa))
|
|
|
|
continue;
|
|
|
|
if(wa.override_redirect
|
|
|
|
|| XGetTransientForHint(dpy, wins[i], &d1))
|
|
|
|
continue;
|
2006-03-09 01:53:52 +03:00
|
|
|
if(wa.map_state == IsViewable)
|
|
|
|
manage_client(alloc_client(wins[i], &wa));
|
2005-12-21 18:18:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(wins)
|
|
|
|
XFree(wins);
|
|
|
|
}
|
|
|
|
|
2006-02-10 00:48:01 +03:00
|
|
|
static int
|
|
|
|
win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
|
|
|
|
{
|
|
|
|
Atom real;
|
|
|
|
int format;
|
|
|
|
unsigned long res, extra;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
status =
|
|
|
|
XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
|
|
|
|
&res, &extra, prop);
|
|
|
|
|
|
|
|
if(status != Success || *prop == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(res == 0) {
|
|
|
|
free((void *) *prop);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
int
|
|
|
|
win_proto(Window w)
|
|
|
|
{
|
|
|
|
Atom *protocols;
|
|
|
|
long res;
|
|
|
|
int protos = 0;
|
|
|
|
int i;
|
|
|
|
|
2006-03-05 02:11:08 +03:00
|
|
|
res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L,
|
2005-12-21 18:18:11 +03:00
|
|
|
((unsigned char **) &protocols));
|
|
|
|
if(res <= 0) {
|
|
|
|
return protos;
|
|
|
|
}
|
|
|
|
for(i = 0; i < res; i++) {
|
2006-03-05 02:11:08 +03:00
|
|
|
if(protocols[i] == wm_atom[WMDelete])
|
2005-12-21 18:18:11 +03:00
|
|
|
protos |= PROTO_DEL;
|
|
|
|
}
|
|
|
|
free((char *) protocols);
|
|
|
|
return protos;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
win_state(Window w)
|
|
|
|
{
|
|
|
|
/* state hints */
|
|
|
|
XWMHints *hints = XGetWMHints(dpy, w);
|
|
|
|
int res;
|
|
|
|
|
|
|
|
long *prop = 0;
|
2006-03-05 02:11:08 +03:00
|
|
|
if(win_property(w, wm_atom[WMState], wm_atom[WMState], 2L,
|
|
|
|
((unsigned char **) &prop)) > 0)
|
|
|
|
{
|
2005-12-21 18:18:11 +03:00
|
|
|
res = (int) *prop;
|
|
|
|
free((long *) prop);
|
|
|
|
} else {
|
|
|
|
res = hints ? hints->initial_state : NormalState;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(hints) {
|
|
|
|
free(hints);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_atoms()
|
|
|
|
{
|
2006-03-05 02:11:08 +03:00
|
|
|
wm_atom[WMState] = XInternAtom(dpy, "WM_STATE", False);
|
|
|
|
wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
|
|
|
|
wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
|
2005-12-21 18:18:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_cursors()
|
|
|
|
{
|
2006-03-05 15:22:42 +03:00
|
|
|
cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
|
|
|
|
cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
|
|
|
|
cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
|
|
|
|
cursor[CurW] = XCreateFontCursor(dpy, XC_left_side);
|
|
|
|
cursor[CurE] = XCreateFontCursor(dpy, XC_right_side);
|
|
|
|
cursor[CurN] = XCreateFontCursor(dpy, XC_top_side);
|
|
|
|
cursor[CurS] = XCreateFontCursor(dpy, XC_bottom_side);
|
|
|
|
cursor[CurNW] = XCreateFontCursor(dpy, XC_top_left_corner);
|
|
|
|
cursor[CurNE] = XCreateFontCursor(dpy, XC_top_right_corner);
|
|
|
|
cursor[CurSW] = XCreateFontCursor(dpy, XC_bottom_left_corner);
|
|
|
|
cursor[CurSE] = XCreateFontCursor(dpy, XC_bottom_right_corner);
|
2005-12-21 18:18:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_screen()
|
|
|
|
{
|
|
|
|
XGCValues gcv;
|
|
|
|
|
|
|
|
gcv.subwindow_mode = IncludeInferiors;
|
|
|
|
gcv.function = GXxor;
|
2006-02-02 22:08:34 +03:00
|
|
|
gcv.foreground = def.sel.bg;
|
2005-12-21 18:18:11 +03:00
|
|
|
gcv.line_width = 4;
|
|
|
|
gcv.plane_mask = AllPlanes;
|
|
|
|
gcv.graphics_exposures = False;
|
2006-01-25 20:39:08 +03:00
|
|
|
gc_xor = XCreateGC(dpy, root, GCForeground | GCGraphicsExposures
|
2005-12-21 18:18:11 +03:00
|
|
|
| GCFunction | GCSubwindowMode | GCLineWidth
|
|
|
|
| GCPlaneMask, &gcv);
|
|
|
|
rect.x = rect.y = 0;
|
2006-01-25 20:39:08 +03:00
|
|
|
rect.width = DisplayWidth(dpy, screen);
|
|
|
|
rect.height = DisplayHeight(dpy, screen);
|
2005-12-21 18:18:11 +03:00
|
|
|
|
2006-03-05 15:22:42 +03:00
|
|
|
XDefineCursor(dpy, root, cursor[CurNormal]);
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There's no way to check accesses to destroyed windows, thus
|
|
|
|
* those cases are ignored (especially on UnmapNotify's).
|
|
|
|
* Other types of errors call Xlib's default error handler, which
|
|
|
|
* calls exit().
|
|
|
|
*/
|
2005-12-21 18:18:11 +03:00
|
|
|
static int
|
|
|
|
wmii_error_handler(Display * dpy, XErrorEvent * error)
|
|
|
|
{
|
|
|
|
if(error->error_code == BadWindow
|
|
|
|
|| (error->request_code == X_SetInputFocus
|
|
|
|
&& error->error_code == BadMatch)
|
|
|
|
|| (error->request_code == X_PolyText8
|
|
|
|
&& error->error_code == BadDrawable)
|
|
|
|
|| (error->request_code == X_PolyFillRectangle
|
|
|
|
&& error->error_code == BadDrawable)
|
|
|
|
|| (error->request_code == X_PolySegment
|
|
|
|
&& error->error_code == BadDrawable)
|
|
|
|
|| (error->request_code == X_ConfigureWindow
|
|
|
|
&& error->error_code == BadMatch))
|
|
|
|
return 0;
|
|
|
|
fprintf(stderr, "%s", "wmiiwm: fatal error");
|
|
|
|
return x_error_handler(dpy, error); /* calls exit() */
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Startup Error handler to check if another window manager
|
|
|
|
* is already running.
|
|
|
|
*/
|
2005-12-21 18:18:11 +03:00
|
|
|
static int
|
|
|
|
startup_error_handler(Display * dpy, XErrorEvent * error)
|
|
|
|
{
|
|
|
|
other_wm_running = 1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup()
|
|
|
|
{
|
2006-03-01 13:55:46 +03:00
|
|
|
unsigned int i;
|
2006-01-25 20:39:08 +03:00
|
|
|
Client *c;
|
2006-01-26 17:24:34 +03:00
|
|
|
for(i = 0; client && client[i]; i++) {
|
|
|
|
c = client[i];
|
2006-03-07 01:55:47 +03:00
|
|
|
reparent_client(c, root, c->frame[c->sel]->rect.x, c->frame[c->sel]->rect.y);
|
2006-01-25 20:39:08 +03:00
|
|
|
}
|
2005-12-21 18:18:11 +03:00
|
|
|
XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
|
2006-01-10 16:43:09 +03:00
|
|
|
XSync(dpy, False);
|
2005-12-21 18:18:11 +03:00
|
|
|
}
|
|
|
|
|
2006-02-08 12:50:09 +03:00
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int checkwm = 0;
|
2006-02-03 20:11:22 +03:00
|
|
|
char *address = nil, *errstr;
|
2006-02-10 00:48:01 +03:00
|
|
|
XSetWindowAttributes wa;
|
2005-12-21 18:18:11 +03:00
|
|
|
|
|
|
|
/* command line args */
|
|
|
|
if(argc > 1) {
|
|
|
|
for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
|
|
|
|
switch (argv[i][1]) {
|
|
|
|
case 'v':
|
2006-02-04 17:31:52 +03:00
|
|
|
fprintf(stdout, "%s", version);
|
2005-12-21 18:18:11 +03:00
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
checkwm = 1;
|
|
|
|
break;
|
2006-02-02 18:44:45 +03:00
|
|
|
case 'a':
|
2005-12-21 18:18:11 +03:00
|
|
|
if(i + 1 < argc)
|
2006-02-02 00:24:07 +03:00
|
|
|
address = argv[++i];
|
2005-12-21 18:18:11 +03:00
|
|
|
else
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-02-06 17:46:02 +03:00
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
dpy = XOpenDisplay(0);
|
|
|
|
if(!dpy) {
|
|
|
|
fprintf(stderr, "%s", "wmiiwm: cannot open display\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2006-01-25 20:39:08 +03:00
|
|
|
screen = DefaultScreen(dpy);
|
|
|
|
root = RootWindow(dpy, screen);
|
2005-12-21 18:18:11 +03:00
|
|
|
|
|
|
|
/* check if another WM is already running */
|
|
|
|
other_wm_running = 0;
|
|
|
|
XSetErrorHandler(startup_error_handler);
|
|
|
|
/* this causes an error if some other WM is running */
|
|
|
|
XSelectInput(dpy, root, ROOT_MASK);
|
|
|
|
XSync(dpy, False);
|
|
|
|
|
|
|
|
if(other_wm_running) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"wmiiwm: another window manager is already running\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(checkwm) {
|
|
|
|
XCloseDisplay(dpy);
|
|
|
|
exit(0);
|
|
|
|
}
|
2006-02-08 19:59:57 +03:00
|
|
|
/* above -c is checked */
|
|
|
|
if(!address)
|
|
|
|
usage();
|
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
XSetErrorHandler(0);
|
|
|
|
x_error_handler = XSetErrorHandler(wmii_error_handler);
|
2006-02-02 16:50:04 +03:00
|
|
|
errstr = nil;
|
|
|
|
i = ixp_create_sock(address, &errstr);
|
|
|
|
if(i < 0) {
|
|
|
|
fprintf(stderr, "wmii: fatal: %s\n", errstr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* IXP server */
|
2006-02-03 20:11:22 +03:00
|
|
|
ixp_server_open_conn(&srv, i, new_ixp_conn, ixp_server_close_conn);
|
2006-02-02 18:44:45 +03:00
|
|
|
root_qid.type = IXP_QTDIR;
|
|
|
|
root_qid.version = 0;
|
2006-03-05 16:16:48 +03:00
|
|
|
root_qid.path = mkqpath(FsDroot, 0, 0, 0);
|
2006-02-02 16:50:04 +03:00
|
|
|
|
2006-02-03 20:11:22 +03:00
|
|
|
/* X server */
|
|
|
|
ixp_server_open_conn(&srv, ConnectionNumber(dpy), check_x_event, nil);
|
2006-02-02 19:23:04 +03:00
|
|
|
init_x_event_handler();
|
2005-12-21 18:18:11 +03:00
|
|
|
|
2006-03-05 21:20:27 +03:00
|
|
|
nctag = ctagsz = ntag = nclient = tagsz = clientsz = sel = 0;
|
2006-03-04 11:23:17 +03:00
|
|
|
tag = nil;
|
2006-03-05 21:20:27 +03:00
|
|
|
ctag = nil;
|
2006-02-22 12:58:00 +03:00
|
|
|
client = nil;
|
2006-02-02 00:24:07 +03:00
|
|
|
|
2006-02-10 00:48:01 +03:00
|
|
|
key = nil;
|
|
|
|
keysz = nkey = 0;
|
2006-02-10 17:59:30 +03:00
|
|
|
label = nil;
|
|
|
|
nlabel = labelsz = iexpand = 0;
|
2006-03-08 18:52:05 +03:00
|
|
|
class = nil;
|
|
|
|
nclass = classsz = 0;
|
2006-02-10 00:48:01 +03:00
|
|
|
|
2006-02-02 22:36:10 +03:00
|
|
|
def.font = strdup(BLITZ_FONT);
|
2006-02-02 21:32:25 +03:00
|
|
|
def.border = DEF_BORDER;
|
|
|
|
def.snap = DEF_SNAP;
|
2006-03-06 18:35:07 +03:00
|
|
|
cext_strlcpy(def.tag, "1", sizeof(def.tag));
|
2006-02-09 21:40:12 +03:00
|
|
|
cext_strlcpy(def.selcolor, BLITZ_SELCOLORS, sizeof(def.selcolor));
|
2006-02-02 19:23:04 +03:00
|
|
|
blitz_loadcolor(dpy, screen, def.selcolor, &def.sel);
|
2006-02-09 21:40:12 +03:00
|
|
|
cext_strlcpy(def.normcolor, BLITZ_NORMCOLORS, sizeof(def.normcolor));
|
2006-02-02 19:23:04 +03:00
|
|
|
blitz_loadcolor(dpy, screen, def.normcolor, &def.norm);
|
2006-01-25 20:39:08 +03:00
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
init_atoms();
|
|
|
|
init_cursors();
|
2006-02-02 00:24:07 +03:00
|
|
|
xfont = blitz_getfont(dpy, def.font);
|
2006-02-10 00:48:01 +03:00
|
|
|
init_lock_modifiers();
|
2005-12-21 18:18:11 +03:00
|
|
|
init_screen();
|
2006-02-10 00:48:01 +03:00
|
|
|
|
|
|
|
wa.override_redirect = 1;
|
|
|
|
wa.background_pixmap = ParentRelative;
|
|
|
|
wa.event_mask = ExposureMask | ButtonPressMask
|
|
|
|
| SubstructureRedirectMask | SubstructureNotifyMask;
|
|
|
|
brect = rect;
|
|
|
|
brect.height = xfont->ascent + xfont->descent + 4;
|
|
|
|
brect.y = rect.height - brect.height;
|
|
|
|
winbar = XCreateWindow(dpy, RootWindow(dpy, screen), brect.x, brect.y,
|
|
|
|
brect.width, brect.height, 0, DefaultDepth(dpy, screen),
|
|
|
|
CopyFromParent, DefaultVisual(dpy, screen),
|
|
|
|
CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
|
|
|
|
|
|
|
|
XDefineCursor(dpy, winbar, XCreateFontCursor(dpy, XC_left_ptr));
|
|
|
|
XSync(dpy, False);
|
|
|
|
|
|
|
|
gcbar = XCreateGC(dpy, winbar, 0, 0);
|
|
|
|
|
|
|
|
pmapbar = XCreatePixmap(dpy, winbar, brect.width, brect.height,
|
|
|
|
DefaultDepth(dpy, screen));
|
2006-02-10 11:13:35 +03:00
|
|
|
XMapRaised(dpy, winbar);
|
|
|
|
draw_bar();
|
2006-03-01 13:09:49 +03:00
|
|
|
scan_wins();
|
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
/* main event loop */
|
2006-02-02 16:50:04 +03:00
|
|
|
errstr = ixp_server_loop(&srv);
|
|
|
|
if(errstr)
|
|
|
|
fprintf(stderr, "wmii: fatal: %s\n", errstr);
|
|
|
|
|
2006-02-03 20:11:22 +03:00
|
|
|
ixp_server_close(&srv);
|
2005-12-21 18:18:11 +03:00
|
|
|
cleanup();
|
2006-02-02 19:23:04 +03:00
|
|
|
XCloseDisplay(dpy);
|
2005-11-18 18:54:58 +03:00
|
|
|
|
2006-02-03 20:11:22 +03:00
|
|
|
return errstr ? 1 : 0;
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|