1 package Imager::Screenshot;
3 use vars qw(@ISA $VERSION @EXPORT_OK);
8 @EXPORT_OK = 'screenshot';
15 # try XSLoader first, DynaLoader has annoying baggage
17 XSLoader::load('Imager::Screenshot' => $VERSION);
21 push @ISA, 'DynaLoader';
22 bootstrap Imager::Screenshot $VERSION;
27 # lose the class if called as a method
28 @_ % 2 == 1 and shift;
30 my %opts = (decor => 0, display => 0, @_);
35 defined &_win32 ? _win32(0) :
36 defined &_x11 ? _x11($opts{display}, 0) :
37 die "No drivers enabled\n";
39 if (defined $opts{hwnd}) {
41 or die "Win32 driver not enabled\n";
42 $result = _win32($opts{hwnd}, $opts{decor});
44 elsif (defined $opts{id}) { # X11 window id
46 or die "X11 driver not enabled\n";
47 $result = _x11($opts{display}, $opts{id});
49 elsif ($opts{widget}) {
51 my $top = $opts{widget}->toplevel;
52 my $sys = $top->windowingsystem;
53 if ($sys eq 'win32') {
54 unless (defined &_win32) {
55 Imager->_set_error("Win32 Tk and Win32 support not built");
58 $result = _win32(hex($opts{widget}->id));
60 elsif ($sys eq 'x11') {
61 unless (defined &_x11) {
62 Imager->_set_error("X11 Tk and X11 support not built");
66 my $id_hex = $opts{widget}->id;
67 $opts{widget}->can('frame')
68 and $id_hex = $opts{widget}->frame;
70 # is there a way to get the display pointer from Tk?
71 $result = _x11(0, hex($id_hex));
74 Imager->_set_error("Unsupported windowing system '$sys'");
80 Imager->_set_error(Imager->_error_as_msg());
96 my $display = _x11_open(@_);
98 Imager->_set_error(Imager->_error_as_msg);
115 Imager::Screenshot - screenshot to an Imager image
119 use Imager::Screenshot 'screenshot';
122 my $img = screenshot();
125 my $img2 = screenshot(hwnd => $hwnd);
128 my $img3 = screenshot(display => $display, id => $window_id);
131 my $display = Imager::Screenshot::x11_open();
132 Imager::Screenshot::x11_close($display);
134 # test for win32 support
135 if (Imager::Screenshot->have_win32) { ... }
137 # test for x11 support
138 if (Imager::Screenshot->have_x11) { ... }
143 Imager::Screenshot captures either a desktop or a specified window and
144 returns the result as an Imager image.
146 Currently the image is always returned as a 24-bit image.
150 =item screenshot hwnd => I<window handle>
152 =item screenshot hwnd => I<window handle>, decor => <capture decorations>
154 Retrieve a screenshot under Win32, if I<window handle> is zero,
157 By default, window decorations are not captured, if the C<decor>
158 parameter is set to true then window decorations are included.
160 =item screenshot id => I<window id>
162 =item screenshot id => I<window id>, display => I<display object>
164 Retrieve a screenshot under X11, if I<id> is zero, capture the root
165 window. I<display object> is a integer version of an X11 C< Display *
166 >, if this isn't supplied C<screenshot()> will attempt connect to the
167 the display specified by $ENV{DISPLAY}.
169 Note: taking a screenshot of a remote display is slow.
173 If no parameters are supplied:
179 if Win32 support is compiled, return screenshot(hwnd => 0).
183 if X11 support is compiled, return screenshot(id => 0).
193 Returns true if Win32 support is available.
197 Returns true if X11 support is available.
199 =item Imager::Screenshot::x11_open
201 =item Imager::Screenshot::x11_open I<display name>
203 Attempts to open a connection to either the display name in
204 $ENV{DISPLAY} or the supplied display name. Returns a value suitable
205 for the I<display> parameter of screenshot, or undef.
207 =item Imager::Screenshot::x11_close I<display>
209 Closes a display returned by Imager::Screenshot::x11_open().
215 Imager::Screenshot is licensed under the same terms as Perl itself.
219 Tony Cook <tonyc@cpan.org>