PSYC/Event/Glib.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 151 CC 12 SCAR 31.3
CC
12
Cov
0%
add
56.0
remove
20.0
revoke
12.0
CRAP 22.9
1 package Net::PSYC::Event::Glib;
2  
3 our $VERSION = '0.1';
4  
5 use strict;
6  
7 use Net::PSYC qw(W);
8 use Glib;
9  
10 use base qw(Exporter);
11 our @EXPORT_OK = qw(init can_read can_write has_exception add remove start_loop stop_loop revoke);
12  
13 my (%r, %w, %e, %revoke, @t, $loop);
14  
15 sub can_read {
16 croak('can_read() is not yet implemented by Net::PSYC::Event::Glib');
17 }
18  
19 sub can_write {
20 croak('can_write() is not yet implemented by Net::PSYC::Event::Glib');
21 }
22  
23 sub has_exception {
24 croak('has_exception() is not yet implemented by Net::PSYC::Event::Glib');
25 }
26  
27 # add (\*fd, flags, cb[, repeat])
28 sub add {
29 my ($fd, $flags, $cb, $repeat) = @_;
30 W2('Glib->add(%s, %s, %s)', $fd, $flags, $cb);
31 W0('add () using Glib requires a callback! (has to be a code-ref)')
32 if (!$cb || ref $cb ne 'CODE' );
33
34 # one-shot event!
35 if (defined($repeat) && $repeat == 0) {
36 my $t = $cb;
37 $cb = sub {
38 remove($fd, $flags) if ($t->() != -1);
39 };
40 $revoke{fileno($fd)} = [ $fd, $flags, $cb ];
41 }
42  
43 if ($flags =~ /r/) {
44 $r{fileno($fd)} = Glib::IO->add_watch(fileno($fd), 'in', $cb);
45 }
46 if ($flags =~ /w/) {
47 $w{fileno($fd)} = Glib::IO->add_watch(fileno($fd), 'out', $cb);
48 }
49 if ($flags =~ /e/) {
50 $e{fileno($fd)} = Glib::IO->add_watch(fileno($fd), 'err', $cb);
51 }
52 }
53  
54 # revoke ( \*fd )
55 sub revoke {
56 my $name = fileno(shift);
57 W2('Glib->revoke(%s)', $name);
58 if (exists $revoke{$name}) {
59 my $flags = $revoke{$name}->[1];
60 return if ((!$flags =~ /r/ || exists $r{$name}) && (!$flags =~ /w/ || exists $w{$name}));
61 add(@{$revoke{$name}});
62 }
63 }
64  
65 # remove (\*fd[, flags] )
66 sub remove {
67 my ($name, $flags) = (fileno(shift), shift);
68 W2('gtk2->remove(%s, %s)', $name, $flags);
69
70 if ((!$flags || $flags =~ /r/) && exists $r{$name} ) {
71 Glib::IO->remove_watch( delete $r{$name} );
72 }
73 if ((!$flags || $flags =~ /w/) && exists $w{$name}) {
74 Glib::IO->remove_watch( delete $w{$name} );
75 }
76 if ((!$flags || $flags =~ /e/) && exists $w{$name}) {
77 Glib::IO->remove_watch( delete $e{$name} );
78 }
79 }
80  
81 sub start_loop {
82 die 'Net::PSYC::Event::Glib does not offer an event-loop.';
83 }
84  
85 sub stop_loop {
86 start_loop();
87 }
88  
89 1;