2007-04-27 21:58:48 +04:00
|
|
|
/*
|
|
|
|
* calmwm - the calm window manager
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004 Marius Aamodt Eriksen <marius@monkey.org>
|
2008-01-11 19:06:44 +03:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2007-04-27 21:58:48 +04:00
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "headers.h"
|
|
|
|
#include "calmwm.h"
|
|
|
|
|
2007-05-28 22:34:27 +04:00
|
|
|
Display *X_Dpy;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2007-05-28 22:34:27 +04:00
|
|
|
Cursor Cursor_move;
|
|
|
|
Cursor Cursor_resize;
|
|
|
|
Cursor Cursor_select;
|
|
|
|
Cursor Cursor_default;
|
|
|
|
Cursor Cursor_question;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2007-05-28 22:34:27 +04:00
|
|
|
struct screen_ctx_q Screenq;
|
|
|
|
struct screen_ctx *Curscreen;
|
|
|
|
u_int Nscreens;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2007-05-28 22:34:27 +04:00
|
|
|
struct client_ctx_q Clientq;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2007-05-28 22:34:27 +04:00
|
|
|
int Doshape, Shape_ev;
|
2008-09-30 03:16:46 +04:00
|
|
|
int HasXinerama, HasRandr, Randr_ev;
|
2007-05-28 22:34:27 +04:00
|
|
|
int Starting;
|
|
|
|
struct conf Conf;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
/* From TWM */
|
|
|
|
#define gray_width 2
|
|
|
|
#define gray_height 2
|
|
|
|
static char gray_bits[] = {0x02, 0x01};
|
|
|
|
|
2008-04-16 00:24:41 +04:00
|
|
|
static void _sigchld_cb(int);
|
2008-07-23 00:42:24 +04:00
|
|
|
static void dpy_init(const char *);
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2008-07-11 18:21:28 +04:00
|
|
|
const char *conf_file = NULL;
|
|
|
|
char *display_name = NULL;
|
|
|
|
int ch;
|
2007-05-10 21:23:49 +04:00
|
|
|
|
2008-03-23 18:09:21 +03:00
|
|
|
while ((ch = getopt(argc, argv, "c:d:")) != -1) {
|
2007-04-27 21:58:48 +04:00
|
|
|
switch (ch) {
|
2008-03-23 18:09:21 +03:00
|
|
|
case 'c':
|
2008-05-19 22:53:09 +04:00
|
|
|
conf_file = optarg;
|
2008-03-23 18:09:21 +03:00
|
|
|
break;
|
2007-05-10 21:23:49 +04:00
|
|
|
case 'd':
|
|
|
|
display_name = optarg;
|
|
|
|
break;
|
2007-04-27 21:58:48 +04:00
|
|
|
default:
|
2008-02-14 00:04:19 +03:00
|
|
|
usage();
|
2007-04-27 21:58:48 +04:00
|
|
|
}
|
|
|
|
}
|
2007-05-10 21:23:49 +04:00
|
|
|
argc -= optind;
|
2007-05-21 11:53:11 +04:00
|
|
|
argv += optind;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2008-04-16 00:24:41 +04:00
|
|
|
if (signal(SIGCHLD, _sigchld_cb) == SIG_ERR)
|
|
|
|
err(1, "signal");
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
group_init();
|
|
|
|
|
2007-05-28 22:34:27 +04:00
|
|
|
Starting = 1;
|
2008-07-23 00:42:24 +04:00
|
|
|
dpy_init(display_name);
|
2008-04-15 22:22:08 +04:00
|
|
|
bzero(&Conf, sizeof(Conf));
|
2008-05-19 22:53:09 +04:00
|
|
|
conf_setup(&Conf, conf_file);
|
2007-04-27 21:58:48 +04:00
|
|
|
client_setup();
|
2008-07-23 00:42:24 +04:00
|
|
|
x_setup();
|
2007-05-28 22:34:27 +04:00
|
|
|
Starting = 0;
|
2007-04-27 21:58:48 +04: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);
|
2008-07-23 01:01:54 +04:00
|
|
|
XEV_QUICK(NULL, NULL, MappingNotify, xev_handle_mapping, NULL);
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
xev_loop();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-07-23 00:42:24 +04:00
|
|
|
dpy_init(const char *dpyname)
|
2007-04-27 21:58:48 +04:00
|
|
|
{
|
2008-07-23 00:42:24 +04:00
|
|
|
int i;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2008-07-23 00:42:24 +04:00
|
|
|
if ((X_Dpy = XOpenDisplay(dpyname)) == NULL)
|
2008-02-13 15:09:47 +03:00
|
|
|
errx(1, "unable to open display \"%s\"",
|
2008-07-23 00:42:24 +04:00
|
|
|
XDisplayName(dpyname));
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
XSetErrorHandler(x_errorhandler);
|
|
|
|
|
2007-05-28 22:34:27 +04:00
|
|
|
Doshape = XShapeQueryExtension(X_Dpy, &Shape_ev, &i);
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2008-09-30 03:16:46 +04:00
|
|
|
HasRandr = XRRQueryExtension(X_Dpy, &Randr_ev, &i);
|
|
|
|
|
2008-07-23 00:42:24 +04:00
|
|
|
TAILQ_INIT(&Screenq);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
x_setup(void)
|
|
|
|
{
|
|
|
|
struct screen_ctx *sc;
|
2008-07-23 00:51:54 +04:00
|
|
|
struct keybinding *kb;
|
2008-07-23 00:42:24 +04:00
|
|
|
int i;
|
|
|
|
|
2007-05-28 22:34:27 +04:00
|
|
|
Nscreens = ScreenCount(X_Dpy);
|
|
|
|
for (i = 0; i < (int)Nscreens; i++) {
|
2008-09-30 03:16:46 +04:00
|
|
|
XCALLOC(sc, struct screen_ctx);
|
2007-04-27 21:58:48 +04:00
|
|
|
x_setupscreen(sc, i);
|
2007-05-28 22:34:27 +04:00
|
|
|
TAILQ_INSERT_TAIL(&Screenq, sc, entry);
|
2007-04-27 21:58:48 +04:00
|
|
|
}
|
|
|
|
|
2008-07-23 00:51:54 +04:00
|
|
|
/*
|
|
|
|
* XXX key grabs weren't done before, since Screenq was empty,
|
|
|
|
* do them here for now (this needs changing).
|
|
|
|
*/
|
|
|
|
TAILQ_FOREACH(kb, &Conf.keybindingq, entry)
|
|
|
|
conf_grab(&Conf, kb);
|
|
|
|
|
|
|
|
|
2007-05-28 22:34:27 +04:00
|
|
|
Cursor_move = XCreateFontCursor(X_Dpy, XC_fleur);
|
|
|
|
Cursor_resize = XCreateFontCursor(X_Dpy, XC_bottom_right_corner);
|
|
|
|
Cursor_select = XCreateFontCursor(X_Dpy, XC_hand1);
|
|
|
|
Cursor_default = XCreateFontCursor(X_Dpy, XC_X_cursor);
|
|
|
|
Cursor_question = XCreateFontCursor(X_Dpy, XC_question_arrow);
|
2007-04-27 21:58:48 +04:00
|
|
|
}
|
|
|
|
|
2008-01-16 14:39:20 +03:00
|
|
|
void
|
2007-04-27 21:58:48 +04:00
|
|
|
x_setupscreen(struct screen_ctx *sc, u_int which)
|
|
|
|
{
|
2008-07-11 18:21:28 +04:00
|
|
|
XColor tmp;
|
|
|
|
XGCValues gv;
|
|
|
|
Window *wins, w0, w1;
|
|
|
|
XWindowAttributes winattr;
|
|
|
|
XSetWindowAttributes rootattr;
|
2008-09-30 03:16:46 +04:00
|
|
|
int fake;
|
2008-07-11 18:21:28 +04:00
|
|
|
u_int nwins, i;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2008-06-15 06:47:46 +04:00
|
|
|
Curscreen = sc;
|
|
|
|
|
2007-04-27 21:58:48 +04:00
|
|
|
sc->display = x_screenname(which);
|
|
|
|
sc->which = which;
|
2007-05-28 22:34:27 +04:00
|
|
|
sc->rootwin = RootWindow(X_Dpy, which);
|
2008-05-20 18:50:51 +04:00
|
|
|
|
|
|
|
sc->xmax = DisplayWidth(X_Dpy, sc->which);
|
|
|
|
sc->ymax = DisplayHeight(X_Dpy, sc->which);
|
|
|
|
|
2007-05-28 22:34:27 +04:00
|
|
|
XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
|
2007-04-27 21:58:48 +04:00
|
|
|
"black", &sc->fgcolor, &tmp);
|
2007-05-28 22:34:27 +04:00
|
|
|
XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
|
2007-04-27 21:58:48 +04:00
|
|
|
"#00cc00", &sc->bgcolor, &tmp);
|
2007-05-28 22:34:27 +04:00
|
|
|
XAllocNamedColor(X_Dpy,DefaultColormap(X_Dpy, which),
|
2007-04-27 21:58:48 +04:00
|
|
|
"blue", &sc->fccolor, &tmp);
|
2007-05-28 22:34:27 +04:00
|
|
|
XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
|
2007-04-27 21:58:48 +04:00
|
|
|
"red", &sc->redcolor, &tmp);
|
2007-05-28 22:34:27 +04:00
|
|
|
XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
|
2007-04-27 21:58:48 +04:00
|
|
|
"#00ccc8", &sc->cyancolor, &tmp);
|
2007-05-28 22:34:27 +04:00
|
|
|
XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
|
2007-04-27 21:58:48 +04:00
|
|
|
"white", &sc->whitecolor, &tmp);
|
2007-05-28 22:34:27 +04:00
|
|
|
XAllocNamedColor(X_Dpy, DefaultColormap(X_Dpy, which),
|
2007-04-27 21:58:48 +04:00
|
|
|
"black", &sc->blackcolor, &tmp);
|
|
|
|
|
2007-05-28 22:34:27 +04:00
|
|
|
sc->blackpixl = BlackPixel(X_Dpy, sc->which);
|
|
|
|
sc->whitepixl = WhitePixel(X_Dpy, sc->which);
|
2007-04-27 21:58:48 +04:00
|
|
|
sc->bluepixl = sc->fccolor.pixel;
|
|
|
|
sc->redpixl = sc->redcolor.pixel;
|
|
|
|
sc->cyanpixl = sc->cyancolor.pixel;
|
|
|
|
|
2008-04-16 00:24:41 +04:00
|
|
|
sc->gray = XCreatePixmapFromBitmapData(X_Dpy, sc->rootwin,
|
|
|
|
gray_bits, gray_width, gray_height,
|
|
|
|
sc->blackpixl, sc->whitepixl, DefaultDepth(X_Dpy, sc->which));
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2008-04-16 00:24:41 +04:00
|
|
|
sc->blue = XCreatePixmapFromBitmapData(X_Dpy, sc->rootwin,
|
|
|
|
gray_bits, gray_width, gray_height,
|
|
|
|
sc->bluepixl, sc->whitepixl, DefaultDepth(X_Dpy, sc->which));
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2008-04-16 00:24:41 +04:00
|
|
|
sc->red = XCreatePixmapFromBitmapData(X_Dpy, sc->rootwin,
|
|
|
|
gray_bits, gray_width, gray_height,
|
2007-05-28 22:34:27 +04:00
|
|
|
sc->redpixl, sc->whitepixl, DefaultDepth(X_Dpy, sc->which));
|
2007-04-27 21:58:48 +04: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 22:34:27 +04:00
|
|
|
sc->gc = XCreateGC(X_Dpy, sc->rootwin,
|
2007-04-27 21:58:48 +04:00
|
|
|
GCForeground|GCBackground|GCFunction|
|
2008-01-16 14:39:20 +03:00
|
|
|
GCLineWidth|GCSubwindowMode, &gv);
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
font_init(sc);
|
2008-06-15 06:47:46 +04:00
|
|
|
conf_font(&Conf);
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
TAILQ_INIT(&sc->mruq);
|
|
|
|
|
|
|
|
/* Initialize menu window. */
|
2008-05-21 18:11:19 +04:00
|
|
|
menu_init(sc);
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
/* Deal with existing clients. */
|
2008-04-16 00:24:41 +04:00
|
|
|
XQueryTree(X_Dpy, sc->rootwin, &w0, &w1, &wins, &nwins);
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
for (i = 0; i < nwins; i++) {
|
2007-05-28 22:34:27 +04:00
|
|
|
XGetWindowAttributes(X_Dpy, wins[i], &winattr);
|
2007-04-27 21:58:48 +04:00
|
|
|
if (winattr.override_redirect ||
|
|
|
|
winattr.map_state != IsViewable) {
|
|
|
|
char *name;
|
2007-05-28 22:34:27 +04:00
|
|
|
XFetchName(X_Dpy, wins[i], &name);
|
2007-04-27 21:58:48 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
client_new(wins[i], sc, winattr.map_state != IsUnmapped);
|
|
|
|
}
|
|
|
|
XFree(wins);
|
|
|
|
|
|
|
|
screen_updatestackingorder();
|
|
|
|
|
|
|
|
rootattr.event_mask = ChildMask|PropertyChangeMask|EnterWindowMask|
|
|
|
|
LeaveWindowMask|ColormapChangeMask|ButtonMask;
|
|
|
|
|
2007-05-28 22:34:27 +04:00
|
|
|
XChangeWindowAttributes(X_Dpy, sc->rootwin,
|
2008-05-19 22:53:09 +04:00
|
|
|
CWEventMask, &rootattr);
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2008-09-30 03:16:46 +04:00
|
|
|
if (XineramaQueryExtension(X_Dpy, &fake, &fake) == 1 &&
|
|
|
|
((HasXinerama = XineramaIsActive(X_Dpy)) == 1))
|
|
|
|
HasXinerama = 1;
|
|
|
|
if (HasRandr)
|
|
|
|
XRRSelectInput(X_Dpy, sc->rootwin, RRScreenChangeNotifyMask);
|
|
|
|
/*
|
|
|
|
* initial setup of xinerama screens, if we're using RandR then we'll
|
|
|
|
* redo this whenever the screen changes since a CTRC may have been
|
|
|
|
* added or removed
|
|
|
|
*/
|
|
|
|
screen_init_xinerama(sc);
|
|
|
|
|
2007-05-28 22:34:27 +04:00
|
|
|
XSync(X_Dpy, False);
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2008-01-16 14:39:20 +03:00
|
|
|
return;
|
2007-04-27 21:58:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
x_screenname(int which)
|
|
|
|
{
|
2008-07-11 18:21:28 +04:00
|
|
|
char *cp, *dstr, *sn;
|
|
|
|
size_t snlen;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
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 22:34:27 +04:00
|
|
|
dstr = xstrdup(DisplayString(X_Dpy));
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2008-05-19 22:53:09 +04:00
|
|
|
if ((cp = strrchr(dstr, ':')) == NULL)
|
2007-04-27 21:58:48 +04:00
|
|
|
return (NULL);
|
|
|
|
|
2008-05-19 22:53:09 +04:00
|
|
|
if ((cp = strchr(cp, '.')) != NULL)
|
2007-04-27 21:58:48 +04:00
|
|
|
*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 22:34:27 +04:00
|
|
|
XGetErrorText(X_Dpy, e->error_code, msg, sizeof(msg));
|
2007-04-27 21:58:48 +04:00
|
|
|
snprintf(number, sizeof(number), "%d", e->request_code);
|
2007-05-28 22:34:27 +04:00
|
|
|
XGetErrorDatabaseText(X_Dpy, "XRequest", number,
|
2007-04-27 21:58:48 +04:00
|
|
|
"<unknown>", req, sizeof(req));
|
|
|
|
|
|
|
|
warnx("%s(0x%x): %s", req, (u_int)e->resourceid, msg);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-04-16 00:24:41 +04:00
|
|
|
if (Starting &&
|
|
|
|
e->error_code == BadAccess &&
|
|
|
|
e->request_code == X_GrabKey)
|
2007-04-27 21:58:48 +04:00
|
|
|
errx(1, "root window unavailable - perhaps another "
|
2008-04-16 00:24:41 +04:00
|
|
|
"wm is running?");
|
2007-04-27 21:58:48 +04:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_sigchld_cb(int which)
|
|
|
|
{
|
2008-07-11 18:21:28 +04:00
|
|
|
pid_t pid;
|
|
|
|
int save_errno = errno;
|
|
|
|
int status;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2008-04-16 00:24:41 +04:00
|
|
|
/* Collect dead children. */
|
|
|
|
while ((pid = waitpid(-1, &status, WNOHANG)) > 0 ||
|
|
|
|
(pid < 0 && errno == EINTR))
|
2007-04-27 21:58:48 +04:00
|
|
|
;
|
2008-05-06 19:12:04 +04:00
|
|
|
|
|
|
|
errno = save_errno;
|
2007-04-27 21:58:48 +04:00
|
|
|
}
|
2008-02-14 00:04:19 +03:00
|
|
|
|
2008-02-14 00:48:03 +03:00
|
|
|
__dead void
|
2008-02-14 00:04:19 +03:00
|
|
|
usage(void)
|
|
|
|
{
|
2008-07-11 18:21:28 +04:00
|
|
|
extern char *__progname;
|
2008-02-14 00:04:19 +03:00
|
|
|
|
2008-06-25 05:09:09 +04:00
|
|
|
fprintf(stderr, "usage: %s [-c file] [-d display]\n", __progname);
|
2008-02-14 00:04:19 +03:00
|
|
|
exit(1);
|
|
|
|
}
|