Advertisement
cheako

Multitouch Visualizer

Mar 4th, 2012
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 8.62 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. #
  4. # Multitouch Visualizer
  5. #
  6. #   Copyright (C) 2012 Mike Mestnik
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21.  
  22. use strict;
  23. use warnings;
  24. use Carp;
  25. use Math::Trig ':pi';
  26. use File::Spec;
  27. use Time::HiRes qw(tv_interval);
  28. use Linux::Input::Joystick;
  29.  
  30. use Data::Dumper;
  31. use Statistics::Descriptive;
  32.  
  33. use AnyEvent;
  34. use AnyEvent::Handle;
  35. use AnyEvent::AggressiveIdle qw(aggressive_idle);
  36.  
  37. use OpenGL;
  38.  
  39. my $dev =
  40.   Linux::Input->new(
  41.     File::Spec->catfile( qw%/ dev input%, 'event' . ( $ARGV[0] || 14 ) ) )
  42.   or die @_;
  43.  
  44. glpOpenWindow;
  45. my ( $appxa, $appxb, $appya, $appyb ) = ( -1104, 1360, 1600, -2048 );
  46. glOrtho( $appxa, $appxb, $appya, $appyb, -1, 1 );
  47. glClearColor( 0, 0, 0, 1 );
  48. glClear(GL_COLOR_BUFFER_BIT);
  49. glEnable(GL_BLEND);
  50. glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  51.  
  52. print "Press Ctrl-C to exit.\n";
  53.  
  54. my %stat = (
  55.     tv_sec  => Statistics::Descriptive::Sparse->new(),
  56.     tv_usec => Statistics::Descriptive::Sparse->new(),
  57.     x       => Statistics::Descriptive::Sparse->new(),
  58.     y       => Statistics::Descriptive::Sparse->new(),
  59.     size    => Statistics::Descriptive::Sparse->new(),
  60.     hard    => Statistics::Descriptive::Sparse->new(),
  61.     other   => Statistics::Descriptive::Sparse->new(),
  62. );
  63. my @objects;
  64. my $fiveseven = -1;
  65. my $cv        = AnyEvent->condvar;
  66. my $hdl;
  67. $hdl = AnyEvent::Handle->new(
  68.     fh       => $dev->fh,
  69.     on_error => sub {
  70.         my ( $hdl, $fatal, $msg ) = @_;
  71.         $hdl->destroy;
  72.     },
  73.     on_read => sub {
  74.         my $input_device = $dev;
  75.         my @event        = $input_device->poll(0.01);
  76.         for ( my $iev = 0 ; $iev < $#event - 1 ; $iev++ ) {
  77.             my $ev = $event[$iev];
  78.             next
  79.               if ( ( $ev->{type} == 0 && $ev->{code} == 0 && $ev->{value} == 0 )
  80.                 || $ev->{type} == 2 );
  81.             if (
  82.                   $ev->{type} == 0 && defined $event[ 1 + $iev ]
  83.                 ? $event[ 1 + $iev ]->{type} == 0
  84.                 : 0
  85.               )
  86.             {
  87.                 if (
  88.                     defined $event[ 2 + $iev ]
  89.                     ? $event[ 2 + $iev ]->{type} == 0
  90.                     && defined $event[ 3 + $iev ]
  91.                         ? $event[ 3 + $iev ]->{type} == 0
  92.                         : 0
  93.                     : 0
  94.                   )
  95.                 {
  96.                     $stat{tv_sec}->add_data( $ev->{tv_sec} );
  97.                     $stat{tv_usec}->add_data( $ev->{tv_usec} );
  98.                     print $stat{tv_sec}->count() . " Super End\n";
  99.                     $iev += 3;
  100.                     next;
  101.                 }
  102.                 else {
  103.                     $stat{tv_sec}->add_data( $ev->{tv_sec} );
  104.                     $stat{tv_usec}->add_data( $ev->{tv_usec} );
  105.                     print $stat{tv_sec}->count() . " End\n";
  106.                     $iev++;
  107.                     next;
  108.                 }
  109.             }
  110.             if (
  111.                    $ev->{type} == 3
  112.                 && $ev->{code} == 57
  113.                 && (
  114.                     defined $event[ 1 + $iev ] ? $event[ 1 + $iev ]->{type} == 3
  115.                     && $event[ 1 + $iev ]->{code} == 48 : 0 )
  116.                 && (
  117.                     defined $event[ 2 + $iev ] ? $event[ 2 + $iev ]->{type} == 3
  118.                     && $event[ 2 + $iev ]->{code} == 49 : 0 )
  119.               )
  120.             {
  121.                 if (
  122.                     (
  123.                         defined $event[ 3 + $iev ]
  124.                         ? $event[ 3 + $iev ]->{type} == 3
  125.                         && $event[ 3 + $iev ]->{code} == 52
  126.                         : 0
  127.                     )
  128.                     && (
  129.                         defined $event[ 4 + $iev ]
  130.                         ? $event[ 4 + $iev ]->{type} == 3
  131.                         && $event[ 4 + $iev ]->{code} == 53
  132.                         : 0
  133.                     )
  134.                     && (
  135.                         defined $event[ 5 + $iev ]
  136.                         ? $event[ 5 + $iev ]->{type} == 3
  137.                         && $event[ 5 + $iev ]->{code} == 54
  138.                         : 0
  139.                     )
  140.                   )
  141.                 {
  142.                     $stat{tv_sec}->add_data( $ev->{tv_sec} );
  143.                     $stat{tv_usec}->add_data( $ev->{tv_usec} );
  144.                     $stat{x}->add_data( $event[ 4 + $iev ]->{value} );
  145.                     $stat{y}->add_data( $event[ 5 + $iev ]->{value} );
  146.                     $stat{size}->add_data( $event[ 1 + $iev ]->{value} );
  147.                     $stat{hard}->add_data( $event[ 2 + $iev ]->{value} );
  148.                     $stat{other}->add_data( $event[ 3 + $iev ]->{value} );
  149.                     print Dumper [ $ev, %stat ];
  150.  
  151.              # 3824, 1330820887.862841 , type => Big Packet, new => 0,
  152.              # Area => 0/1008~404.807324840764, Hard => 0/1004~350.656050955414,
  153.              # 52 => -31/32~-0.78781847133758, X => -1100/1358~3.77945859872611,
  154.              # Y => -1593/2047~-2.87300955414013,
  155.                     my $size = $event[ 1 + $iev ]->{value} / 32;
  156.                     my ( $x, $y ) = (
  157.                         $event[ 4 + $iev ]->{value},
  158.                         $event[ 5 + $iev ]->{value}
  159.                     );
  160.  
  161.                     push(
  162.                         @objects,
  163.                         {
  164.                             tv_time => [ $ev->{tv_sec}, $ev->{tv_usec} ],
  165.                             new   => $ev->{value} != $fiveseven,
  166.                             force => $event[ 2 + $iev ]->{value} / 1024,
  167.                             x     => $x,
  168.                             y     => $y,
  169.                             size  => $size
  170.                         }
  171.                     );
  172.  
  173.                     $fiveseven = $ev->{value};
  174.                     $iev += 5;
  175.                     next;
  176.                 }
  177.                 else {
  178.                     $stat{tv_sec}->add_data( $ev->{tv_sec} );
  179.                     $stat{tv_usec}->add_data( $ev->{tv_usec} );
  180.                     $stat{size}->add_data( $event[ 1 + $iev ]->{value} );
  181.                     $stat{hard}->add_data( $event[ 2 + $iev ]->{value} );
  182.                     print Dumper [ $ev, %stat ];
  183.                     $fiveseven = $ev->{value};
  184.                     $iev += 2;
  185.                     next;
  186.                 }
  187.             }
  188.             print Dumper [$ev];
  189.         }
  190.     }
  191. );
  192.  
  193. aggressive_idle(
  194.     sub {
  195.         my $ctr = 0;
  196.         foreach my $obj (@objects) {
  197.             my $fade = tv_interval( $obj->{tv_time} );
  198.             $ctr++ if $fade > 2;
  199.             eval { local $SIG{'__DIE__'}; $fade = 1 - sqrt($fade); };
  200.             $fade = 0 if $@;
  201.             glColor4f( 1, $obj->{new}, $obj->{force}, $fade );
  202.  
  203.             DrawCircle( $obj->{x}, $obj->{y}, $obj->{size} );
  204.         }
  205.         my $err = glGetError();
  206.         die $err if $err;
  207.         glpFlush();
  208.         glClear(GL_COLOR_BUFFER_BIT);
  209.  
  210.         if ( $ctr > 1 ) {
  211.             foreach my $ii ( 1 .. $ctr ) {
  212.                 shift @objects;
  213.             }
  214.         }
  215.     }
  216. );
  217.  
  218. $cv->recv;
  219.  
  220. sub DrawCircle {
  221.     my ( $cx, $cy, $r ) = @_;
  222.     my $num_segments = 1 + 10 * sqrt($r);
  223.     my $theta        = pi2 / $num_segments;
  224.     my $tangetial_factor =
  225.       Math::Trig::tan($theta);    # calculate the tangential factor
  226.  
  227.     my $radial_factor = cos($theta);    # calculate the radial factor
  228.  
  229.     my ( $x, $y ) = ( $r, 0 );          # we start at angle = 0
  230.  
  231.     glBegin(GL_LINE_LOOP);
  232.     foreach my $ii ( 0 .. $num_segments ) {
  233.         glVertex2f( $x + $cx, $y + $cy );    # output vertex
  234.  
  235.  # calculate the tangential vector
  236.  # remember, the radial vector is (x, y)
  237.  # to get the tangential vector we flip those coordinates and negate one of them
  238.  
  239.         my $tx = -$y;
  240.         my $ty = $x;
  241.  
  242.         # add the tangential vector
  243.  
  244.         $x += $tx * $tangetial_factor;
  245.         $y += $ty * $tangetial_factor;
  246.  
  247.         # correct using the radial factor
  248.  
  249.         $x *= $radial_factor;
  250.         $y *= $radial_factor;
  251.     }
  252.     glEnd();
  253. }
  254. exit 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement