Debian Codegraph @

contrib/xmindpath/main.c

raw
1 /*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $Id: main.c,v 1.5 1998/09/30 23:58:17 itojun Exp $
30 */
31 /*
32 * Most of X11 code was derived from twiddler driver.
33 */
34
35 #include <sys/time.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <stdlib.h>
39 #include <fcntl.h>
40 #include <termios.h>
41 #include <signal.h>
42 #include <X11/X.h>
43 #include <X11/extensions/XTest.h>
44 #include <X11/keysym.h>
45
46 #ifdef X_DISPLAY_MISSING
47 # error this program cannot be compiled without X11.
48 #endif
49
50 #ifndef REMOTE_DEVICE
51 # define REMOTE_DEVICE "/dev/tty02" /*biased to VAIO505 :-P*/
52 #endif
53
54 static char *remote = REMOTE_DEVICE;
55 static int debug = 0;
56 #define dprintf(x) { if (debug) fprintf x; }
57 static int verbose = 0;
58 #define verbmsg(x) { if (verbose) fprintf x; }
59
60 Display *display = NULL;
61 static uid_t uid, euid;
62 static gid_t gid, egid;
63 static int uidswapped = 0;
64
65 static void usage __P((void));
66 static void mainloop __P((int));
67 static void buttonpress __P((int, int));
68 static int uucplock __P((char *));
69 static int uucpunlock __P((char *));
70 static RETSIGTYPE sigtrap __P((int));
71 static void daemonuid __P((void));
72 static void useruid __P((void));
73
74 #define EFFECT 0x15
75 #define LEFT 0x16
76 #define RIGHT 0x17
77 #define RELEASE 0x55
78
79 int
80 main(argc, argv)
81 int argc;
82 char **argv;
83 {
84 int fd;
85 struct termios old, raw;
86 int ch;
87 extern int optind;
88 extern char *optarg;
89
90 uid = getuid();
91 euid = geteuid();
92 gid = getgid();
93 egid = getegid();
94 useruid();
95
96 while ((ch = getopt(argc, argv, "df:v")) != EOF) {
97 switch (ch) {
98 case 'd':
99 debug++;
100 break;
101 case 'f':
102 remote = optarg;
103 break;
104 case 'v':
105 verbose++;
106 break;
107 default:
108 usage();
109 /*NOTREACHED*/
110 }
111 }
112 argc -= optind;
113 argv += optind;
114
115 fprintf(stderr, "initializing...\r");
116
117 verbmsg((stderr, "initializing X11... "));
118 display = XOpenDisplay(getenv("DISPLAY"));
119 if (!display)
120 errx(1, "opening X11 display");
121 verbmsg((stderr, "done.\n"));
122
123 verbmsg((stderr, "initializing serial port %s... ", remote));
124 daemonuid();
125 if (!strchr(remote, '/')) {
126 char *p;
127 p = (char *)malloc(strlen("/dev/") + strlen(remote) + 1);
128 if (!p)
129 err(1, "malloc");
130 sprintf(p, "/dev/%s", remote);
131 remote = p;
132 }
133 if (uucplock(remote) < 0) {
134 errx(1, "cannot lock %s", remote);
135 /*NOTREACHED*/
136 }
137 signal(SIGINT, sigtrap);
138 signal(SIGHUP, sigtrap);
139 fd = open(remote, O_NONBLOCK);
140 if (fd < 0) {
141 uucpunlock(remote);
142 err(1, "%s", remote);
143 /*NOTREACHED*/
144 }
145 tcgetattr(fd, &old);
146 raw = old;
147 cfmakeraw(&raw);
148 cfsetspeed(&raw, B1200);
149 raw.c_cflag &= ~(CSIZE|PARENB|CSTOPB|MDMBUF);
150 raw.c_cflag |= CS8|CREAD|CLOCAL;
151 tcsetattr(fd, TCSANOW, &raw);
152 sleep(1);
153 close(fd);
154 sleep(1);
155
156 fd = open(remote, O_RDONLY);
157 if (fd < 0) {
158 uucpunlock(remote);
159 err(1, "%s", remote);
160 /*NOTREACHED*/
161 }
162 useruid();
163 verbmsg((stderr, "done.\n"));
164
165 fprintf(stderr, "xmindpath ready.\n");
166 fprintf(stderr, "\007");
167
168 mainloop(fd);
169
170 uucpunlock(remote);
171 exit(0);
172 }
173
174 static void
175 usage()
176 {
177 fprintf(stderr, "usage: xmindpath [-dv] [-f dev]\n");
178 exit(0);
179 }
180
181 static void
182 mainloop(fd)
183 int fd;
184 {
185 struct timeval tv;
186 struct fd_set rfd;
187 u_char buf[BUFSIZ];
188 int nfd;
189 int len;
190 int heartbeat = 0;
191 int i;
192 int keystate;
193
194 keystate = 0;
195 while (1) {
196 tv.tv_sec = 0;
197 tv.tv_usec = 500*1000;
198 FD_ZERO(&rfd);
199 FD_SET(fd, &rfd);
200 nfd = select(fd + 1, &rfd, 0, 0, &tv);
201 if (nfd < 0) {
202 uucpunlock(remote);
203 err(1, "select");
204 /*NOTREACHED*/
205 }
206
207 switch (nfd) {
208 case 0: /*timeout*/
209 dprintf((stderr, "%c\r", "\\|/-"[heartbeat++ % 4]));
210 break;
211 case 1:
212 len = read(fd, buf, sizeof(buf));
213 for (i = 0; i < len; i++) {
214 /* filter out bogus remote commander code */
215 switch (buf[i]) {
216 case LEFT:
217 case RIGHT:
218 case EFFECT:
219 case RELEASE:
220 break;
221 default:
222 dprintf((stderr, "bogus char: %02x\n",
223 buf[i]));
224 continue;
225 }
226
227 switch (buf[i]) {
228 case LEFT:
229 dprintf((stderr, "left\n"));
230 buttonpress(Button3, 1);
231 break;
232 case RIGHT:
233 dprintf((stderr, "right\n"));
234 buttonpress(Button1, 1);
235 break;
236 case EFFECT:
237 dprintf((stderr, "effect\n"));
238 buttonpress(Button2, 1);
239 break;
240 case RELEASE:
241 dprintf((stderr, "release\n"));
242 buttonpress(Button1, 0);
243 buttonpress(Button2, 0);
244 buttonpress(Button3, 0);
245 break;
246 default:
247 dprintf((stderr, "%02x\n", buf[i]));
248 continue;
249 }
250 }
251 break;
252 default:
253 dprintf((stderr, "select=%d\n", nfd));
254 break;
255 }
256 }
257 }
258
259 /* fake mouse button press */
260 static void
261 buttonpress(button, state)
262 int button;
263 int state;
264 {
265 int offset;
266 static int buttonstate[5]; /* X11 defines 5 buttons */
267
268 offset = button - Button1;
269 if (offset < 0 || sizeof(buttonstate)/sizeof(buttonstate[0]) <= offset)
270 return;
271
272 /* edge trigger */
273 if ((buttonstate[offset] && state) || (!buttonstate[offset] && !state))
274 return;
275
276 XTestFakeButtonEvent(display, button, state ? True : False, 0);
277 buttonstate[offset] = state;
278 XFlush(display);
279 }
280
281 static int
282 uucplock(name)
283 char *name;
284 {
285 return uu_lock(strrchr(name, '/') + 1);
286 }
287
288 static int
289 uucpunlock(name)
290 char *name;
291 {
292 return uu_unlock(strrchr(name, '/') + 1);
293 }
294
295 static RETSIGTYPE
296 sigtrap(no)
297 int no;
298 {
299 uucpunlock(remote);
300 exit(1);
301 }
302
303 static void
304 daemonuid()
305 {
306 if (uidswapped == 0)
307 return;
308
309 #ifdef HAVE_SETREUID
310 setreuid(uid, euid);
311 setregid(gid, egid);
312 #else
313 setuid(uid);
314 seteuid(euid);
315 setgid(gid);
316 setegid(egid);
317 #endif
318 uidswapped = 0;
319 }
320
321 static void
322 useruid()
323 {
324 if (uidswapped == 1)
325 return;
326
327 #ifdef HAVE_SETREUID
328 setregid(egid, gid);
329 setreuid(euid, uid);
330 #else
331 setgid(egid);
332 setegid(gid);
333 setuid(euid);
334 seteuid(uid);
335 #endif
336 uidswapped = 1;
337 }