2004-01-21 01:24:28 +03:00
|
|
|
/* $NetBSD: wsmoused.c,v 1.19 2004/01/20 22:24:28 matt Exp $ */
|
2002-06-27 03:13:04 +04:00
|
|
|
|
|
|
|
/*
|
2004-01-05 13:56:02 +03:00
|
|
|
* Copyright (c) 2002, 2003, 2004 The NetBSD Foundation, Inc.
|
2002-06-27 03:13:04 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2003-08-06 22:07:53 +04:00
|
|
|
* by Julio M. Merino Vidal.
|
2002-06-27 03:13:04 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. The name authors may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
#ifndef lint
|
2003-08-06 22:07:53 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 2002, 2003\n"
|
|
|
|
"The NetBSD Foundation, Inc. All rights reserved.\n");
|
2004-01-21 01:24:28 +03:00
|
|
|
__RCSID("$NetBSD: wsmoused.c,v 1.19 2004/01/20 22:24:28 matt Exp $");
|
2002-06-27 03:13:04 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/tty.h>
|
|
|
|
#include <dev/wscons/wsconsio.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
2003-08-07 03:58:40 +04:00
|
|
|
#include <stdarg.h>
|
2002-06-27 03:13:04 +04:00
|
|
|
#include <stdlib.h>
|
2003-08-07 03:58:40 +04:00
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
2002-06-27 03:13:04 +04:00
|
|
|
#include <unistd.h>
|
2003-03-05 01:31:15 +03:00
|
|
|
#include <util.h>
|
2002-06-27 03:13:04 +04:00
|
|
|
|
|
|
|
#include "pathnames.h"
|
|
|
|
#include "wsmoused.h"
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* --------------------------------------------------------------------- */
|
2002-06-27 03:13:04 +04:00
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/*
|
|
|
|
* Global variables.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct mouse Mouse;
|
|
|
|
static char *Pid_File = NULL;
|
2003-08-07 03:58:40 +04:00
|
|
|
static int Foreground = 1;
|
2003-08-06 22:07:53 +04:00
|
|
|
static int X_Console = -1;
|
2004-01-05 15:16:25 +03:00
|
|
|
static int X_Console_Delay = 5;
|
2003-08-06 22:07:53 +04:00
|
|
|
|
2003-08-07 02:11:48 +04:00
|
|
|
#ifdef WSMOUSED_SELECTION_MODE
|
|
|
|
extern struct mode_bootstrap Action_Mode;
|
|
|
|
#endif
|
2003-08-06 22:07:53 +04:00
|
|
|
#ifdef WSMOUSED_SELECTION_MODE
|
|
|
|
extern struct mode_bootstrap Selection_Mode;
|
|
|
|
#endif
|
|
|
|
|
2003-08-07 02:11:48 +04:00
|
|
|
#define MAX_MODES 2
|
2003-08-06 22:07:53 +04:00
|
|
|
static struct mode_bootstrap *Modes[MAX_MODES];
|
|
|
|
static struct mode_bootstrap *Avail_Modes[] = {
|
2003-08-07 02:11:48 +04:00
|
|
|
#ifdef WSMOUSED_ACTION_MODE
|
|
|
|
&Action_Mode,
|
|
|
|
#endif
|
2003-08-06 22:07:53 +04:00
|
|
|
#ifdef WSMOUSED_SELECTION_MODE
|
|
|
|
&Selection_Mode,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
2002-06-27 03:13:04 +04:00
|
|
|
|
|
|
|
/*
|
2003-08-06 22:07:53 +04:00
|
|
|
* Prototypes for functions private to this module.
|
2002-06-27 03:13:04 +04:00
|
|
|
*/
|
2003-08-06 22:07:53 +04:00
|
|
|
|
|
|
|
static void usage(void);
|
|
|
|
static void open_device(unsigned int);
|
|
|
|
static void init_mouse(void);
|
|
|
|
static void event_loop(void);
|
|
|
|
static void generic_wscons_event(struct wscons_event);
|
|
|
|
static int attach_mode(const char *);
|
|
|
|
static void attach_modes(char *);
|
|
|
|
static void detach_mode(const char *);
|
|
|
|
static void detach_modes(void);
|
|
|
|
static void signal_terminate(int);
|
|
|
|
int main(int, char **);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Shows program usage information and exits. */
|
2002-06-27 03:13:04 +04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2003-08-06 22:07:53 +04:00
|
|
|
|
2002-06-27 03:13:04 +04:00
|
|
|
(void)fprintf(stderr,
|
2004-01-06 02:23:32 +03:00
|
|
|
"usage: %s [-d device] [-f config_file] [-m modes] [-n]\n",
|
2002-06-27 03:13:04 +04:00
|
|
|
getprogname());
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* --------------------------------------------------------------------- */
|
2002-06-27 03:13:04 +04:00
|
|
|
|
2003-08-07 03:58:40 +04:00
|
|
|
/* Logs the given error message to syslog if running in daemon mode, or
|
|
|
|
* to the console if running in the foreground. */
|
|
|
|
void
|
|
|
|
log_err(int e, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if (Foreground)
|
|
|
|
verr(e, fmt, ap);
|
|
|
|
else {
|
|
|
|
int olderrno = errno;
|
|
|
|
vsyslog(LOG_DAEMON | LOG_ERR, fmt, ap);
|
|
|
|
errno = olderrno;
|
|
|
|
syslog(LOG_DAEMON | LOG_ERR, "%m");
|
|
|
|
exit(e);
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Logs the given error message to syslog if running in daemon mode, or
|
|
|
|
* to the console if running in the foreground. */
|
|
|
|
void
|
|
|
|
log_errx(int e, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if (Foreground)
|
|
|
|
verrx(e, fmt, ap);
|
|
|
|
else {
|
|
|
|
vsyslog(LOG_DAEMON | LOG_ERR, fmt, ap);
|
|
|
|
exit(e);
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Logs the given info message to syslog if running in daemon mode, or
|
|
|
|
* to the console if running in the foreground. */
|
|
|
|
void
|
|
|
|
log_info(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if (Foreground)
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
else
|
|
|
|
vsyslog(LOG_DAEMON | LOG_INFO, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Logs the given warning message to syslog if running in daemon mode, or
|
|
|
|
* to the console if running in the foreground. */
|
|
|
|
void
|
|
|
|
log_warn(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if (Foreground)
|
|
|
|
vwarn(fmt, ap);
|
|
|
|
else {
|
|
|
|
int olderrno = errno;
|
|
|
|
vsyslog(LOG_DAEMON | LOG_WARNING, fmt, ap);
|
|
|
|
errno = olderrno;
|
|
|
|
syslog(LOG_DAEMON | LOG_WARNING, "%m");
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Logs the given warning message to syslog if running in daemon mode, or
|
|
|
|
* to the console if running in the foreground. */
|
|
|
|
void
|
|
|
|
log_warnx(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if (Foreground)
|
|
|
|
vwarnx(fmt, ap);
|
|
|
|
else
|
|
|
|
vsyslog(LOG_DAEMON | LOG_WARNING, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* Initializes mouse information. Basically, it opens required files
|
|
|
|
* for the daemon to work. */
|
2002-06-27 03:13:04 +04:00
|
|
|
static void
|
2003-08-06 22:07:53 +04:00
|
|
|
init_mouse(void)
|
2002-06-27 03:13:04 +04:00
|
|
|
{
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
Mouse.m_devfd = -1;
|
|
|
|
open_device(0);
|
2002-06-27 03:13:04 +04:00
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* Open FIFO, if wanted */
|
|
|
|
Mouse.m_fifofd = -1;
|
|
|
|
if (Mouse.m_fifoname != NULL) {
|
|
|
|
Mouse.m_fifofd = open(Mouse.m_fifoname,
|
|
|
|
O_RDWR | O_NONBLOCK, 0);
|
|
|
|
if (Mouse.m_fifofd == -1)
|
2003-08-07 03:58:40 +04:00
|
|
|
log_err(EXIT_FAILURE, "cannot open %s",
|
|
|
|
Mouse.m_fifoname);
|
2002-06-27 03:13:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Opens the mouse device (if not already opened). The argument `secs'
|
|
|
|
* specifies how much seconds the function will wait before trying to
|
|
|
|
* open the device; this is used when returning from the X console. */
|
2002-06-27 03:13:04 +04:00
|
|
|
static void
|
2003-08-06 22:07:53 +04:00
|
|
|
open_device(unsigned int secs)
|
2002-06-27 03:13:04 +04:00
|
|
|
{
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
if (Mouse.m_devfd != -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sleep(secs);
|
2002-06-27 03:13:04 +04:00
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* Open mouse file descriptor */
|
|
|
|
Mouse.m_devfd = open(Mouse.m_devname, O_RDONLY | O_NONBLOCK, 0);
|
|
|
|
if (Mouse.m_devfd == -1)
|
2003-08-07 03:58:40 +04:00
|
|
|
log_err(EXIT_FAILURE, "cannot open %s", Mouse.m_devname);
|
2002-06-27 03:13:04 +04:00
|
|
|
}
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Main program event loop. This function polls the wscons status
|
|
|
|
* device and the mouse device; whenever an event is received, the
|
|
|
|
* appropiate callback is fired for all attached modes. If the polls
|
|
|
|
* times out (which only appens when the mouse is disabled), another
|
|
|
|
* callback is launched. */
|
2002-06-27 03:13:04 +04:00
|
|
|
static void
|
|
|
|
event_loop(void)
|
|
|
|
{
|
2003-08-06 22:07:53 +04:00
|
|
|
int i, res;
|
2002-06-27 03:13:04 +04:00
|
|
|
struct pollfd fds[2];
|
|
|
|
struct wscons_event event;
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
fds[0].fd = Mouse.m_statfd;
|
2002-06-27 03:13:04 +04:00
|
|
|
fds[0].events = POLLIN;
|
|
|
|
|
|
|
|
for (;;) {
|
2003-08-06 22:07:53 +04:00
|
|
|
fds[1].fd = Mouse.m_devfd;
|
2002-06-27 03:13:04 +04:00
|
|
|
fds[1].events = POLLIN;
|
2003-08-06 22:07:53 +04:00
|
|
|
if (Mouse.m_disabled)
|
2002-06-27 03:13:04 +04:00
|
|
|
res = poll(fds, 1, INFTIM);
|
|
|
|
else
|
|
|
|
res = poll(fds, 2, 300);
|
2002-07-05 00:50:29 +04:00
|
|
|
|
2002-06-27 03:13:04 +04:00
|
|
|
if (res < 0)
|
2003-08-07 03:58:40 +04:00
|
|
|
log_warn("failed to read from devices");
|
2002-06-27 03:13:04 +04:00
|
|
|
|
|
|
|
if (fds[0].revents & POLLIN) {
|
2003-08-06 22:07:53 +04:00
|
|
|
res = read(Mouse.m_statfd, &event, sizeof(event));
|
2002-06-27 03:13:04 +04:00
|
|
|
if (res != sizeof(event))
|
2003-08-07 03:58:40 +04:00
|
|
|
log_warn("failed to read from mouse stat");
|
2003-08-06 22:07:53 +04:00
|
|
|
|
2004-01-05 15:01:52 +03:00
|
|
|
for (i = 0; i < MAX_MODES && Modes[i] != NULL; i++)
|
|
|
|
if (Modes[i]->mb_wscons_event != NULL)
|
2004-01-21 01:11:02 +03:00
|
|
|
Modes[i]->mb_wscons_event(event, 1);
|
2004-01-05 15:01:52 +03:00
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
generic_wscons_event(event);
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_MODES && Modes[i] != NULL; i++)
|
|
|
|
if (Modes[i]->mb_wscons_event != NULL)
|
2004-01-21 01:11:02 +03:00
|
|
|
Modes[i]->mb_wscons_event(event, 0);
|
2003-08-06 22:07:53 +04:00
|
|
|
|
2002-06-27 03:13:04 +04:00
|
|
|
} else if (fds[1].revents & POLLIN) {
|
2003-08-06 22:07:53 +04:00
|
|
|
res = read(Mouse.m_devfd, &event, sizeof(event));
|
2002-06-27 03:13:04 +04:00
|
|
|
if (res != sizeof(event))
|
2003-08-07 03:58:40 +04:00
|
|
|
log_warn("failed to read from mouse");
|
2002-06-27 03:13:04 +04:00
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
if (Mouse.m_fifofd >= 0) {
|
|
|
|
res = write(Mouse.m_fifofd, &event,
|
2002-06-27 03:13:04 +04:00
|
|
|
sizeof(event));
|
|
|
|
if (res != sizeof(event))
|
2003-08-07 03:58:40 +04:00
|
|
|
log_warn("failed to write to fifo");
|
2002-06-27 03:13:04 +04:00
|
|
|
}
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
for (i = 0; i < MAX_MODES && Modes[i] != NULL; i++)
|
|
|
|
if (Modes[i]->mb_wsmouse_event != NULL)
|
|
|
|
Modes[i]->mb_wsmouse_event(event);
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < MAX_MODES && Modes[i] != NULL; i++)
|
|
|
|
if (Modes[i]->mb_poll_timeout != NULL)
|
|
|
|
Modes[i]->mb_poll_timeout();
|
|
|
|
}
|
2002-06-27 03:13:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* This function parses generic wscons status events. Actually, it
|
|
|
|
* handles the screen switch event to enable or disable the mouse,
|
|
|
|
* depending if we are entering or leaving the X console. */
|
2002-06-27 03:13:04 +04:00
|
|
|
static void
|
2003-08-06 22:07:53 +04:00
|
|
|
generic_wscons_event(struct wscons_event evt)
|
2002-06-27 03:13:04 +04:00
|
|
|
{
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
switch (evt.type) {
|
|
|
|
case WSCONS_EVENT_SCREEN_SWITCH:
|
|
|
|
if (evt.value == X_Console) {
|
|
|
|
Mouse.m_disabled = 1;
|
|
|
|
(void)close(Mouse.m_devfd);
|
|
|
|
Mouse.m_devfd = -1;
|
|
|
|
} else {
|
|
|
|
if (Mouse.m_disabled) {
|
2004-01-05 15:16:25 +03:00
|
|
|
open_device(X_Console_Delay);
|
2003-08-06 22:07:53 +04:00
|
|
|
Mouse.m_disabled = 0;
|
|
|
|
} else {
|
|
|
|
(void)close(Mouse.m_devfd);
|
|
|
|
Mouse.m_devfd = -1;
|
|
|
|
open_device(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2002-06-27 03:13:04 +04:00
|
|
|
}
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Attaches a mode to the list of active modes, based on its name.
|
|
|
|
* Returns 1 on success or 0 if the mode fails to initialize or there is
|
|
|
|
* any other problem. */
|
|
|
|
static int
|
|
|
|
attach_mode(const char *name)
|
2002-06-27 03:13:04 +04:00
|
|
|
{
|
2003-08-06 22:07:53 +04:00
|
|
|
int i, pos;
|
|
|
|
struct mode_bootstrap *mb;
|
|
|
|
|
|
|
|
for (i = 0, pos = -1; i < MAX_MODES; i++)
|
|
|
|
if (Modes[i] == NULL) {
|
|
|
|
pos = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (pos == -1) {
|
2003-08-07 03:58:40 +04:00
|
|
|
log_warnx("modes table full; cannot register `%s'", name);
|
2003-08-06 22:07:53 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_MODES; i++) {
|
|
|
|
mb = Avail_Modes[i];
|
|
|
|
if (mb != NULL && strcmp(name, mb->mb_name) == 0) {
|
|
|
|
int res;
|
|
|
|
|
|
|
|
res = mb->mb_startup(&Mouse);
|
|
|
|
if (res == 0) {
|
2003-08-07 03:58:40 +04:00
|
|
|
log_warnx("startup failed for `%s' mode",
|
2003-08-06 22:07:53 +04:00
|
|
|
mb->mb_name);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
Modes[pos] = mb;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-07 03:58:40 +04:00
|
|
|
log_warnx("unknown mode `%s' (see the `modes' directive)", name);
|
2003-08-06 22:07:53 +04:00
|
|
|
return 0;
|
2002-06-27 03:13:04 +04:00
|
|
|
}
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Attaches all modes given in the whitespace separated string `list'.
|
|
|
|
* A fatal error is produced if no active modes can be attached. */
|
|
|
|
static void
|
|
|
|
attach_modes(char *list)
|
2002-06-27 03:13:04 +04:00
|
|
|
{
|
2003-08-06 22:07:53 +04:00
|
|
|
char *last, *p;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
/* Attach all requested modes */
|
|
|
|
(void)memset(&Modes, 0, sizeof(struct mode_bootstrap) * MAX_MODES);
|
|
|
|
for (count = 0, (p = strtok_r(list, " ", &last)); p;
|
|
|
|
(p = strtok_r(NULL, " ", &last))) {
|
|
|
|
if (attach_mode(p))
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count == 0)
|
2003-08-07 03:58:40 +04:00
|
|
|
log_errx(EXIT_FAILURE, "no active modes found; exiting...");
|
2002-06-27 03:13:04 +04:00
|
|
|
}
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* --------------------------------------------------------------------- */
|
2002-06-27 03:13:04 +04:00
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* Detaches a mode from the active modes list based on its name. */
|
|
|
|
static void
|
|
|
|
detach_mode(const char *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct mode_bootstrap *mb;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_MODES; i++) {
|
|
|
|
mb = Modes[i];
|
|
|
|
if (mb != NULL && strcmp(name, mb->mb_name) == 0) {
|
|
|
|
int res;
|
|
|
|
|
|
|
|
res = mb->mb_cleanup();
|
|
|
|
if (res == 0) {
|
2003-08-07 03:58:40 +04:00
|
|
|
log_warnx("cleanup failed for `%s' mode",
|
2003-08-06 22:07:53 +04:00
|
|
|
mb->mb_name);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
Modes[i] = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2002-06-27 03:13:04 +04:00
|
|
|
}
|
2003-08-06 22:07:53 +04:00
|
|
|
|
2003-08-07 03:58:40 +04:00
|
|
|
log_warnx("unknown mode `%s' (see the `modes' directive)", name);
|
2002-06-27 03:13:04 +04:00
|
|
|
}
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Detaches all active modes. */
|
|
|
|
static void
|
|
|
|
detach_modes(void)
|
2002-06-27 03:13:04 +04:00
|
|
|
{
|
2003-08-06 22:07:53 +04:00
|
|
|
int i;
|
2002-06-27 03:13:04 +04:00
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
for (i = 0; i < MAX_MODES && Modes[i] != NULL; i++)
|
|
|
|
detach_mode(Modes[i]->mb_name);
|
|
|
|
}
|
2002-06-27 03:13:04 +04:00
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* --------------------------------------------------------------------- */
|
2002-06-27 03:13:04 +04:00
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* Signal handler for close signals. The program can only be exited
|
|
|
|
* through this function. */
|
|
|
|
/* ARGSUSED */
|
|
|
|
static void
|
|
|
|
signal_terminate(int sig)
|
|
|
|
{
|
2002-06-27 03:13:04 +04:00
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
detach_modes();
|
|
|
|
config_free();
|
|
|
|
exit(EXIT_SUCCESS);
|
2002-06-27 03:13:04 +04:00
|
|
|
}
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Main program. Parses command line options, reads the configuration
|
|
|
|
* file, initializes the mouse and associated files and launches the main
|
|
|
|
* event loop. */
|
2002-06-27 03:13:04 +04:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2003-08-06 22:07:53 +04:00
|
|
|
char *conffile, *modelist, *tstat;
|
|
|
|
int needconf, nodaemon, opt;
|
|
|
|
struct block *conf;
|
2002-06-27 03:13:04 +04:00
|
|
|
|
|
|
|
setprogname(argv[0]);
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
(void)memset(&Mouse, 0, sizeof(struct mouse));
|
2003-03-04 17:33:55 +03:00
|
|
|
conffile = _PATH_CONF;
|
2003-08-06 22:07:53 +04:00
|
|
|
modelist = NULL;
|
|
|
|
needconf = 0;
|
|
|
|
nodaemon = -1;
|
2002-12-25 22:13:53 +03:00
|
|
|
|
2002-06-27 03:13:04 +04:00
|
|
|
/* Parse command line options */
|
2003-08-06 22:07:53 +04:00
|
|
|
while ((opt = getopt(argc, argv, "d:f:m:n")) != -1) {
|
2002-06-27 03:13:04 +04:00
|
|
|
switch (opt) {
|
|
|
|
case 'd': /* Mouse device name */
|
2003-08-06 22:07:53 +04:00
|
|
|
Mouse.m_devname = optarg;
|
2002-06-27 03:13:04 +04:00
|
|
|
break;
|
2003-03-04 17:33:55 +03:00
|
|
|
case 'f': /* Configuration file name */
|
|
|
|
needconf = 1;
|
|
|
|
conffile = optarg;
|
2002-12-25 22:13:53 +03:00
|
|
|
break;
|
2003-08-06 22:07:53 +04:00
|
|
|
case 'm': /* List of modes to activate */
|
|
|
|
modelist = optarg;
|
|
|
|
break;
|
2002-06-27 03:13:04 +04:00
|
|
|
case 'n': /* No daemon */
|
2003-03-04 17:33:55 +03:00
|
|
|
nodaemon = 1;
|
2002-08-20 20:55:28 +04:00
|
|
|
break;
|
2002-06-27 03:13:04 +04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* Read the configuration file and get some basic properties */
|
2003-03-04 17:33:55 +03:00
|
|
|
config_read(conffile, needconf);
|
2003-08-06 22:07:53 +04:00
|
|
|
conf = config_get_mode("Global");
|
2003-03-04 17:33:55 +03:00
|
|
|
if (nodaemon == -1)
|
2003-08-06 22:07:53 +04:00
|
|
|
nodaemon = block_get_propval_int(conf, "nodaemon", 0);
|
|
|
|
X_Console = block_get_propval_int(conf, "xconsole", -1);
|
2004-01-05 15:16:25 +03:00
|
|
|
X_Console_Delay = block_get_propval_int(conf, "xconsole_delay",
|
|
|
|
X_Console_Delay);
|
2003-03-04 17:33:55 +03:00
|
|
|
|
2003-08-06 22:07:53 +04:00
|
|
|
/* Open wsdisplay status device */
|
|
|
|
tstat = block_get_propval(conf, "ttystat", _PATH_TTYSTAT);
|
|
|
|
Mouse.m_statfd = open(tstat, O_RDONLY | O_NONBLOCK, 0);
|
|
|
|
if (Mouse.m_statfd == -1)
|
2003-08-07 03:58:40 +04:00
|
|
|
log_err(EXIT_FAILURE, "cannot open %s", tstat);
|
2003-08-06 22:07:53 +04:00
|
|
|
|
|
|
|
/* Initialize mouse information and attach modes */
|
|
|
|
if (Mouse.m_devname == NULL)
|
|
|
|
Mouse.m_devname = block_get_propval(conf, "device",
|
|
|
|
_PATH_DEFAULT_MOUSE);
|
2004-01-05 13:56:02 +03:00
|
|
|
Mouse.m_fifoname = block_get_propval(conf, "fifo", NULL);
|
2003-08-06 22:07:53 +04:00
|
|
|
init_mouse();
|
|
|
|
if (modelist != NULL)
|
|
|
|
attach_modes(modelist);
|
|
|
|
else
|
|
|
|
attach_modes(block_get_propval(conf, "modes", "selection"));
|
2002-06-27 03:13:04 +04:00
|
|
|
|
|
|
|
/* Setup signal handlers */
|
|
|
|
(void)signal(SIGINT, signal_terminate);
|
|
|
|
(void)signal(SIGKILL, signal_terminate);
|
|
|
|
(void)signal(SIGQUIT, signal_terminate);
|
|
|
|
(void)signal(SIGTERM, signal_terminate);
|
|
|
|
|
2003-03-05 01:31:15 +03:00
|
|
|
if (!nodaemon) {
|
|
|
|
/* Become a daemon */
|
|
|
|
if (daemon(0, 0) == -1)
|
2003-08-07 03:58:40 +04:00
|
|
|
log_err(EXIT_FAILURE, "failed to become a daemon");
|
2003-03-05 01:31:15 +03:00
|
|
|
|
|
|
|
/* Create the pidfile, if wanted */
|
2003-08-06 22:07:53 +04:00
|
|
|
Pid_File = block_get_propval(conf, "pidfile", NULL);
|
|
|
|
if (pidfile(Pid_File) == -1)
|
2003-08-07 03:58:40 +04:00
|
|
|
log_warn("pidfile %s", Pid_File);
|
|
|
|
|
|
|
|
Foreground = 0;
|
2003-03-05 01:31:15 +03:00
|
|
|
}
|
2003-08-06 22:07:53 +04:00
|
|
|
|
2002-06-27 03:13:04 +04:00
|
|
|
event_loop();
|
|
|
|
|
2003-03-04 17:33:55 +03:00
|
|
|
/* NOTREACHED */
|
2002-06-27 03:13:04 +04:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|