Web edits
[beagleboard.org.git] / db / 2969.xml
blob244b2ed1fae47ff9ceee0054fd05cf6b48c83471
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>&lt;script&gt;\r
14     setTimeout(function(){$(&quot;.line&quot;).attr(&apos;class&apos;, &apos;gist_line&apos;);}, 3000);\r
15 &lt;/script&gt;\r
16 &lt;h3 class=&apos;post-title entry-title&apos; itemprop=&apos;name&apos;&gt;\r
17 Using Perl to read Linux events\r
18 &lt;/h3&gt;\r
19 &lt;div class=&apos;post-header&apos;&gt;\r
20 &lt;div class=&apos;post-header-line-1&apos;&gt;&lt;/div&gt;\r
21 &lt;/div&gt;\r
22 &lt;div class=&apos;post-body entry-content&apos; id=&apos;post-body-1640951945066395271&apos; itemprop=&apos;description articleBody&apos;&gt;\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.&lt;br /&gt;&lt;br /&gt;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().&lt;br /&gt;&lt;br /&gt;I&apos;ve uploaded &lt;a href=&quot;https://gist.github.com/848252&quot;&gt;a gist of my example play_movie.pl&lt;/a&gt; script:&lt;br /&gt;&lt;br /&gt;\r
24 &lt;br /&gt;\r
25 &lt;script src=&quot;https://gist.github.com/jadonk/848252.js&quot;&gt;&lt;/script&gt;\r
26 &lt;noscript&gt;&lt;pre&gt;#!/usr/bin/perl&lt;br /&gt;$ENV{&apos;DISPLAY&apos;} = &quot;:0.0&quot;;&lt;br /&gt;system(&quot;xhost +&quot;);&lt;br /&gt;#system(&quot;totem --quit&quot;);&lt;br /&gt;#system(&quot;nice -n -5 totem --fullscreen /home/root/playlist.xml &amp;&quot;);&lt;br /&gt;$cmdline_start = &quot;nice -n -5 mplayer /home/root/*.mov &amp;&quot;;&lt;br /&gt;system($cmdline_start);&lt;br /&gt;&lt;br /&gt;open(FILE, &quot;/dev/input/event0&quot;);&lt;br /&gt;binmode(FILE);&lt;br /&gt;while(1)&lt;br /&gt; {&lt;br /&gt;  eval&lt;br /&gt;   {&lt;br /&gt;    local $SIG{ALRM} = sub { die(&quot;Alarm!\n&quot;) };&lt;br /&gt;    alarm(60*28); # 28 minutes&lt;br /&gt;    read(FILE, $buf, 16);&lt;br /&gt;    alarm(0);&lt;br /&gt;   };&lt;br /&gt;  if($@)&lt;br /&gt;   {&lt;br /&gt;    printf(&quot;Restarting due to timeout\n&quot;);&lt;br /&gt;    #system(&quot;totem --next&quot;);&lt;br /&gt;    system(&quot;killall -15 mplayer&quot;);&lt;br /&gt;    sleep(1);&lt;br /&gt;    system(&quot;killall -9 mplayer&quot;);&lt;br /&gt;    sleep(1);&lt;br /&gt;    system($cmdline_start);&lt;br /&gt;   }&lt;br /&gt;  else&lt;br /&gt;   {&lt;br /&gt;    ($time1, $time2, $type, $code, $value) = unpack(&quot;iissi&quot;, $buf);&lt;br /&gt;    printf(&quot;%f %05d %05d 0x%08x\n&quot;, $time1+$time2/1000000, $type, $code, $value);&lt;br /&gt;    if($code == 276 &amp;&amp; $value == 1) # USER button pressed&lt;br /&gt;     {&lt;br /&gt;      printf(&quot;Restarting due to USER button press\n&quot;);&lt;br /&gt;      #system(&quot;totem --next&quot;);&lt;br /&gt;      system(&quot;killall -15 mplayer&quot;);&lt;br /&gt;      sleep(1);&lt;br /&gt;      system(&quot;killall -9 mplayer&quot;);&lt;br /&gt;      sleep(1);&lt;br /&gt;      system($cmdline_start);&lt;br /&gt;     }&lt;br /&gt;   }&lt;br /&gt; }&lt;/pre&gt;&lt;/noscript&gt;\r
27 &lt;div style=&apos;clear: both;&apos;&gt;&lt;/div&gt;\r
28 &lt;/div&gt;</body>
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>
33     <lang>en-us</lang>
34   </hopobject>
35 </xmlroot>