File Coverage

blib/lib/Speech/Speakup.pm
Criterion Covered Total %
statement 13 99 13.1
branch 2 42 4.7
condition 0 6 0.0
subroutine 5 17 29.4
pod 4 12 33.3
total 24 176 13.6


line stmt bran cond sub pod time code
1             # Speakup::Speakup.pm
2             #########################################################################
3             # This Perl module is Copyright (c) 2002, Peter J Billam #
4             # c/o P J B Computing, www.pjb.com.au #
5             # #
6             # This module is free software; you can redistribute it and/or #
7             # modify it under the same terms as Perl itself. #
8             #########################################################################
9              
10             package Speech::Speakup;
11             $VERSION = '1.04'; #
12             my $stupid_bloody_warning = $VERSION; # circumvent -w warning
13             require Exporter;
14             @ISA = qw(Exporter);
15             @EXPORT = qw();
16             @EXPORT_OK = qw(speakup_get speakup_set synth_get synth_set);
17             %EXPORT_TAGS = (ALL => [@EXPORT,@EXPORT_OK]);
18              
19 1     1   18252 no strict; no warnings;
  1     1   3  
  1         36  
  1         5  
  1         2  
  1         88  
20              
21             $Speech::Speakup::Message = undef;
22             $Speech::Speakup::SpDir = undef;
23             foreach ('/sys/accessibility/speakup','/proc/speakup') {
24             if (-d $_) { $SpDir = $_; }
25             }
26             # if (!$SpDir) { die ; } # results in 100% failure for CPAN-testers :-(
27             # eval 'require Speech::Speakup';
28             # sets $@ to "Can't locate ..." if Speech::Speakup is not installed
29             # sets $@ to "Compilation failed ..." if $SpDir is not present
30              
31 1     1   934 use open ':locale'; # the open pragma was introduced in 5.8.6
  1         1843  
  1         6  
32              
33 0     0 1   sub speakup_get { get($SpDir, @_); }
34 0     0 1   sub speakup_set { set($SpDir, @_); }
35 0     0 1   sub synth_get { get(synthdir(),@_); }
36 0     0 1   sub synth_set { set(synthdir(),@_); }
37              
38 0     0 0   sub set { my ($dir, $param, $value) = @_;
39 0 0         if (! $param) { # return a list of all settable params
40 0 0         if (! opendir(D,$dir)) {
41 0           $Message = "can't opendir $dir: ";
42 0           return undef;
43             }
44 0 0 0       my @l = sort grep
45 0           { (!/^\./) && (-f "$dir/$_") && is_w("$dir/$_") } readdir(D);
46 0           closedir D;
47 0           $Message = undef;
48 0           return @l;
49             }
50 0 0         if (! open(F, '>', "$dir/$param")) {
51 0           $Message = "can't open $dir/$param: $!";
52 0           return undef;
53             } else {
54 0           print F "$value\n"; close F;
  0            
55 0           $Message = undef;
56 0           return 1;
57             }
58             }
59              
60 0     0 0   sub get { my ($dir, $param) = @_;
61 0 0         if (! $param) { # return a list of all gettable params
62 0 0         if (! opendir(D,$dir)) {
63 0           $Message = "can't opendir $dir: ";
64 0           return undef;
65             }
66 0 0 0       my @l = sort grep
67 0           { (!/^\./) && (-f "$dir/$_") && is_r("$dir/$_") } readdir(D);
68 0           closedir D;
69 0           $Message = undef;
70 0           return @l;
71             }
72 0 0         if (! open(F, '<', "$dir/$param")) {
73 0           $Message = "can't open $dir/$param: $!";
74 0           return undef;
75             } else {
76 0           my @lines = (); close F;
  0            
77 0           $Message = undef;
78             # 1.03 keymap is 65 lines long ! the others are only one line.
79 0 0         if (1 < @lines) { return join('', @lines);
  0            
80 1     1   25226 } else { my $value = $lines[$[]; $value =~ s/\s+$//; return $value;
  1         412  
  1         909  
  0            
  0            
  0            
81             }
82             }
83             }
84              
85             sub synthdir {
86 0     0 0   my $sd = get($SpDir,'synth');
87 0 0         if (! $sd) { warn "can't find the synth directory\n"; return ''; }
  0            
  0            
88 0           my $d = "$SpDir/$sd";
89 0 0         if (! -e $d) { warn "synth directory $d does not exist\n"; return ''; }
  0            
  0            
90 0 0         if (! -d $d) { warn "synth directory $d is not a directory\n"; return ''; }
  0            
  0            
91 0           return $d;
92             }
93              
94             sub is_w {
95             # because -w as root reports yes regardless of file permissions
96 0     0 0   my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
97             $atime,$mtime,$ctime,$blksize,$blocks) = stat($_[$[]);
98 0           return $mode & 2;
99             }
100             sub is_r {
101 0     0 0   my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
102             $atime,$mtime,$ctime,$blksize,$blocks) = stat($_[$[]);
103 0           return $mode & 4;
104             }
105              
106             sub enter_speakup_silent { # 1.62
107             # echo 7 > /sys/accessibility/speakup/silent if it exists
108 0 0   0 0   if (!$SpeakUpSilentFile) { return 0; }
  0            
109 0 0         if ($IsSpeakUpSilent) {
110 0           warn "enter_speakup_silent but already IsSpeakUpSilent\r\n"; return 1 ;
  0            
111             }
112 0 0         if (open(S, '>', $SpeakUpSilentFile)) { print S "7\n"; close S; }
  0            
  0            
113 0           $IsSpeakUpSilent = 1;
114 0           return 1;
115             }
116             sub leave_speakup_silent { # 1.62
117             # echo 4 > /sys/accessibility/speakup/silent if it exists
118 0 0   0 0   if (!$SpeakUpSilentFile) { return 0; }
  0            
119 0 0         if (!$IsSpeakUpSilent) {
120 0           warn "leave_speakup_silent but not IsSpeakUpSilent\r\n"; return 1 ;
  0            
121             }
122 0 0         if (open(S, '>', $SpeakUpSilentFile)) { print S "4\n"; close S; }
  0            
  0            
123 0           $IsSpeakUpSilent = 0;
124 0           return 1;
125             }
126              
127             sub which {
128 0     0 0   my $f;
129 0 0         foreach $d (split(":",$ENV{'PATH'})) {$f="$d/$_[$[]"; return $f if -x $f;}
  0            
  0            
130             }
131             %SpeakMode = ();
132             sub END {
133 1 50   1   4935 if ($Eflite_FH) { print $Eflite_FH "s\nq { }\n"; close $Eflite_FH;
  0 50          
  0            
134 0           } elsif ($Espeak_PID) { kill SIGHUP, $Espeak_PID; wait;
  0            
135             }
136             }
137              
138             1;
139              
140             __END__