File Coverage

blib/lib/Linux/Input.pm
Criterion Covered Total %
statement 18 43 41.8
branch 0 6 0.0
condition 0 3 0.0
subroutine 6 10 60.0
pod 4 4 100.0
total 28 66 42.4


line stmt bran cond sub pod time code
1             package Linux::Input;
2              
3             $VERSION = '1.03';
4              
5 1     1   37578 use base 'Class::Data::Inheritable';
  1         3  
  1         959  
6 1     1   426 use strict;
  1         2  
  1         30  
7 1     1   6 use warnings;
  1         5  
  1         32  
8              
9 1     1   5 use Config;
  1         2  
  1         36  
10 1     1   888 use IO::File;
  1         12869  
  1         148  
11 1     1   1061 use IO::Select;
  1         2042  
  1         604  
12              
13             # class data
14             Linux::Input->mk_classdata('event_bytes');
15             Linux::Input->event_bytes(
16             ($Config{longsize} * 2) + # input_event.time (struct timeval)
17             ($Config{i16size} * 2) + # input_event.type, input_event.code
18             ($Config{i32size}) # input_event.value
19             );
20             Linux::Input->mk_classdata('timeout');
21             Linux::Input->timeout(0.01);
22              
23             # instaniate a new input device
24             sub new {
25 0     0 1   my $class = shift;
26 0           my $filename = shift;
27 0           my $self = { };
28 0           bless ($self => $class);
29              
30 0           $self->{fh} = IO::File->new("< $filename");
31 0 0         die($!) unless ($self->{fh});
32              
33 0           return $self;
34             }
35              
36             # get filehandle of device
37             sub fh {
38 0     0 1   my $self = shift;
39 0           return $self->{fh};
40             }
41              
42             # $self's IO::Select object
43             sub selector {
44 0     0 1   my $self = shift;
45 0 0         unless ($self->{__io_select}) {
46 0           $self->{__io_select} = IO::Select->new($self->fh());
47             }
48 0           return $self->{__io_select};
49             }
50              
51             # poll for all pending events
52             sub poll {
53 0     0 1   my $self = shift;
54 0   0       my $timeout = shift || ref($self)->timeout();
55 0           my $selector = $self->selector();
56 0           my @ev;
57              
58 0           my $struct_len = Linux::Input->event_bytes();
59 0           while (my ($fh) = $selector->can_read($timeout)) {
60 0           my $buffer;
61 0           my $len = sysread($fh, $buffer, $struct_len);
62 0           my ($sec, $usec, $type, $code, $value) =
63             unpack('L!L!S!S!i!', $buffer);
64 0           my $event = {
65             tv_sec => $sec,
66             tv_usec => $usec,
67             type => $type,
68             code => $code,
69             value => $value,
70             };
71 0 0         push @ev, $event if (defined($type));
72             }
73 0           return @ev;
74             }
75              
76             1;
77              
78             __END__