wmii/main.c

375 lines
10 KiB
C
Raw Normal View History

2006-10-12 18:10:57 +04: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.
*/
2006-10-20 12:07:55 +04:00
#include "wmii.h"
#include <errno.h>
2005-11-18 18:54:58 +03:00
#include <fcntl.h>
#include <locale.h>
#include <pwd.h>
2007-02-04 21:31:12 +03:00
#include <signal.h>
#include <stdarg.h>
2005-11-18 18:54:58 +03:00
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#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
2005-12-05 01:45:59 +03:00
static int other_wm_running;
static int (*x_error_handler) (Display *, XErrorEvent *);
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
2006-10-12 18:10:57 +04:00
usage() {
fputs("usage: wmiiwm -a <address> [-r <wmiirc>] [-v]\n", stderr);
exit(1);
}
2007-02-04 21:31:12 +03:00
static void
sigchld_handler(int sig) {
int ret;
/* We only spawn one child */
wait(&ret);
}
static void
2006-10-12 18:10:57 +04:00
scan_wins() {
int i;
unsigned int num;
Window *wins;
XWindowAttributes wa;
Window d1, d2;
2006-07-03 20:41:14 +04:00
if(XQueryTree(blz.dpy, blz.root, &d1, &d2, &wins, &num)) {
for(i = 0; i < num; i++) {
2006-07-03 20:41:14 +04:00
if(!XGetWindowAttributes(blz.dpy, wins[i], &wa))
continue;
2006-07-03 20:41:14 +04:00
if(wa.override_redirect || XGetTransientForHint(blz.dpy, wins[i], &d1))
continue;
if(wa.map_state == IsViewable)
2006-04-12 12:44:07 +04:00
manage_client(create_client(wins[i], &wa));
}
}
if(wins)
XFree(wins);
2005-12-21 18:18:11 +03:00
}
static int
2006-10-12 18:10:57 +04:00
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(blz.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;
}
int
2006-10-12 18:10:57 +04:00
win_proto(Window w) {
Atom *protocols;
long res;
int protos = 0;
int i;
res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L,
((unsigned char **) &protocols));
if(res <= 0) {
return protos;
}
for(i = 0; i < res; i++) {
if(protocols[i] == wm_atom[WMDelete])
protos |= WM_PROTOCOL_DELWIN;
}
free((char *) protocols);
return protos;
}
2005-12-21 18:18:11 +03:00
static void
2006-10-12 18:10:57 +04:00
init_atoms() {
wm_atom[WMState] = XInternAtom(blz.dpy, "WM_STATE", False);
wm_atom[WMProtocols] = XInternAtom(blz.dpy, "WM_PROTOCOLS", False);
wm_atom[WMDelete] = XInternAtom(blz.dpy, "WM_DELETE_WINDOW", False);
2006-07-03 20:41:14 +04:00
net_atom[NetSupported] = XInternAtom(blz.dpy, "_NET_SUPPORTED", False);
net_atom[NetWMName] = XInternAtom(blz.dpy, "_NET_WM_NAME", False);
tags_atom = XInternAtom(blz.dpy, "_WIN_TAGS", False);
XChangeProperty(blz.dpy, blz.root, net_atom[NetSupported], XA_ATOM, 32,
PropModeReplace, (unsigned char *) net_atom, NetLast);
2005-12-21 18:18:11 +03:00
}
static void
2006-10-12 18:10:57 +04:00
init_cursors() {
Pixmap pix;
XColor black, dummy;
XAllocNamedColor(blz.dpy, DefaultColormap(blz.dpy, blz.screen), "black", &black, &dummy);
pix = XCreateBitmapFromData(blz.dpy, blz.root, (char[]){0}, 1, 1);
2006-07-03 20:41:14 +04:00
cursor[CurNormal] = XCreateFontCursor(blz.dpy, XC_left_ptr);
cursor[CurResize] = XCreateFontCursor(blz.dpy, XC_sizing);
cursor[CurMove] = XCreateFontCursor(blz.dpy, XC_fleur);
cursor[CurInput] = XCreateFontCursor(blz.dpy, XC_xterm);
cursor[CurInvisible] = XCreatePixmapCursor(blz.dpy, pix, pix, &black, &black, 0, 0);
XFreePixmap(blz.dpy, pix);
2005-12-21 18:18:11 +03:00
}
static void
2006-10-12 18:10:57 +04:00
init_screen(WMScreen *screen) {
Window w;
int ret;
unsigned mask;
XGCValues gcv;
gcv.subwindow_mode = IncludeInferiors;
gcv.function = GXxor;
gcv.foreground = def.selcolor.bg;
gcv.plane_mask = AllPlanes;
gcv.graphics_exposures = False;
2006-07-03 20:41:14 +04:00
xorgc = XCreateGC(blz.dpy, blz.root, GCForeground | GCGraphicsExposures |
GCFunction | GCSubwindowMode | GCPlaneMask, &gcv);
screen->rect.x = screen->rect.y = 0;
2006-07-03 20:41:14 +04:00
screen->rect.width = DisplayWidth(blz.dpy, blz.screen);
screen->rect.height = DisplayHeight(blz.dpy, blz.screen);
def.snap = screen->rect.height / 63;
2006-07-03 20:41:14 +04:00
sel_screen = XQueryPointer(blz.dpy, blz.root, &w, &w, &ret, &ret, &ret, &ret, &mask);
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().
*/
int
2006-10-12 18:10:57 +04:00
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)
|| (error->request_code == X_GrabKey
&& error->error_code == BadAccess))
return 0;
2006-05-14 19:27:00 +04:00
fprintf(stderr, "wmiiwm: fatal error: Xrequest code=%d, Xerror code=%d\n",
error->request_code, error->error_code);
2006-07-03 20:41:14 +04:00
return x_error_handler(blz.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
2006-10-12 18:10:57 +04:00
startup_error_handler(Display * dpy, XErrorEvent * error) {
other_wm_running = 1;
return -1;
2005-12-21 18:18:11 +03:00
}
static void
2006-10-12 18:10:57 +04:00
cleanup() {
2006-06-08 12:54:19 +04:00
Client *c;
2006-10-12 18:10:57 +04:00
2006-06-08 12:54:19 +04:00
for(c=client; c; c=c->next)
reparent_client(c, blz.root, c->sel->rect.x, c->sel->rect.y);
2006-07-03 20:41:14 +04:00
XSetInputFocus(blz.dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
XSync(blz.dpy, False);
2005-12-21 18:18:11 +03:00
}
int
2006-10-12 18:10:57 +04:00
main(int argc, char *argv[]) {
int i;
2006-10-12 18:10:57 +04:00
char *address = NULL, *wmiirc = NULL, *namespace, *errstr;
WMScreen *s;
2006-06-27 14:54:21 +04:00
struct passwd *passwd;
XSetWindowAttributes wa;
2005-12-21 18:18:11 +03:00
/* command line args */
2006-06-28 06:16:14 +04:00
for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
switch (argv[i][1]) {
case 'v':
fprintf(stdout, "%s", version);
exit(0);
break;
case 'a':
if(i + 1 < argc)
address = argv[++i];
else
usage();
2006-06-28 06:16:14 +04:00
break;
case 'r':
if(i + 1 < argc)
wmiirc = argv[++i];
else
usage();
break;
default:
usage();
break;
}
}
starting = True;
setlocale(LC_CTYPE, "");
2006-07-03 20:41:14 +04:00
blz.dpy = XOpenDisplay(0);
if(!blz.dpy)
2006-10-16 16:43:31 +04:00
ixp_eprint("wmiiwm: cannot open dpy\n");
2006-07-03 20:41:14 +04:00
blz.screen = DefaultScreen(blz.dpy);
blz.root = RootWindow(blz.dpy, blz.screen);
/* 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 */
2006-07-03 20:41:14 +04:00
XSelectInput(blz.dpy, blz.root, SubstructureRedirectMask | EnterWindowMask);
XSync(blz.dpy, False);
if(other_wm_running)
2006-10-16 16:43:31 +04:00
ixp_eprint("wmiiwm: another window manager is already running\n");
if(!address)
usage();
/* Check namespace permissions */
if(!strncmp(address, "unix!", 5)) {
struct stat st;
2006-10-12 18:10:57 +04:00
namespace = ixp_estrdup(&address[5]);
for(i = strlen(namespace) - 1; i >= 0; i--)
if(namespace[i] == '/') break;
namespace[i+1] = '\0';
if(stat(namespace, &st))
2006-10-16 16:43:31 +04:00
ixp_eprint("wmiiwm: can't stat namespace directory \"%s\": %s\n",
namespace, strerror(errno));
if(getuid() != st.st_uid)
2006-10-16 16:43:31 +04:00
ixp_eprint("wmiiwm: namespace directory \"%s\" exists, but is not owned by you",
namespace);
if(st.st_mode & 077)
2006-10-16 18:09:27 +04:00
ixp_eprint("wmiiwm: namespace directory \"%s\" exists, "
"but has group or world permissions",
namespace);
free(namespace);
}
XSetErrorHandler(0);
x_error_handler = XSetErrorHandler(wmii_error_handler);
2006-10-12 18:10:57 +04:00
errstr = NULL;
2006-02-02 16:50:04 +03:00
i = ixp_create_sock(address, &errstr);
if(i < 0)
2006-10-16 16:43:31 +04:00
ixp_eprint("wmiiwm: fatal: %s\n", errstr);
/* start wmiirc */
if(wmiirc) {
int name_len = strlen(wmiirc) + 6;
2006-06-28 06:16:14 +04:00
char execstr[name_len];
2007-02-04 21:31:12 +03:00
signal(SIGCHLD, sigchld_handler);
switch(fork()) {
case 0:
if(setsid() == -1)
2006-10-16 16:43:31 +04:00
ixp_eprint("wmiim: can't setsid: %s\n", strerror(errno));
close(i);
2006-07-03 20:41:14 +04:00
close(ConnectionNumber(blz.dpy));
2006-06-28 06:16:14 +04:00
snprintf(execstr, name_len, "exec %s", wmiirc);
2006-10-12 18:10:57 +04:00
execl("/bin/sh", "sh", "-c", execstr, NULL);
2006-10-16 16:43:31 +04:00
ixp_eprint("wmiiwm: can't exec \"%s\": %s\n", wmiirc, strerror(errno));
case -1:
perror("wmiiwm: cannot fork wmiirc");
default:
break;
}
2006-02-02 16:50:04 +03:00
}
/* IXP server */
2006-10-12 18:10:57 +04:00
ixp_server_open_conn(&srv, i, &p9srv, serve_9pcon, NULL);
2006-02-03 20:11:22 +03:00
/* X server */
2006-10-12 18:10:57 +04:00
ixp_server_open_conn(&srv, ConnectionNumber(blz.dpy), NULL, check_x_event, NULL);
view = NULL;
client = NULL;
key = NULL;
2006-06-27 14:54:21 +04:00
passwd = getpwuid(getuid());
2006-10-12 18:10:57 +04:00
user = ixp_estrdup(passwd->pw_name);
def.colrules.string = NULL;
2006-06-17 15:32:49 +04:00
def.colrules.size = 0;
2006-10-12 18:10:57 +04:00
def.tagrules.string = NULL;
2006-06-17 15:32:49 +04:00
def.tagrules.size = 0;
2006-10-12 18:10:57 +04:00
def.keys = NULL;
def.keyssz = 0;
2006-10-12 18:10:57 +04:00
def.font.fontstr = ixp_estrdup(BLITZ_FONT);
def.border = 2;
def.colmode = Coldefault;
2006-10-12 18:10:57 +04:00
strncpy(def.selcolor.colstr, BLITZ_SELCOLORS, sizeof(def.selcolor.colstr));
2006-10-12 18:12:22 +04:00
loadcolor(&blz, &def.selcolor);
2006-10-12 18:10:57 +04:00
strncpy(def.normcolor.colstr, BLITZ_NORMCOLORS, sizeof(def.normcolor.colstr));
2006-10-12 18:12:22 +04:00
loadcolor(&blz, &def.normcolor);
2006-10-12 18:10:57 +04:00
strncpy(def.grabmod, "Mod1", sizeof(def.grabmod));
def.mod = Mod1Mask;
init_atoms();
init_cursors();
2006-10-12 18:12:22 +04:00
loadfont(&blz, &def.font);
2006-04-12 12:44:07 +04:00
init_lock_keys();
num_screens = 1;
2006-10-12 18:10:57 +04:00
screens = ixp_emallocz(num_screens * sizeof(*screens));
for(i = 0; i < num_screens; i++) {
s = &screens[i];
2006-10-12 18:10:57 +04:00
s->lbar = NULL;
s->rbar = NULL;
s->sel = NULL;
init_screen(s);
2006-07-03 20:41:14 +04:00
pmap = XCreatePixmap(blz.dpy, blz.root, s->rect.width, s->rect.height,
DefaultDepth(blz.dpy, blz.screen));
wa.event_mask = SubstructureRedirectMask | EnterWindowMask | LeaveWindowMask;
wa.cursor = cursor[CurNormal];
2006-07-03 20:41:14 +04:00
XChangeWindowAttributes(blz.dpy, blz.root, CWEventMask | CWCursor, &wa);
wa.override_redirect = 1;
wa.background_pixmap = ParentRelative;
wa.event_mask = ExposureMask | ButtonReleaseMask
| SubstructureRedirectMask | SubstructureNotifyMask;
s->brect = s->rect;
2006-10-12 18:12:22 +04:00
s->brect.height = labelh(&def.font);
s->brect.y = s->rect.height - s->brect.height;
2006-07-03 20:41:14 +04:00
s->barwin = XCreateWindow(blz.dpy, RootWindow(blz.dpy, blz.screen),
s->brect.x, s->brect.y,
s->brect.width, s->brect.height, 0,
2006-07-03 20:41:14 +04:00
DefaultDepth(blz.dpy, blz.screen),
CopyFromParent, DefaultVisual(blz.dpy, blz.screen),
CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
2006-07-03 20:41:14 +04:00
XSync(blz.dpy, False);
s->bbrush.blitz = &blz;
2006-07-03 20:41:14 +04:00
s->bbrush.gc = XCreateGC(blz.dpy, s->barwin, 0, 0);
s->bbrush.drawable = pmap;
s->bbrush.rect = s->brect;
s->bbrush.rect.x = 0;
s->bbrush.rect.y = 0;
s->bbrush.color = def.normcolor;
s->bbrush.font = &def.font;
s->bbrush.border = False;
draw_bar(s);
2006-07-03 20:41:14 +04:00
XMapRaised(blz.dpy, s->barwin);
}
screen = &screens[0];
scan_wins();
2006-06-25 17:16:03 +04:00
update_views();
starting = False;
/* 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);
cleanup();
2006-07-03 20:41:14 +04:00
XCloseDisplay(blz.dpy);
ixp_server_close(&srv);
return errstr ? 1 : 0;
2005-11-18 18:54:58 +03:00
}