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