2007-04-27 11:58:48 -06:00
|
|
|
/*
|
|
|
|
* calmwm - the calm window manager
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004 Marius Aamodt Eriksen <marius@monkey.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2007-05-28 12:34:27 -06:00
|
|
|
* $Id: calmwm.c,v 1.4 2007/05/28 18:34:27 jasper Exp $
|
2007-04-27 11:58:48 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "headers.h"
|
|
|
|
#include "calmwm.h"
|
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
Display *X_Dpy;
|
|
|
|
XFontStruct *X_Font;
|
2007-04-27 11:58:48 -06:00
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
Cursor Cursor_move;
|
|
|
|
Cursor Cursor_resize;
|
|
|
|
Cursor Cursor_select;
|
|
|
|
Cursor Cursor_default;
|
|
|
|
Cursor Cursor_question;
|
2007-04-27 11:58:48 -06:00
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
struct screen_ctx_q Screenq;
|
|
|
|
struct screen_ctx *Curscreen;
|
|
|
|
u_int Nscreens;
|
2007-04-27 11:58:48 -06:00
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
struct client_ctx_q Clientq;
|
2007-04-27 11:58:48 -06:00
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
int Doshape, Shape_ev;
|
|
|
|
int Starting;
|
|
|
|
struct conf Conf;
|
2007-04-27 11:58:48 -06:00
|
|
|
struct fontdesc *DefaultFont;
|
|
|
|
char *DefaultFontName;
|
|
|
|
|
|
|
|
/* From TWM */
|
|
|
|
#define gray_width 2
|
|
|
|
#define gray_height 2
|
|
|
|
static char gray_bits[] = {0x02, 0x01};
|
|
|
|
|
|
|
|
/* List borrowed from 9wm/rio */
|
|
|
|
char *tryfonts[] = {
|
|
|
|
"9x15bold",
|
|
|
|
"blit",
|
|
|
|
"*-lucidatypewriter-bold-*-14-*-75-*",
|
|
|
|
"*-lucidatypewriter-medium-*-12-*-75-*",
|
|
|
|
"fixed",
|
|
|
|
"*",
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void _sigchld_cb(int);
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
int conf_flags = 0;
|
|
|
|
|
2007-05-10 11:23:49 -06:00
|
|
|
char *display_name = NULL;
|
|
|
|
|
2007-04-27 11:58:48 -06:00
|
|
|
DefaultFontName = "sans-serif:pixelsize=14:bold";
|
|
|
|
|
2007-05-10 11:23:49 -06:00
|
|
|
while ((ch = getopt(argc, argv, "d:sf:")) != -1) {
|
2007-04-27 11:58:48 -06:00
|
|
|
switch (ch) {
|
2007-05-10 11:23:49 -06:00
|
|
|
case 'd':
|
|
|
|
display_name = optarg;
|
|
|
|
break;
|
2007-04-27 11:58:48 -06:00
|
|
|
case 's':
|
|
|
|
conf_flags |= CONF_STICKY_GROUPS;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
DefaultFontName = xstrdup(optarg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errx(1, "Unknown option '%c'", ch);
|
|
|
|
}
|
|
|
|
}
|
2007-05-10 11:23:49 -06:00
|
|
|
argc -= optind;
|
2007-05-21 01:53:11 -06:00
|
|
|
argv += optind;
|
2007-04-27 11:58:48 -06:00
|
|
|
|
|
|
|
/* Ignore a few signals. */
|
|
|
|
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
|
|
|
|
err(1, "signal");
|
|
|
|
|
|
|
|
if (signal(SIGCHLD, _sigchld_cb) == SIG_ERR)
|
|
|
|
err(1, "signal");
|
|
|
|
|
|
|
|
group_init();
|
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
Starting = 1;
|
|
|
|
conf_setup(&Conf);
|
|
|
|
Conf.flags |= conf_flags;
|
2007-04-27 11:58:48 -06:00
|
|
|
client_setup();
|
2007-05-10 11:23:49 -06:00
|
|
|
x_setup(display_name);
|
2007-05-28 12:34:27 -06:00
|
|
|
Starting = 0;
|
2007-04-27 11:58:48 -06:00
|
|
|
|
|
|
|
xev_init();
|
|
|
|
XEV_QUICK(NULL, NULL, MapRequest, xev_handle_maprequest, NULL);
|
|
|
|
XEV_QUICK(NULL, NULL, UnmapNotify, xev_handle_unmapnotify, NULL);
|
|
|
|
XEV_QUICK(NULL, NULL, ConfigureRequest,
|
|
|
|
xev_handle_configurerequest, NULL);
|
|
|
|
XEV_QUICK(NULL, NULL, PropertyNotify, xev_handle_propertynotify, NULL);
|
|
|
|
XEV_QUICK(NULL, NULL, EnterNotify, xev_handle_enternotify, NULL);
|
|
|
|
XEV_QUICK(NULL, NULL, LeaveNotify, xev_handle_leavenotify, NULL);
|
|
|
|
XEV_QUICK(NULL, NULL, ButtonPress, xev_handle_buttonpress, NULL);
|
|
|
|
XEV_QUICK(NULL, NULL, ButtonRelease, xev_handle_buttonrelease, NULL);
|
|
|
|
XEV_QUICK(NULL, NULL, KeyPress, xev_handle_keypress, NULL);
|
|
|
|
XEV_QUICK(NULL, NULL, KeyRelease, xev_handle_keyrelease, NULL);
|
|
|
|
XEV_QUICK(NULL, NULL, Expose, xev_handle_expose, NULL);
|
|
|
|
XEV_QUICK(NULL, NULL, DestroyNotify, xev_handle_destroynotify, NULL);
|
|
|
|
XEV_QUICK(NULL, NULL, ClientMessage, xev_handle_clientmessage, NULL);
|
|
|
|
|
|
|
|
xev_loop();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-05-10 11:23:49 -06:00
|
|
|
x_setup(char *display_name)
|
2007-04-27 11:58:48 -06:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct screen_ctx *sc;
|
|
|
|
char *fontname;
|
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
TAILQ_INIT(&Screenq);
|
2007-04-27 11:58:48 -06:00
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
if ((X_Dpy = XOpenDisplay(display_name)) == NULL)
|
2007-04-27 11:58:48 -06:00
|
|
|
errx(1, "%s:%d XOpenDisplay()", __FILE__, __LINE__);
|
|
|
|
|
|
|
|
XSetErrorHandler(x_errorhandler);
|
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
Doshape = XShapeQueryExtension(X_Dpy, &Shape_ev, &i);
|
2007-04-27 11:58:48 -06:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while ((fontname = tryfonts[i++]) != NULL) {
|
2007-05-28 12:34:27 -06:00
|
|
|
if ((X_Font = XLoadQueryFont(X_Dpy, fontname)) != NULL)
|
2007-04-27 11:58:48 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fontname == NULL)
|
|
|
|
errx(1, "Couldn't load any fonts.");
|
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
Nscreens = ScreenCount(X_Dpy);
|
|
|
|
for (i = 0; i < (int)Nscreens; i++) {
|
2007-04-27 11:58:48 -06:00
|
|
|
XMALLOC(sc, struct screen_ctx);
|
|
|
|
x_setupscreen(sc, i);
|
2007-05-28 12:34:27 -06:00
|
|
|
TAILQ_INSERT_TAIL(&Screenq, sc, entry);
|
2007-04-27 11:58:48 -06:00
|
|
|
}
|
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
Cursor_move = XCreateFontCursor(X_Dpy, XC_fleur);
|
|
|
|
Cursor_resize = XCreateFontCursor(X_Dpy, XC_bottom_right_corner);
|
|
|
|
/* (used to be) XCreateFontCursor(X_Dpy, XC_hand1); */
|
|
|
|
Cursor_select = XCreateFontCursor(X_Dpy, XC_hand1);
|
|
|
|
/* Cursor_select = cursor_bigarrow(Curscreen); */
|
|
|
|
Cursor_default = XCreateFontCursor(X_Dpy, XC_X_cursor);
|
|
|
|
/* Cursor_default = cursor_bigarrow(Curscreen); */
|
|
|
|
Cursor_question = XCreateFontCursor(X_Dpy, XC_question_arrow);
|
2007-04-27 11:58:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
x_setupscreen(struct screen_ctx *sc, u_int which)
|
|
|
|
{
|
|
|
|
XColor tmp;
|
|
|
|
XGCValues gv, gv1/* , gv2 */;
|
|
|
|
Window *wins, w0, w1;
|
|
|
|
u_int nwins, i = 0;
|
|
|
|
XWindowAttributes winattr;
|
|
|
|
XSetWindowAttributes rootattr;
|
|
|
|
struct keybinding *kb;
|
|
|
|
|
|
|
|
sc->display = x_screenname(which);
|
|
|
|
sc->which = which;
|
2007-05-28 12:34:27 -06:00
|
|
|
sc->rootwin = RootWindow(X_Dpy, which);
|
|
|
|
XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
|
2007-04-27 11:58:48 -06:00
|
|
|
"black", &sc->fgcolor, &tmp);
|
2007-05-28 12:34:27 -06:00
|
|
|
XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
|
2007-04-27 11:58:48 -06:00
|
|
|
"#00cc00", &sc->bgcolor, &tmp);
|
2007-05-28 12:34:27 -06:00
|
|
|
XAllocNamedColor(X_Dpy,DefaultColormap(X_Dpy, which),
|
2007-04-27 11:58:48 -06:00
|
|
|
"blue", &sc->fccolor, &tmp);
|
2007-05-28 12:34:27 -06:00
|
|
|
XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
|
2007-04-27 11:58:48 -06:00
|
|
|
"red", &sc->redcolor, &tmp);
|
2007-05-28 12:34:27 -06:00
|
|
|
XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
|
2007-04-27 11:58:48 -06:00
|
|
|
"#00ccc8", &sc->cyancolor, &tmp);
|
2007-05-28 12:34:27 -06:00
|
|
|
XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
|
2007-04-27 11:58:48 -06:00
|
|
|
"white", &sc->whitecolor, &tmp);
|
2007-05-28 12:34:27 -06:00
|
|
|
XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
|
2007-04-27 11:58:48 -06:00
|
|
|
"black", &sc->blackcolor, &tmp);
|
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
TAILQ_FOREACH(kb, &Conf.keybindingq, entry)
|
2007-04-27 11:58:48 -06:00
|
|
|
xu_key_grab(sc->rootwin, kb->modmask, kb->keysym);
|
|
|
|
|
|
|
|
/* Special -- for alt state. */
|
|
|
|
/* xu_key_grab(sc->rootwin, 0, XK_Alt_L); */
|
|
|
|
/* xu_key_grab(sc->rootwin, 0, XK_Alt_R); */
|
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
sc->blackpixl = BlackPixel(X_Dpy, sc->which);
|
|
|
|
sc->whitepixl = WhitePixel(X_Dpy, sc->which);
|
2007-04-27 11:58:48 -06:00
|
|
|
sc->bluepixl = sc->fccolor.pixel;
|
|
|
|
sc->redpixl = sc->redcolor.pixel;
|
|
|
|
sc->cyanpixl = sc->cyancolor.pixel;
|
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
sc->gray = XCreatePixmapFromBitmapData(X_Dpy, sc->rootwin,
|
2007-04-27 11:58:48 -06:00
|
|
|
gray_bits, gray_width, gray_height,
|
2007-05-28 12:34:27 -06:00
|
|
|
sc->blackpixl, sc->whitepixl, DefaultDepth(X_Dpy, sc->which));
|
2007-04-27 11:58:48 -06:00
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
sc->blue = XCreatePixmapFromBitmapData(X_Dpy, sc->rootwin,
|
2007-04-27 11:58:48 -06:00
|
|
|
gray_bits, gray_width, gray_height,
|
2007-05-28 12:34:27 -06:00
|
|
|
sc->bluepixl, sc->whitepixl, DefaultDepth(X_Dpy, sc->which));
|
2007-04-27 11:58:48 -06:00
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
sc->red = XCreatePixmapFromBitmapData(X_Dpy, sc->rootwin,
|
2007-04-27 11:58:48 -06:00
|
|
|
gray_bits, gray_width, gray_height,
|
2007-05-28 12:34:27 -06:00
|
|
|
sc->redpixl, sc->whitepixl, DefaultDepth(X_Dpy, sc->which));
|
2007-04-27 11:58:48 -06:00
|
|
|
|
|
|
|
gv.foreground = sc->blackpixl^sc->whitepixl;
|
|
|
|
gv.background = sc->whitepixl;
|
|
|
|
gv.function = GXxor;
|
|
|
|
gv.line_width = 1;
|
|
|
|
gv.subwindow_mode = IncludeInferiors;
|
2007-05-28 12:34:27 -06:00
|
|
|
gv.font = X_Font->fid;
|
2007-04-27 11:58:48 -06:00
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
sc->gc = XCreateGC(X_Dpy, sc->rootwin,
|
2007-04-27 11:58:48 -06:00
|
|
|
GCForeground|GCBackground|GCFunction|
|
|
|
|
GCLineWidth|GCSubwindowMode|GCFont, &gv);
|
|
|
|
|
|
|
|
#ifdef notyet
|
|
|
|
gv2.foreground = sc->blackpixl^sc->cyanpixl;
|
|
|
|
gv2.background = sc->cyanpixl;
|
|
|
|
gv2.function = GXxor;
|
|
|
|
gv2.line_width = 1;
|
|
|
|
gv2.subwindow_mode = IncludeInferiors;
|
2007-05-28 12:34:27 -06:00
|
|
|
gv2.font = X_Font->fid;
|
2007-04-27 11:58:48 -06:00
|
|
|
#endif
|
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
sc->hlgc = XCreateGC(X_Dpy, sc->rootwin,
|
2007-04-27 11:58:48 -06:00
|
|
|
GCForeground|GCBackground|GCFunction|
|
|
|
|
GCLineWidth|GCSubwindowMode|GCFont, &gv);
|
|
|
|
|
|
|
|
gv1.function = GXinvert;
|
|
|
|
gv1.subwindow_mode = IncludeInferiors;
|
|
|
|
gv1.line_width = 1;
|
2007-05-28 12:34:27 -06:00
|
|
|
gv1.font = X_Font->fid;
|
2007-04-27 11:58:48 -06:00
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
sc->invgc = XCreateGC(X_Dpy, sc->rootwin,
|
2007-04-27 11:58:48 -06:00
|
|
|
GCFunction|GCSubwindowMode|GCLineWidth|GCFont, &gv1);
|
|
|
|
|
|
|
|
font_init(sc);
|
|
|
|
DefaultFont = font_getx(sc, DefaultFontName);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX - this should *really* be in screen_init(). ordering
|
|
|
|
* problem.
|
|
|
|
*/
|
|
|
|
TAILQ_INIT(&sc->mruq);
|
|
|
|
|
|
|
|
/* Initialize menu window. */
|
|
|
|
grab_menuinit(sc);
|
|
|
|
search_init(sc);
|
|
|
|
|
|
|
|
/* Deal with existing clients. */
|
2007-05-28 12:34:27 -06:00
|
|
|
XQueryTree(X_Dpy, sc->rootwin, &w0, &w1, &wins, &nwins);
|
2007-04-27 11:58:48 -06:00
|
|
|
|
|
|
|
for (i = 0; i < nwins; i++) {
|
2007-05-28 12:34:27 -06:00
|
|
|
XGetWindowAttributes(X_Dpy, wins[i], &winattr);
|
2007-04-27 11:58:48 -06:00
|
|
|
if (winattr.override_redirect ||
|
|
|
|
winattr.map_state != IsViewable) {
|
|
|
|
char *name;
|
2007-05-28 12:34:27 -06:00
|
|
|
XFetchName(X_Dpy, wins[i], &name);
|
2007-04-27 11:58:48 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
client_new(wins[i], sc, winattr.map_state != IsUnmapped);
|
|
|
|
}
|
|
|
|
XFree(wins);
|
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
Curscreen = sc; /* XXX */
|
2007-04-27 11:58:48 -06:00
|
|
|
screen_init();
|
|
|
|
screen_updatestackingorder();
|
|
|
|
|
|
|
|
rootattr.event_mask = ChildMask|PropertyChangeMask|EnterWindowMask|
|
|
|
|
LeaveWindowMask|ColormapChangeMask|ButtonMask;
|
|
|
|
|
|
|
|
/* Set the root cursor to a nice obnoxious arrow :-) */
|
|
|
|
/* rootattr.cursor = cursor_bigarrow(sc); */
|
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
XChangeWindowAttributes(X_Dpy, sc->rootwin,
|
2007-04-27 11:58:48 -06:00
|
|
|
/* CWCursor| */CWEventMask, &rootattr);
|
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
XSync(X_Dpy, False);
|
2007-04-27 11:58:48 -06:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
x_screenname(int which)
|
|
|
|
{
|
|
|
|
char *cp, *dstr, *sn;
|
|
|
|
size_t snlen;
|
|
|
|
|
|
|
|
if (which > 9)
|
|
|
|
errx(1, "Can't handle more than 9 screens. If you need it, "
|
|
|
|
"tell <marius@monkey.org>. It's a trivial fix.");
|
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
dstr = xstrdup(DisplayString(X_Dpy));
|
2007-04-27 11:58:48 -06:00
|
|
|
|
|
|
|
if ((cp = rindex(dstr, ':')) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if ((cp = index(cp, '.')) != NULL)
|
|
|
|
*cp = '\0';
|
|
|
|
|
|
|
|
snlen = strlen(dstr) + 3; /* string, dot, number, null */
|
|
|
|
sn = (char *)xmalloc(snlen);
|
|
|
|
snprintf(sn, snlen, "%s.%d", dstr, which);
|
|
|
|
free(dstr);
|
|
|
|
|
|
|
|
return (sn);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
x_errorhandler(Display *dpy, XErrorEvent *e)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
char msg[80], number[80], req[80];
|
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
XGetErrorText(X_Dpy, e->error_code, msg, sizeof(msg));
|
2007-04-27 11:58:48 -06:00
|
|
|
snprintf(number, sizeof(number), "%d", e->request_code);
|
2007-05-28 12:34:27 -06:00
|
|
|
XGetErrorDatabaseText(X_Dpy, "XRequest", number,
|
2007-04-27 11:58:48 -06:00
|
|
|
"<unknown>", req, sizeof(req));
|
|
|
|
|
|
|
|
warnx("%s(0x%x): %s", req, (u_int)e->resourceid, msg);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-05-28 12:34:27 -06:00
|
|
|
if (Starting &&
|
2007-04-27 11:58:48 -06:00
|
|
|
e->error_code == BadAccess &&
|
|
|
|
e->request_code == X_GrabKey)
|
|
|
|
errx(1, "root window unavailable - perhaps another "
|
|
|
|
"wm is running?");
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_sigchld_cb(int which)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
/* Collect dead children. */
|
|
|
|
while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
|
|
|
|
(pid < 0 && errno == EINTR))
|
|
|
|
;
|
|
|
|
}
|