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
|
|
|
*
|
2011-05-11 17:53:51 +04:00
|
|
|
* $OpenBSD$
|
2007-04-27 21:58:48 +04:00
|
|
|
*/
|
|
|
|
|
2009-12-15 07:10:42 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2009-12-15 06:34:34 +03:00
|
|
|
#include <getopt.h>
|
2012-08-07 18:05:49 +04:00
|
|
|
#include <locale.h>
|
2012-12-18 03:03:41 +04:00
|
|
|
#include <pwd.h>
|
2009-12-15 06:34:34 +03:00
|
|
|
#include <signal.h>
|
2012-11-09 07:52:02 +04:00
|
|
|
#include <stdio.h>
|
2009-12-15 07:10:42 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2009-12-15 06:34:34 +03:00
|
|
|
|
2007-04-27 21:58:48 +04:00
|
|
|
#include "calmwm.h"
|
|
|
|
|
2012-10-31 23:30:19 +04:00
|
|
|
char **cwm_argv;
|
2007-05-28 22:34:27 +04:00
|
|
|
Display *X_Dpy;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2009-08-25 01:22:48 +04:00
|
|
|
struct screen_ctx_q Screenq = TAILQ_HEAD_INITIALIZER(Screenq);
|
|
|
|
struct client_ctx_q Clientq = TAILQ_HEAD_INITIALIZER(Clientq);
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2012-07-19 01:53:22 +04:00
|
|
|
int HasRandr, Randr_ev;
|
2007-05-28 22:34:27 +04:00
|
|
|
struct conf Conf;
|
2012-12-18 03:03:41 +04:00
|
|
|
char *homedir;
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2009-05-18 04:23:35 +04:00
|
|
|
static void sigchld_cb(int);
|
2009-06-26 16:21:58 +04:00
|
|
|
static int x_errorhandler(Display *, XErrorEvent *);
|
2013-06-17 18:08:51 +04:00
|
|
|
static void x_init(const char *);
|
2009-04-15 18:01:45 +04:00
|
|
|
static void x_teardown(void);
|
2013-06-17 18:08:51 +04:00
|
|
|
static int x_wmerrorhandler(Display *, XErrorEvent *);
|
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;
|
2012-12-18 04:14:41 +04:00
|
|
|
char *conf_path, *display_name = NULL;
|
2008-07-11 18:21:28 +04:00
|
|
|
int ch;
|
2012-12-18 03:03:41 +04:00
|
|
|
struct passwd *pw;
|
2007-05-10 21:23:49 +04:00
|
|
|
|
2012-08-07 18:05:49 +04:00
|
|
|
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
|
|
|
|
warnx("no locale support");
|
|
|
|
mbtowc(NULL, NULL, MB_CUR_MAX);
|
|
|
|
|
2012-10-31 23:30:19 +04:00
|
|
|
cwm_argv = argv;
|
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
|
|
|
|
2009-05-18 04:23:35 +04:00
|
|
|
if (signal(SIGCHLD, sigchld_cb) == SIG_ERR)
|
2008-04-16 00:24:41 +04:00
|
|
|
err(1, "signal");
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2012-12-18 03:03:41 +04:00
|
|
|
if ((homedir = getenv("HOME")) == NULL || *homedir == '\0') {
|
|
|
|
pw = getpwuid(getuid());
|
|
|
|
if (pw != NULL && pw->pw_dir != NULL && *pw->pw_dir != '\0')
|
|
|
|
homedir = pw->pw_dir;
|
|
|
|
else
|
|
|
|
homedir = "/";
|
|
|
|
}
|
|
|
|
|
2012-12-18 04:14:41 +04:00
|
|
|
if (conf_file == NULL)
|
|
|
|
xasprintf(&conf_path, "%s/%s", homedir, CONFFILE);
|
|
|
|
else
|
|
|
|
conf_path = xstrdup(conf_file);
|
|
|
|
|
|
|
|
if (access(conf_path, R_OK) != 0) {
|
|
|
|
if (conf_file != NULL)
|
|
|
|
warn("%s", conf_file);
|
|
|
|
free(conf_path);
|
|
|
|
conf_path = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
conf_init(&Conf);
|
|
|
|
if (conf_path && (parse_config(conf_path, &Conf) == -1))
|
|
|
|
warnx("config file %s has errors, not loading", conf_path);
|
|
|
|
free(conf_path);
|
|
|
|
|
2013-06-17 18:08:51 +04:00
|
|
|
x_init(display_name);
|
2007-04-27 21:58:48 +04:00
|
|
|
xev_loop();
|
2009-04-15 18:01:45 +04:00
|
|
|
x_teardown();
|
|
|
|
|
2007-04-27 21:58:48 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2009-06-26 16:21:58 +04:00
|
|
|
static void
|
2013-06-17 18:08:51 +04:00
|
|
|
x_init(const char *dpyname)
|
2007-04-27 21:58:48 +04:00
|
|
|
{
|
2012-07-19 01:53:22 +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)
|
2013-07-08 19:46:16 +04:00
|
|
|
errx(1, "unable to open display \"%s\"", XDisplayName(dpyname));
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2011-06-24 09:58:51 +04:00
|
|
|
XSetErrorHandler(x_wmerrorhandler);
|
|
|
|
XSelectInput(X_Dpy, DefaultRootWindow(X_Dpy), SubstructureRedirectMask);
|
|
|
|
XSync(X_Dpy, False);
|
2007-04-27 21:58:48 +04:00
|
|
|
XSetErrorHandler(x_errorhandler);
|
|
|
|
|
2008-09-30 03:16:46 +04:00
|
|
|
HasRandr = XRRQueryExtension(X_Dpy, &Randr_ev, &i);
|
2008-07-23 00:42:24 +04:00
|
|
|
|
2013-06-17 18:08:51 +04:00
|
|
|
xu_getatoms();
|
2008-07-23 00:42:24 +04:00
|
|
|
|
2013-06-17 21:11:10 +04:00
|
|
|
conf_cursor(&Conf);
|
2011-06-24 10:52:23 +04:00
|
|
|
|
2013-04-12 18:46:30 +04:00
|
|
|
for (i = 0; i < ScreenCount(X_Dpy); i++)
|
|
|
|
screen_init(i);
|
2007-04-27 21:58:48 +04:00
|
|
|
}
|
|
|
|
|
2009-06-26 16:21:58 +04:00
|
|
|
static void
|
2009-04-15 18:01:45 +04:00
|
|
|
x_teardown(void)
|
|
|
|
{
|
|
|
|
XCloseDisplay(X_Dpy);
|
|
|
|
}
|
|
|
|
|
2011-06-24 09:58:51 +04:00
|
|
|
static int
|
|
|
|
x_wmerrorhandler(Display *dpy, XErrorEvent *e)
|
|
|
|
{
|
|
|
|
errx(1, "root window unavailable - perhaps another wm is running?");
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2012-05-13 19:15:54 +04:00
|
|
|
|
2009-06-26 16:21:58 +04:00
|
|
|
static int
|
2007-04-27 21:58:48 +04:00
|
|
|
x_errorhandler(Display *dpy, XErrorEvent *e)
|
|
|
|
{
|
2013-05-14 17:39:53 +04:00
|
|
|
#ifdef DEBUG
|
2011-06-24 09:58:51 +04:00
|
|
|
char msg[80], number[80], req[80];
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2011-06-24 09:58:51 +04:00
|
|
|
XGetErrorText(X_Dpy, e->error_code, msg, sizeof(msg));
|
2011-07-25 19:10:24 +04:00
|
|
|
(void)snprintf(number, sizeof(number), "%d", e->request_code);
|
2011-06-24 09:58:51 +04:00
|
|
|
XGetErrorDatabaseText(X_Dpy, "XRequest", number,
|
|
|
|
"<unknown>", req, sizeof(req));
|
2007-04-27 21:58:48 +04:00
|
|
|
|
2011-06-24 09:58:51 +04:00
|
|
|
warnx("%s(0x%x): %s", req, (u_int)e->resourceid, msg);
|
2007-04-27 21:58:48 +04:00
|
|
|
#endif
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-18 04:23:35 +04:00
|
|
|
sigchld_cb(int which)
|
2007-04-27 21:58:48 +04:00
|
|
|
{
|
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
|
|
|
|
2011-07-25 19:10:24 +04:00
|
|
|
(void)fprintf(stderr, "usage: %s [-c file] [-d display]\n",
|
|
|
|
__progname);
|
2008-02-14 00:04:19 +03:00
|
|
|
exit(1);
|
|
|
|
}
|