Little fix.
[irreco.git] / lirc-0.8.4a / daemons / hw_mouseremote.c
blob8fcd561006d6429cb2df1c9234db9ade31f7a8e1
1 /* $Id: hw_mouseremote.c,v 5.5 2007/07/29 18:20:08 lirc Exp $ */
3 /****************************************************************************
4 ** hw_mouseremote.c ********************************************************
5 ****************************************************************************
7 * routines for X10 Mouse Remote
8 *
9 * Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
10 * modified for logitech receiver by Isaac Lauer <inl101@alumni.psu.edu>
11 * modified for X10 receiver by Shawn Nycz <dscordia@eden.rutgers.edu>
12 * modified for X10 MouseRemote by Brian Craft <bcboy@thecraftstudio.com>
13 * removed dependency on multimouse by Geoffrey Hausheer <zcke0au02@sneakemail.com>
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <limits.h>
26 #include <signal.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <sys/ioctl.h>
31 #include <termios.h>
32 #include <signal.h>
34 #include "hardware.h"
35 #include "serial.h"
36 #include "ir_remote.h"
37 #include "lircd.h"
38 #include "hw_mouseremote.h"
40 #define TIMEOUT 50000
42 extern struct ir_remote *repeat_remote,*last_remote;
44 static struct timeval start,end,last;
45 static lirc_t signal_length;
46 static ir_code pre,code;
47 static int serial_input;
49 struct hardware hw_mouseremote=
51 LIRC_DRIVER_DEVICE, /* default device */
52 -1, /* fd */
53 LIRC_CAN_REC_LIRCCODE, /* features */
54 0, /* send_mode */
55 LIRC_MODE_LIRCCODE, /* rec_mode */
56 32, /* code_length */
57 mouseremote_init, /* init_func */
58 NULL, /* config_func */
59 mouseremote_deinit, /* deinit_func */
60 NULL, /* send_func */
61 mouseremote_rec, /* rec_func */
62 mouseremote_decode, /* decode_func */
63 NULL, /* ioctl_func */
64 NULL, /* readdata */
65 "mouseremote"
67 struct hardware hw_mouseremote_ps2=
69 "/dev/psaux", /* default device */
70 -1, /* fd */
71 LIRC_CAN_REC_LIRCCODE, /* features */
72 0, /* send_mode */
73 LIRC_MODE_LIRCCODE, /* rec_mode */
74 32, /* code_length */
75 mouseremote_ps2_init, /* init_func */
76 NULL, /* config_func */
77 mouseremote_deinit, /* deinit_func */
78 NULL, /* send_func */
79 mouseremote_rec, /* rec_func */
80 mouseremote_decode, /* decode_func */
81 NULL, /* ioctl_func */
82 NULL, /* readdata */
83 "mouseremote_ps2"
86 int mouseremote_decode(struct ir_remote *remote,
87 ir_code *prep,ir_code *codep,ir_code *postp,
88 int *repeat_flagp,
89 lirc_t *min_remaining_gapp,
90 lirc_t *max_remaining_gapp)
92 if(!map_code(remote,prep,codep,postp,
93 8,0x08,16,code,8,0x7f))
95 return(0);
98 map_gap(remote, &start, &last, signal_length, repeat_flagp,
99 min_remaining_gapp, max_remaining_gapp);
101 return(1);
104 int mouseremote_init(void)
106 serial_input = 1;
107 signal_length=hw.code_length*1000000/1200;
109 if(!tty_create_lock(hw.device))
111 logprintf(LOG_ERR,"could not create lock files");
112 return(0);
114 if((hw.fd=open(hw.device,O_RDWR|O_NONBLOCK|O_NOCTTY))<0)
116 logprintf(LOG_ERR,"could not open %s",hw.device);
117 logperror(LOG_ERR,"mouseremote_init()");
118 tty_delete_lock();
119 return(0);
121 if(!tty_reset(hw.fd))
123 logprintf(LOG_ERR,"could not reset tty");
124 mouseremote_deinit();
125 return(0);
127 if(!tty_setbaud(hw.fd,1200))
129 logprintf(LOG_ERR,"could not set baud rate");
130 mouseremote_deinit();
131 return(0);
133 if(!tty_setcsize(hw.fd,7))
135 logprintf(LOG_ERR,"could not set character size");
136 mouseremote_deinit();
137 return(0);
139 return(1);
142 int mouseremote_ps2_init(void)
144 serial_input = 0;
145 signal_length=hw.code_length*1000000/1200;
147 if(!tty_create_lock(hw.device))
149 logprintf(LOG_ERR,"could not create lock files");
150 return(0);
152 if((hw.fd=open(hw.device,O_RDWR|O_NONBLOCK|O_NOCTTY))<0)
154 logprintf(LOG_ERR,"could not open %s",hw.device);
155 logperror(LOG_ERR,"mouseremote_ps2_init()");
156 tty_delete_lock();
157 return(0);
159 return(1);
162 int mouseremote_deinit(void)
164 close(hw.fd);
165 tty_delete_lock();
166 return(1);
169 char *mouseremote_rec(struct ir_remote *remotes)
171 char *m;
172 int i=0, dx = 0, dy = 0, stat = 0;
173 #define NUMBYTES 3
174 unsigned char b[NUMBYTES];
176 b[0]=0x08;
177 b[2]=0x7f;
179 pre=0x08;
181 last=end;
182 gettimeofday(&start,NULL);
183 while(i < 3)
185 int val;
186 if(!waitfordata(TIMEOUT))
188 LOGPRINTF(0,"timeout reading byte %d",i);
189 return(NULL);
191 if((val=read(hw.fd,&b[i],1))!=1)
193 logprintf(LOG_ERR,"reading of byte %d (%d) failed",i,val);
194 logperror(LOG_ERR,NULL);
195 return(NULL);
197 if (i == 0 && (
198 (serial_input && (b[i] & 0xC0) != 0x40) ||
199 (!serial_input && (b[i] & 0x0C) != 0x08))) {
200 continue;
202 if(serial_input &&
203 i && ((b[i] & 0x40) || (b[i] == 0x80))) {
204 /* the PS/2 initialization isn't unique
205 * enough to check the stream for
207 i = 0;
208 continue;
210 LOGPRINTF(1,"byte %d: %02x",i,b[i]);
211 ++i;
213 gettimeofday(&end,NULL);
215 if(serial_input)
217 if (((char)(b[0]) & 0x0c) != 0x0c &&
218 (char)(b[2]) == 0x3f && ((char)(b[2]) & 0x07)) {
219 code=(ir_code) (char)(b[1]) |
220 (((char)(b[0]) & 0x03)<<6);
221 LOGPRINTF(1,"result %llx", (unsigned long long) code);
222 m=decode_all(remotes);
223 return(m);
225 stat = ((b[0] & 0x20) >> 3) |
226 ((b[0] & 0x10) >> 4);
227 dx = (char)(((b[0] & 0x03) << 6) |
228 (b[1] & 0x3F));
229 dy = -((char)(((b[0] & 0x0C) << 4) |
230 (b[2] & 0x3F)));
231 } else {
232 if((char)b[2] == 0x7f) {
233 if ((char)b[0] != 0x08) {
234 LOGPRINTF(1,"Bad data");
235 return(NULL);
237 code = (ir_code)b[1];
238 LOGPRINTF(1,"result %llx", (unsigned long long) code);
239 m=decode_all(remotes);
240 return(m);
242 stat = ((b[0] & 0x01) << 2) |
243 ((b[0] & 0x06) >> 1);
244 dx = (char)b[1];
245 dy = (char)b[2];
247 code = 0;
248 if (dy < 0) {
249 dy = -dy;
250 code |= 0x80;
252 if (dx < 0) {
253 dx = -dx;
254 code |= 0x08;
256 if(dy == 1 || dy == 2 || dy == 8) {
257 code |= 0x10;
258 if (dy == 2 && dx != 1) {
259 code |= 0x0200;
260 } else if (dy == 8) {
261 code |= 0x0400;
264 if(dx == 1 || dx == 2 || dx == 8) {
265 code |= 0x01;
266 if (dx == 2 && dy != 1) {
267 code |= 0x0200;
268 } else if (dx == 8) {
269 code |= 0x0400;
272 if (dy == 4 || dy == 16) {
273 code |= 0x30;
274 } else if (dx == 4 || dx == 16) {
275 code |= 0x03;
277 if (code != 0) {
278 code |= 0x0100;
279 LOGPRINTF(1,"result %llx", (unsigned long long) code);
280 m=decode_all(remotes);
281 return(m);
282 } else if (dx == 0 && dy == 0) {
283 code = 0x0800 | stat;
284 LOGPRINTF(1,"result %llx", (unsigned long long) code);
285 m=decode_all(remotes);
286 return(m);
288 LOGPRINTF(1,"fallthrough is bad!%d %d %d",dx, dy, stat);
289 return(NULL);