Pvoice/Input.pm untested

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 311 CC 38 SCAR 33.3
CC
38
Cov
0%
_monitorport
110.0
new
30.0
SetupMouse
30.0
CRAP 27.9
1 package AAC::Pvoice::Input;
2 use strict;
3 use warnings;
4
5 use Wx qw(:everything);
6 use Wx::Perl::Carp;
7 BEGIN
8 {
9 use Device::ParallelPort;
10 if ($^O eq 'MSWin32')
11 {
12 require Device::ParallelPort::drv::win32;
13 }
14 else
15 {
16 require Device::ParallelPort::drv::parport;
17 }
18 }
19 use Wx::Event qw( EVT_TIMER
20 EVT_CHAR
21 EVT_MOUSE_EVENTS);
22 our $VERSION = sprintf("%d.%02d", q$Revision: 1.12 $=~/(\d+)\.(\d+)/);
23
24 sub new
25 {
26 my $proto = shift;
27 my $class = ref($proto) || $proto;
28 my $self = {};
29 bless $self, $class;
30 $self->{window} = shift;
31
32 # We get the configuration from the Windows registry
33 # If it's not initialized, we provide some defaults
34 $self->{window}->{config} = Wx::ConfigBase::Get || croak "Can't get Config";
35
36 # Get the input-device
37 # icon = mouse left/right buttons
38 # adremo = electric wheelchair adremo
39 # keys = keystrokes
40 $self->{window}->{Device} = $self->{window}->{config}->Read('Device', 'icon');
41
42 $self->{window}->{Interval} = $self->{window}->{config}->ReadInt('Interval', 10);
43 $self->{window}->{Buttons} = $self->{window}->{config}->ReadInt('Buttons', 2);
44 $self->{window}->{OneButtonInterval} = $self->{window}->{config}->ReadInt('OneButtonInterval', 2000);
45
46 $self->_initmonitor if $self->{window}->{Device} eq 'adremo';
47 $self->_initautoscan if $self->{window}->{config}->ReadInt('Buttons') == 1;
48 $self->_initkeys;
49 $self->_initicon;
50
51 $self->StartMonitor if $self->{window}->{Device} eq 'adremo';
52 $self->StartAutoscan if $self->{window}->{config}->ReadInt('Buttons') == 1;
53
54 return $self;
55 }
56
57 sub newchild
58 {
59 my $proto = shift;
60 my $class = ref($proto) || $proto;
61 my $self = {};
62 bless $self, $class;
63 $self->{window} = shift;
64
65 # We get the configuration from the Windows registry
66 # If it's not initialized, we provide some defaults
67 $self->{window}->{config} = Wx::ConfigBase::Get || croak "Can't get Config";
68
69 # Get the input-device
70 # icon = mouse left/right buttons
71 # adremo = electric wheelchair adremo
72 # keys = keystrokes
73 $self->{window}->{Device} = $self->{window}->{config}->Read('Device', 'icon');
74
75 $self->_initkeys;
76 $self->_initicon;
77
78 return $self;
79 }
80
81 sub _initkeys
82 {
83 my $self = shift;
84 EVT_CHAR($self->{window}, \&_keycontrol)
85 if $self->{window}->{config}->Read('Device') eq 'keys';
86 }
87
88 sub _initicon
89 {
90 my $self = shift;
91 EVT_MOUSE_EVENTS($self->{window}, \&_iconcontrol)
92 if $self->{window}->{config}->Read('Device') eq 'icon';
93 }
94
95 sub _initmonitor
96 {
97 my $self = shift;
98 # The event for the adremo device
99 $self->{window}->{adremotimer} = Wx::Timer->new($self->{window},my $tid = Wx::NewId());
100 EVT_TIMER($self->{window}, $tid, \&_monitorport);
101 }
102
103 sub _initautoscan
104 {
105 my $self = shift;
106 $self->{window}->{onebuttontimer} = Wx::Timer->new($self->{window},my $obtid = Wx::NewId());
107 EVT_TIMER($self->{window}, $obtid, sub{my $self = shift; $self->{input}->{next}->() if $self->{input}->{next}});
108 }
109
110 sub StartMonitor
111 {
112 my $self = shift;
113 $self->{window}->{adremotimer}->Start($self->{window}->{Interval}, 0) # 0 is continuous
114 if $self->{window}->{adremotimer};
115 }
116
117 sub QuitMonitor
118 {
119 my $self = shift;
120 # stop the timer for the port monitor
121 $self->{window}->{adremotimer}->Stop() if $self->{window}->{adremotimer} && $self->{window}->{adremotimer}->IsRunning;
122 }
123
124 sub StartAutoscan
125 {
126 my $self = shift;
127 $self->{window}->{onebuttontimer}->Start($self->{window}->{OneButtonInterval}, 0) # 0 is continuous
128 if $self->{window}->{onebuttontimer};
129 }
130
131 sub QuitAutoscan
132 {
133 my $self = shift;
134 $self->{window}->{onebuttontimer}->Stop() if $self->{window}->{onebuttontimer} && $self->{window}->{onebuttontimer}->IsRunning;
135 }
136
137 sub PauseMonitor
138 {
139 my $self = shift;
140 my $bool = shift;
141 return unless $self->{window}->{config}->Read('Device') eq 'adremo';
142 $self->QuitMonitor if $bool;
143 $self->StartMonitor unless $bool;
144 }
145
146 sub PauseAutoscan
147 {
148 my $self = shift;
149 my $bool = shift;
150 return unless $self->{window}->{config}->ReadInt('Buttons') == 1;
151 $self->QuitAutoscan if $bool;
152 $self->StartAutoscan unless $bool;
153 }
154
155 sub Pause
156 {
157 my $self = shift;
158 $self->{pause} = shift;
159 }
160
161 sub GetDevice
162 {
163 my $self = shift;
164 return $self->{window}->{config}->Read('Device');
165 }
166
167 sub SetupMouse
168 {
169 my $self = shift;
170 my ($window, $subgetfocus, $subup, $sublosefocus) = @_;
171
172 if ($self->{window}->{config}->Read('Device') eq 'mouse')
173 {
174 EVT_MOUSE_EVENTS($window, sub { my ($self, $event) = @_;
175 &$subup if $event->LeftUp;
176 &$sublosefocus if $event->Leaving;
177 &$subgetfocus if $event->Entering;
178 });
179 }
180
181
182 }
183
184 sub Next
185 {
186 my $self = shift;
187 my $sub = shift;
188 $self->{next} = $sub;
189 }
190
191 sub Select
192 {
193 my $self = shift;
194 my $sub = shift;
195 $self->{select} = $sub;
196 }
197
198 sub _keycontrol
199 {
200 # BEWARE: $self is the window object this event belongs to
201 my ($self, $event) = @_;
202 return if $self->{pause};
203 $self->{input}->{select}->() if ($event->GetKeyCode == $self->{config}->ReadInt('SelectKey', WXK_RETURN)) || (uc(chr($event->GetKeyCode)) eq uc(chr($self->{config}->ReadInt('SelectKey'))));
204 $self->{input}->{next}->() if (( ($event->GetKeyCode == $self->{config}->ReadInt('NextKey', WXK_SPACE)) ||
205 (uc(chr($event->GetKeyCode)) eq uc(chr($self->{config}->ReadInt('NextKey'))))) and
206 (not $self->{config}->ReadInt('Buttons') == 1));
207 }
208
209 sub _iconcontrol
210 {
211 # BEWARE: $self is the window object this event belongs to
212 my ($self, $event) = @_;
213 return if $self->{pause};
214 $self->{input}->{select}->() if $event->LeftUp;
215 $self->{input}->{next}->() if $event->RightUp &&
216 not $self->{config}->ReadInt('Buttons') == 1;
217 }
218
219 #----------------------------------------------------------------------
220 # This sub is used to monitor the parallel port for the adremo device
221 sub _monitorport
222 {
223 # BEWARE: $self is the wxWindow subclass the timer
224 # belongs to!
225 my ($self, $event) = @_;
226 # do nothing if the device is not adremo or
227 # if we're already running
228 return if ($self->{monitorrun} ||
229 (not $self->{input}->{next}) ||
230 (not $self->{input}->{select}) ||
231 $self->{pause});
232 # set the flag that we're checking the port
233 $self->{monitorrun} = 1;
234 $self->{pp} = Device::ParallelPort->new() if not $self->{pp};
235 my $curvalue = $self->{pp}->get_status();
236 if (not defined $curvalue)
237 {
238 # clear the flag that we're checking the port and return
239 $self->{monitorrun} = 0;
240 return
241 }
242 $self->{lastvalue} = 0 if not exists $self->{lastvalue};
243 # if we detect a change...
244 if ($curvalue != $self->{lastvalue})
245 {
246 unless ($curvalue & 0x40)
247 {
248 # if bit 6 is off it's a headmove to the right
249 # which will indicate a Next() event unless we're in
250 # one button mode
251 $self->{input}->{next}->() unless $self->{config}->ReadInt('Buttons') == 1;
252 }
253 if ($curvalue & 0x80)
254 {
255 # if bit 7 is on (this bit is inverted), it's a headmove to the left
256 # which will indicate a Select() event.
257 $self->{input}->{select}->();
258 }
259 }
260 # the current value becomes the last value
261 $self->{lastvalue} = $curvalue if $curvalue;
262
263 # clear the flag that we're checking the port
264 $self->{monitorrun} = 0;
265 }
266
267
268 1;
269
270 __END__