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
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>
28 #include <sys/types.h>
29 #include <sys/ioctl.h>
36 #include "ir_remote.h"
38 #include "hw_mouseremote.h"
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 */
53 LIRC_CAN_REC_LIRCCODE
, /* features */
55 LIRC_MODE_LIRCCODE
, /* rec_mode */
57 mouseremote_init
, /* init_func */
58 NULL
, /* config_func */
59 mouseremote_deinit
, /* deinit_func */
61 mouseremote_rec
, /* rec_func */
62 mouseremote_decode
, /* decode_func */
63 NULL
, /* ioctl_func */
67 struct hardware hw_mouseremote_ps2
=
69 "/dev/psaux", /* default device */
71 LIRC_CAN_REC_LIRCCODE
, /* features */
73 LIRC_MODE_LIRCCODE
, /* rec_mode */
75 mouseremote_ps2_init
, /* init_func */
76 NULL
, /* config_func */
77 mouseremote_deinit
, /* deinit_func */
79 mouseremote_rec
, /* rec_func */
80 mouseremote_decode
, /* decode_func */
81 NULL
, /* ioctl_func */
86 int mouseremote_decode(struct ir_remote
*remote
,
87 ir_code
*prep
,ir_code
*codep
,ir_code
*postp
,
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))
98 map_gap(remote
, &start
, &last
, signal_length
, repeat_flagp
,
99 min_remaining_gapp
, max_remaining_gapp
);
104 int mouseremote_init(void)
107 signal_length
=hw
.code_length
*1000000/1200;
109 if(!tty_create_lock(hw
.device
))
111 logprintf(LOG_ERR
,"could not create lock files");
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()");
121 if(!tty_reset(hw
.fd
))
123 logprintf(LOG_ERR
,"could not reset tty");
124 mouseremote_deinit();
127 if(!tty_setbaud(hw
.fd
,1200))
129 logprintf(LOG_ERR
,"could not set baud rate");
130 mouseremote_deinit();
133 if(!tty_setcsize(hw
.fd
,7))
135 logprintf(LOG_ERR
,"could not set character size");
136 mouseremote_deinit();
142 int mouseremote_ps2_init(void)
145 signal_length
=hw
.code_length
*1000000/1200;
147 if(!tty_create_lock(hw
.device
))
149 logprintf(LOG_ERR
,"could not create lock files");
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()");
162 int mouseremote_deinit(void)
169 char *mouseremote_rec(struct ir_remote
*remotes
)
172 int i
=0, dx
= 0, dy
= 0, stat
= 0;
174 unsigned char b
[NUMBYTES
];
182 gettimeofday(&start
,NULL
);
186 if(!waitfordata(TIMEOUT
))
188 LOGPRINTF(0,"timeout reading byte %d",i
);
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
);
198 (serial_input
&& (b
[i
] & 0xC0) != 0x40) ||
199 (!serial_input
&& (b
[i
] & 0x0C) != 0x08))) {
203 i
&& ((b
[i
] & 0x40) || (b
[i
] == 0x80))) {
204 /* the PS/2 initialization isn't unique
205 * enough to check the stream for
210 LOGPRINTF(1,"byte %d: %02x",i
,b
[i
]);
213 gettimeofday(&end
,NULL
);
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
);
225 stat
= ((b
[0] & 0x20) >> 3) |
226 ((b
[0] & 0x10) >> 4);
227 dx
= (char)(((b
[0] & 0x03) << 6) |
229 dy
= -((char)(((b
[0] & 0x0C) << 4) |
232 if((char)b
[2] == 0x7f) {
233 if ((char)b
[0] != 0x08) {
234 LOGPRINTF(1,"Bad data");
237 code
= (ir_code
)b
[1];
238 LOGPRINTF(1,"result %llx", (unsigned long long) code
);
239 m
=decode_all(remotes
);
242 stat
= ((b
[0] & 0x01) << 2) |
243 ((b
[0] & 0x06) >> 1);
256 if(dy
== 1 || dy
== 2 || dy
== 8) {
258 if (dy
== 2 && dx
!= 1) {
260 } else if (dy
== 8) {
264 if(dx
== 1 || dx
== 2 || dx
== 8) {
266 if (dx
== 2 && dy
!= 1) {
268 } else if (dx
== 8) {
272 if (dy
== 4 || dy
== 16) {
274 } else if (dx
== 4 || dx
== 16) {
279 LOGPRINTF(1,"result %llx", (unsigned long long) code
);
280 m
=decode_all(remotes
);
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
);
288 LOGPRINTF(1,"fallthrough is bad!%d %d %d",dx
, dy
, stat
);