1/* $NetBSD: action.c,v 1.1 2003/08/06 22:11:49 jmmv Exp $ */
2
3/*
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. The name authors may not be used to endorse or promote products
16 *    derived from this software without specific prior written
17 *    permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33
34#ifndef lint
35__RCSID("$NetBSD: action.c,v 1.1 2003/08/06 22:11:49 jmmv Exp $");
36#endif /* not lint */
37
38#include <sys/ioctl.h>
39#include <sys/time.h>
40#include <sys/types.h>
41#include <sys/tty.h>
42#include <dev/wscons/wsconsio.h>
43
44#include <err.h>
45#include <stdio.h>
46#include <stdlib.h>
47
48#include "pathnames.h"
49#include "wsmoused.h"
50
51/* ---------------------------------------------------------------------- */
52
53/*
54 * Public interface exported by the `action' mode.
55 */
56
57int  action_startup(struct mouse *m);
58int  action_cleanup(void);
59void action_wsmouse_event(struct wscons_event);
60
61struct mode_bootstrap Action_Mode = {
62	"action",
63	action_startup,
64	action_cleanup,
65	action_wsmouse_event,
66	NULL,
67	NULL
68};
69
70/* ---------------------------------------------------------------------- */
71
72/*
73 * Global variables.
74 */
75
76static int Initialized = 0;
77
78/* ---------------------------------------------------------------------- */
79
80/*
81 * Prototypes for functions private to this module.
82 */
83
84static void run_action(int, const char *);
85
86/* ---------------------------------------------------------------------- */
87
88/* Initializes the action mode.  Does nothing, aside from checking it is
89 * not initialized twice. */
90/* ARGSUSED */
91int
92action_startup(struct mouse *m)
93{
94
95	if (Initialized) {
96		log_warnx("action mode already initialized");
97		return 1;
98	}
99
100	Initialized = 1;
101
102	return 1;
103}
104
105/* ---------------------------------------------------------------------- */
106
107/* Mode cleanup. */
108int
109action_cleanup(void)
110{
111
112	return 1;
113}
114
115/* ---------------------------------------------------------------------- */
116
117/* Parses wsmouse events.  Only button events are picked. */
118void
119action_wsmouse_event(struct wscons_event evt)
120{
121
122	if (IS_BUTTON_EVENT(evt.type)) {
123		switch (evt.type) {
124		case WSCONS_EVENT_MOUSE_UP:
125			run_action(evt.value, "up");
126			break;
127
128		case WSCONS_EVENT_MOUSE_DOWN:
129			run_action(evt.value, "down");
130			break;
131
132		default:
133			log_warnx("unknown button event");
134		}
135	}
136}
137
138/* ---------------------------------------------------------------------- */
139
140/* Executes a command.  `button' specifies the number of the button that
141 * was pressed or released.  `ud' contains the word `up' or `down', which
142 * specify the type of event received. */
143static void
144run_action(int button, const char *ud)
145{
146	char buf[20];
147	const char *cmd;
148	struct block *conf;
149
150	(void)snprintf(buf, sizeof(buf), "button_%d_%s", button, ud);
151
152	conf = config_get_mode("action");
153	cmd = block_get_propval(conf, buf, NULL);
154	if (cmd != NULL) {
155		log_info("running command `%s'", cmd);
156		system(cmd);
157	}
158}
159