1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <?xml-stylesheet type="text/xsl" href="helma.xsl"?>
3 <xmlroot xmlns:hop="http://www.helma.org/docs/guide/features/database">
4 <hopobject id="2969" name="2011-02-28-using-perl-to-read-linux-events" prototype="Page" created="1375203154189" lastModified="1375204101791">
5 <hop:parent idref="2912" prototyperef="Page"/>
6 <is_xhtml type="boolean">true</is_xhtml>
7 <http_remotehost>127.0.0.1</http_remotehost>
8 <http_language>en-US,en;q=0.8</http_language>
9 <uri>2011-02-28-using-perl-to-read-linux-events</uri>
10 <http_browser>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36</http_browser>
11 <time type="date">30.07.2013 12:08:21 CDT</time>
12 <hopsession>127.0.0.192.94.94.1agh54kvhedkc</hopsession>
13 <body><script>
\r
14 setTimeout(function(){$(".line").attr('class', 'gist_line');}, 3000);
\r
16 <h3 class='post-title entry-title' itemprop='name'>
\r
17 Using Perl to read Linux events
\r
19 <div class='post-header'>
\r
20 <div class='post-header-line-1'></div>
\r
22 <div class='post-body entry-content' id='post-body-1640951945066395271' itemprop='description articleBody'>
\r
23 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.<br /><br />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().<br /><br />I've uploaded <a href="https://gist.github.com/848252">a gist of my example play_movie.pl</a> script:<br /><br />
\r
25 <script src="https://gist.github.com/jadonk/848252.js"></script>
\r
26 <noscript><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 timeout\n");<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%08x\n", $time1+$time2/1000000, $type, $code, $value);<br /> if($code == 276 && $value == 1) # USER button pressed<br /> {<br /> printf("Restarting due to USER button press\n");<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></noscript>
\r
27 <div style='clear: both;'></div>
\r
29 <pseudoparent idref="2912" prototyperef="Page"/>
30 <http_referer>http://beagleboard.org/blog/2011-02-28-using-perl-to-read-linux-events/edit</http_referer>
31 <http_host>beagleboard.org</http_host>
32 <user>blog.hangerhead.com</user>