Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- #
- # Multitouch Visualizer
- #
- # Copyright (C) 2012 Mike Mestnik
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License along
- # with this program; if not, write to the Free Software Foundation, Inc.,
- # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- use strict;
- use warnings;
- use Carp;
- use Math::Trig ':pi';
- use File::Spec;
- use Time::HiRes qw(tv_interval);
- use Linux::Input::Joystick;
- use Data::Dumper;
- use Statistics::Descriptive;
- use AnyEvent;
- use AnyEvent::Handle;
- use AnyEvent::AggressiveIdle qw(aggressive_idle);
- use OpenGL;
- my $dev =
- Linux::Input->new(
- File::Spec->catfile( qw%/ dev input%, 'event' . ( $ARGV[0] || 14 ) ) )
- or die @_;
- glpOpenWindow;
- my ( $appxa, $appxb, $appya, $appyb ) = ( -1104, 1360, 1600, -2048 );
- glOrtho( $appxa, $appxb, $appya, $appyb, -1, 1 );
- glClearColor( 0, 0, 0, 1 );
- glClear(GL_COLOR_BUFFER_BIT);
- glEnable(GL_BLEND);
- glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
- print "Press Ctrl-C to exit.\n";
- my %stat = (
- tv_sec => Statistics::Descriptive::Sparse->new(),
- tv_usec => Statistics::Descriptive::Sparse->new(),
- x => Statistics::Descriptive::Sparse->new(),
- y => Statistics::Descriptive::Sparse->new(),
- size => Statistics::Descriptive::Sparse->new(),
- hard => Statistics::Descriptive::Sparse->new(),
- other => Statistics::Descriptive::Sparse->new(),
- );
- my @objects;
- my $fiveseven = -1;
- my $cv = AnyEvent->condvar;
- my $hdl;
- $hdl = AnyEvent::Handle->new(
- fh => $dev->fh,
- on_error => sub {
- my ( $hdl, $fatal, $msg ) = @_;
- $hdl->destroy;
- },
- on_read => sub {
- my $input_device = $dev;
- my @event = $input_device->poll(0.01);
- for ( my $iev = 0 ; $iev < $#event - 1 ; $iev++ ) {
- my $ev = $event[$iev];
- next
- if ( ( $ev->{type} == 0 && $ev->{code} == 0 && $ev->{value} == 0 )
- || $ev->{type} == 2 );
- if (
- $ev->{type} == 0 && defined $event[ 1 + $iev ]
- ? $event[ 1 + $iev ]->{type} == 0
- : 0
- )
- {
- if (
- defined $event[ 2 + $iev ]
- ? $event[ 2 + $iev ]->{type} == 0
- && defined $event[ 3 + $iev ]
- ? $event[ 3 + $iev ]->{type} == 0
- : 0
- : 0
- )
- {
- $stat{tv_sec}->add_data( $ev->{tv_sec} );
- $stat{tv_usec}->add_data( $ev->{tv_usec} );
- print $stat{tv_sec}->count() . " Super End\n";
- $iev += 3;
- next;
- }
- else {
- $stat{tv_sec}->add_data( $ev->{tv_sec} );
- $stat{tv_usec}->add_data( $ev->{tv_usec} );
- print $stat{tv_sec}->count() . " End\n";
- $iev++;
- next;
- }
- }
- if (
- $ev->{type} == 3
- && $ev->{code} == 57
- && (
- defined $event[ 1 + $iev ] ? $event[ 1 + $iev ]->{type} == 3
- && $event[ 1 + $iev ]->{code} == 48 : 0 )
- && (
- defined $event[ 2 + $iev ] ? $event[ 2 + $iev ]->{type} == 3
- && $event[ 2 + $iev ]->{code} == 49 : 0 )
- )
- {
- if (
- (
- defined $event[ 3 + $iev ]
- ? $event[ 3 + $iev ]->{type} == 3
- && $event[ 3 + $iev ]->{code} == 52
- : 0
- )
- && (
- defined $event[ 4 + $iev ]
- ? $event[ 4 + $iev ]->{type} == 3
- && $event[ 4 + $iev ]->{code} == 53
- : 0
- )
- && (
- defined $event[ 5 + $iev ]
- ? $event[ 5 + $iev ]->{type} == 3
- && $event[ 5 + $iev ]->{code} == 54
- : 0
- )
- )
- {
- $stat{tv_sec}->add_data( $ev->{tv_sec} );
- $stat{tv_usec}->add_data( $ev->{tv_usec} );
- $stat{x}->add_data( $event[ 4 + $iev ]->{value} );
- $stat{y}->add_data( $event[ 5 + $iev ]->{value} );
- $stat{size}->add_data( $event[ 1 + $iev ]->{value} );
- $stat{hard}->add_data( $event[ 2 + $iev ]->{value} );
- $stat{other}->add_data( $event[ 3 + $iev ]->{value} );
- print Dumper [ $ev, %stat ];
- # 3824, 1330820887.862841 , type => Big Packet, new => 0,
- # Area => 0/1008~404.807324840764, Hard => 0/1004~350.656050955414,
- # 52 => -31/32~-0.78781847133758, X => -1100/1358~3.77945859872611,
- # Y => -1593/2047~-2.87300955414013,
- my $size = $event[ 1 + $iev ]->{value} / 32;
- my ( $x, $y ) = (
- $event[ 4 + $iev ]->{value},
- $event[ 5 + $iev ]->{value}
- );
- push(
- @objects,
- {
- tv_time => [ $ev->{tv_sec}, $ev->{tv_usec} ],
- new => $ev->{value} != $fiveseven,
- force => $event[ 2 + $iev ]->{value} / 1024,
- x => $x,
- y => $y,
- size => $size
- }
- );
- $fiveseven = $ev->{value};
- $iev += 5;
- next;
- }
- else {
- $stat{tv_sec}->add_data( $ev->{tv_sec} );
- $stat{tv_usec}->add_data( $ev->{tv_usec} );
- $stat{size}->add_data( $event[ 1 + $iev ]->{value} );
- $stat{hard}->add_data( $event[ 2 + $iev ]->{value} );
- print Dumper [ $ev, %stat ];
- $fiveseven = $ev->{value};
- $iev += 2;
- next;
- }
- }
- print Dumper [$ev];
- }
- }
- );
- aggressive_idle(
- sub {
- my $ctr = 0;
- foreach my $obj (@objects) {
- my $fade = tv_interval( $obj->{tv_time} );
- $ctr++ if $fade > 2;
- eval { local $SIG{'__DIE__'}; $fade = 1 - sqrt($fade); };
- $fade = 0 if $@;
- glColor4f( 1, $obj->{new}, $obj->{force}, $fade );
- DrawCircle( $obj->{x}, $obj->{y}, $obj->{size} );
- }
- my $err = glGetError();
- die $err if $err;
- glpFlush();
- glClear(GL_COLOR_BUFFER_BIT);
- if ( $ctr > 1 ) {
- foreach my $ii ( 1 .. $ctr ) {
- shift @objects;
- }
- }
- }
- );
- $cv->recv;
- sub DrawCircle {
- my ( $cx, $cy, $r ) = @_;
- my $num_segments = 1 + 10 * sqrt($r);
- my $theta = pi2 / $num_segments;
- my $tangetial_factor =
- Math::Trig::tan($theta); # calculate the tangential factor
- my $radial_factor = cos($theta); # calculate the radial factor
- my ( $x, $y ) = ( $r, 0 ); # we start at angle = 0
- glBegin(GL_LINE_LOOP);
- foreach my $ii ( 0 .. $num_segments ) {
- glVertex2f( $x + $cx, $y + $cy ); # output vertex
- # calculate the tangential vector
- # remember, the radial vector is (x, y)
- # to get the tangential vector we flip those coordinates and negate one of them
- my $tx = -$y;
- my $ty = $x;
- # add the tangential vector
- $x += $tx * $tangetial_factor;
- $y += $ty * $tangetial_factor;
- # correct using the radial factor
- $x *= $radial_factor;
- $y *= $radial_factor;
- }
- glEnd();
- }
- exit 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement