#!/usr/bin/perl use warnings; use strict; use POSIX; use Time::HiRes qw( ualarm ); ### Detect if the mouse is moving or stopped. Tested on Raspian Linux. ### Adapted from http://www.the-ownage.com/?p=835 ### The guy soldered a pair of wires to a mouse button ### and used it to detect water leaks and send himself notifications. my $fd; my $buf; my $mousedev="/dev/input/mouse0"; $fd = POSIX::open($mousedev, &POSIX::O_RDONLY) or die ("Cannot open $mousedev: $!"); my $stopped = 1; print time,"--Mouse is stopped--\n"; while( 1 ) { local $SIG{ALRM} = sub { print time,"--Mouse is stopped--\n" if ! $stopped; #on transition $stopped = 1; ### turn on output bit on Raspberry Pi $buf =""; }; ualarm 200_000; ## time out after 0.2 seconds POSIX::read($fd, $buf, 1); if ( $buf and $stopped ) { print time,"--Mouse is moving--\n"; $stopped = 0; ### turn off output } ualarm 0; }