blib/lib/Win32/GUIRobot.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 # $Id$
2  
3 package Win32::GUIRobot;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Prima;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Prima::Application;
9 use Time::HiRes qw(time);
10  
11 our $VERSION = 0.05;
12  
13 require Exporter;
14 our @ISA = qw(Exporter);
15 our @EXPORT_OK = qw(
16 ScreenGrab ScreenDepth ScreenWidth ScreenHeight
17 LoadImage ImageDepth ImageWidth ImageHeight
18 FindImage WaitForImage
19  
20 Sleep
21 CloseWindow Rect2OffsetSize
22 SendMouseClick MouseMove MouseMoveRel
23 );
24 our %EXPORT_TAGS = (all => \@EXPORT_OK);
25 if ( $^O =~ /win32|cygwin/i) {
26 eval "use Win32::GuiTest qw(:ALL);";
27 die $@ if $@;
28 push @EXPORT_OK, @Win32::GuiTest::EXPORT_OK;
29 }
30  
31  
32 my %mouse_buttons = (
33 Left => [ \&SendLButtonDown, \&SendLButtonUp ],
34 Middle => [ \&SendMButtonDown, \&SendMButtonUp ],
35 Right => [ \&SendRButtonDown, \&SendRButtonUp ],
36 );
37  
38 our $EventDelay = 0.1;
39  
40 sub LoadImage { Prima::Image-> load( @_ ) }
41 sub ImageDepth { shift-> type & im::BPP }
42 sub ImageWidth { shift-> width }
43 sub ImageHeight { shift-> height }
44 sub ScreenDepth { $::application-> get_image(0,0,1,1)-> type & im::BPP }
45 sub ScreenWidth { $::application-> width }
46 sub ScreenHeight { $::application-> height }
47 sub Sleep { select ( undef, undef, undef, $_[0] || $EventDelay ) }
48 sub CloseWindow { PostMessage( $_[0], 16, 0, 0) }
49 sub Rect2OffsetSize { $_[0], $_[1], $_[2] - $_[0], $_[3] - $_[1] }
50  
51 sub ScreenGrab
52 {
53 my @rect;
54 if ( 4 == @_) {
55 @rect = (
56 $_[0], $::application-> height - $_[1] - $_[3],
57 $_[2], $_[3],
58 );
59 } elsif ( 0 == @_) {
60 @rect = (0,0,$::application-> size);
61 } else {
62 die "ScreenGrab ([X,Y,W,H])";
63 }
64  
65 return $::application-> get_image( @rect);
66 }
67  
68 sub FindImage
69 {
70 my ( $image, $subimage) = @_;
71  
72 if ( ref($subimage) eq 'ARRAY') {
73 for ( my $i = 0; $i < @$subimage; $i++) {
74 my ( $x, $y) = FindImage( $image, $subimage->[$i]);
75 return ( $x, $y, $i) if defined $x;
76 }
77 return;
78 }
79  
80 my $G = $image-> data;
81 my $I = $subimage-> data;
82 my $W = $image-> width;
83 my $w = $subimage-> width;
84  
85 my $bpp = ($subimage-> type & im::BPP) / 8;
86  
87 die "won't do images with less than 256 colors"
88 if $bpp < 0;
89 die "won't do images with different depth"
90 if $subimage-> type != $image-> type;
91  
92 my $gw = int(( $W * ( $image-> type & im::BPP) + 31) / 32) * 4;
93 my $iw = int(( $w * ( $subimage-> type & im::BPP) + 31) / 32) * 4;
94 my $ibw = $w * $bpp;
95 my $dw = $gw - $ibw;
96
97 my $rx = join( ".{$dw}", map { quotemeta substr( $I, $_ * $iw, $ibw) }
98 (0 .. $subimage-> height - 1));
99 my $D = 0;
100 my ( $x, $y);
101 while ( 1) {
102 study $G;
103 return unless $G =~ m/$rx/gs;
104 $x = ( $D + pos($G)) % $gw / $bpp;
105 last if $x >= $w;
106 # handle scanline wraps, -- very unlikely, but still
107 $D += pos($G);
108 substr( $G, pos($G)) = '';
109 }
110 $y = int(( $D + pos($G)) / $gw) + 1;
111 return ( $x - $w, $image-> height - $y);
112 }
113  
114 sub SendMouseClick
115 {
116 my ( $x, $y, $button, $delay) = @_;
117  
118 $button ||= 'Left';
119  
120 die "No such mouse button '$button'" unless $mouse_buttons{$button};
121  
122 MouseMoveAbsPix( $x, $y);
123 Sleep( $delay);
124 $mouse_buttons{$button}-> [0]-> ();
125 Sleep( $delay);
126 $mouse_buttons{$button}-> [1]-> ();
127 Sleep( $delay);
128 }
129  
130 sub MouseMove
131 {
132 my ( $x, $y, $sleep) = @_;
133  
134 MouseMoveAbsPix( $x, $y);
135 Sleep( $sleep);
136 }
137  
138 sub MouseMoveRel
139 {
140 my ( $x, $y, $sleep) = @_;
141  
142 MouseMoveRelPix( $x, $y);
143 Sleep( $sleep);
144 }
145  
146 sub WaitForImage
147 {
148 my ( $subimage, %options) = @_;
149  
150 my @rect;
151
152 if ($options{window}) {
153 @rect = Rect2OffsetSize( GetWindowRect( $options{window} ));
154 } elsif ( $options{rect}) {
155 @rect = @{$options{rect}};
156 } else {
157 @rect = (0, 0, $::application-> size);
158 }
159  
160 $options{maxwait} ||= 0;
161 $options{maxwait} += time;
162  
163 my $grab;
164 while ( 1) {
165 $grab = ScreenGrab( @rect);
166 last unless $grab;
167  
168 my ( $x, $y, $idx) = FindImage( $grab, $subimage);
169 return {
170 ok => 1,
171 x => $x + $rect[0],
172 y => $y + $rect[1],
173 idx => $idx
174 } if defined $x;
175  
176 last if time > $options{maxwait};
177  
178 Sleep( $options{sleep} );
179 }
180  
181 return { grab => $grab } ;
182 }
183  
184  
185 1;
186  
187 __DATA__
↑ Summary