For a simple demo that played a movie on a BeagleBoard, I wanted to add a simple mechanism to start the movie over again if the USER button was pressed.
To read a Linux input event, it is as simple as just performing a read. The only trick to this for me, however, is that I wanted the read to timeout. This was resolved by using the Perl alarm() function can catching the signal within an eval().
I’ve uploaded a gist of my example play_movie.pl script:
</p>
<pre>#!/usr/bin/perl<br />$ENV{‘DISPLAY’} = “:0.0”;<br />system(“xhost +”);<br />#system(“totem –quit”);<br />#system(“nice -n -5 totem –fullscreen /home/root/playlist.xml &”);<br />$cmdline_start = “nice -n -5 mplayer /home/root/*.mov &”;<br />system($cmdline_start);<br /><br />open(FILE, “/dev/input/event0”);<br />binmode(FILE);<br />while(1)<br /> {<br /> eval<br /> {<br /> local $SIG{ALRM} = sub { die(“Alarm!n”) };<br /> alarm(60*28); # 28 minutes<br /> read(FILE, $buf, 16);<br /> alarm(0);<br /> };<br /> if($@)<br /> {<br /> printf(“Restarting due to timeoutn”);<br /> #system(“totem –next”);<br /> system(“killall -15 mplayer”);<br /> sleep(1);<br /> system(“killall -9 mplayer”);<br /> sleep(1);<br /> system($cmdline_start);<br /> }<br /> else<br /> {<br /> ($time1, $time2, $type, $code, $value) = unpack(“iissi”, $buf);<br /> printf(“%f %05d %05d 0x%08xn”, $time1+$time2/1000000, $type, $code, $value);<br /> if($code == 276 && $value == 1) # USER button pressed<br /> {<br /> printf(“Restarting due to USER button pressn”);<br /> #system(“totem –next”);<br /> system(“killall -15 mplayer”);<br /> sleep(1);<br /> system(“killall -9 mplayer”);<br /> sleep(1);<br /> system($cmdline_start);<br /> }<br /> }<br /> }</pre>
<p>