1 package Imager::Screenshot;
3 use vars qw(@ISA $VERSION @EXPORT_OK);
8 @EXPORT_OK = 'screenshot';
13 $VERSION = '0.009_001';
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;
41 if (defined $opts{hwnd}) {
43 or die "Win32 driver not enabled\n";
44 $result = _win32($opts{hwnd}, $opts{decor}, $opts{left}, $opts{top},
45 $opts{right}, $opts{bottom}, $opts{display});
47 elsif (defined $opts{id}) { # X11 window id
48 exists $opts{display} or $opts{display} = 0;
50 or die "X11 driver not enabled\n";
51 $result = _x11($opts{display}, $opts{id}, $opts{left}, $opts{top},
52 $opts{right}, $opts{bottom});
54 elsif (defined $opts{darwin}) { # as long as it's there
56 or die "Darwin driver not enabled\n";
57 $result = _darwin($opts{left}, $opts{top}, $opts{right}, $opts{bottom});
59 elsif ($opts{widget}) {
61 my $top = $opts{widget}->toplevel;
62 my $sys = $top->windowingsystem;
63 if ($sys eq 'win32') {
64 unless (defined &_win32) {
65 Imager->_set_error("Win32 Tk and Win32 support not built");
68 $result = _win32(hex($opts{widget}->id), $opts{decor},
69 $opts{left}, $opts{top}, $opts{right}, $opts{bottom});
71 elsif ($sys eq 'x11') {
72 unless (defined &_x11) {
73 Imager->_set_error("X11 Tk and X11 support not built");
77 my $id_hex = $opts{widget}->id;
79 # is there a way to get the display pointer from Tk?
80 $result = _x11($opts{display}, hex($id_hex), $opts{left}, $opts{top},
81 $opts{right}, $opts{bottom});
84 Imager->_set_error("Unsupported windowing system '$sys'");
90 defined &_win32 ? _win32(0, $opts{decor}, $opts{left}, $opts{top},
91 $opts{right}, $opts{bottom}, $opts{display}) :
92 defined &_darwin ? _darwin($opts{left}, $opts{top},
93 $opts{right}, $opts{bottom}) :
94 defined &_x11 ? _x11($opts{display}, 0, $opts{left}, $opts{top},
95 $opts{right}, $opts{bottom}) :
96 die "No drivers enabled\n";
100 Imager->_set_error(Imager->_error_as_msg());
104 # RT #24992 - the Imager typemap entry is broken pre-0.56, so
106 return bless { IMG => $result }, "Imager";
122 my $display = _x11_open(@_);
124 Imager->_set_error(Imager->_error_as_msg);
141 Imager::Screenshot - screenshot to an Imager image
145 use Imager::Screenshot 'screenshot';
148 my $img = screenshot();
151 my $img2 = screenshot(hwnd => $hwnd);
154 my $img3 = screenshot(display => $display, id => $window_id);
157 my $display = Imager::Screenshot::x11_open();
158 Imager::Screenshot::x11_close($display);
160 # test for win32 support
161 if (Imager::Screenshot->have_win32) { ... }
163 # test for x11 support
164 if (Imager::Screenshot->have_x11) { ... }
166 # test for Darwin (Mac OS X) support
167 if (Imager::Screenshot->have_darwin) { ... }
172 Imager::Screenshot captures either a desktop or a specified window and
173 returns the result as an Imager image.
175 Currently the image is always returned as a 24-bit image.
179 =item screenshot hwnd => I<window handle>
181 =item screenshot hwnd => I<window handle>, decor => <capture decorations>
183 Retrieve a screenshot under Win32, if I<window handle> is zero,
186 By default, window decorations are not captured, if the C<decor>
187 parameter is set to true then window decorations are included.
189 =item screenshot id => I<window id>
191 =item screenshot id => I<window id>, display => I<display object>
193 Retrieve a screenshot under X11, if I<id> is zero, capture the root
194 window. I<display object> is a integer version of an X11 C< Display *
195 >, if this isn't supplied C<screenshot()> will attempt connect to the
196 the display specified by $ENV{DISPLAY}.
198 Note: taking a screenshot of a remote display is slow.
200 =item screenshot darwin => 0
202 Retrieve a screenshot under Mac OS X. The only supported value for
203 the C<darwin> parameter is C<0>.
205 For a screen capture to be taken, the current user using
206 Imager:Screenshot must be the currently logged in user on the display.
208 If you're using fast user switching, the current user must be the
211 Note: this means you can ssh into a Mac OS X box and screenshot from
212 the ssh session, if you're the current user on the display.
214 =item screenshot widget => I<widget>
216 =item screenshot widget => I<widget>, display => I<display>
218 =item screenshot widget => I<widget>, decor => I<capture decorations>
220 Retrieve a screenshot of a Tk widget, under Win32 or X11, depending on
221 how Tk has been built.
223 If Tk was built for X11 then the display parameter applies.
225 If Tk was built for Win32 then the decor parameter applies.
229 If no C<id>, C<hwnd> or C<widget> parameter is supplied:
235 if Win32 support is compiled, return screenshot(hwnd => 0).
239 if Darwin support is compiled, return screenshot(darwin => 0).
243 if X11 support is compiled, return screenshot(id => 0).
251 You can also supply the following parameters to retrieve a subset of
274 If left or top is negative, then treat that as from the right/bottom
277 If right ot bottom is zero or negative then treat as from the
278 right/bottom edge of the window.
280 So setting all 4 values to 0 retrieves the whole window.
282 # a 10-pixel wide right edge of the window
283 my $right_10 = screenshot(left => -10, ...);
285 # the top-left 100x100 portion of the window
286 my $topleft_100 = screenshot(right => 100, bottom => 100, ...);
288 # 10x10 pixel at the bottom right corner
289 my $bott_right_10 = screenshot(left => -10, top => -10, ...);
291 If screenshot() fails, it will return nothing, and the cause of the
292 failure can be retrieved via Imager->errstr, so typical use could be:
294 my $img = screenshot(...) or die Imager->errstr;
298 Returns true if Win32 support is available.
302 Returns true if X11 support is available.
306 Returns true if Darwin support is available.
308 =item Imager::Screenshot::x11_open
310 =item Imager::Screenshot::x11_open I<display name>
312 Attempts to open a connection to either the display name in
313 $ENV{DISPLAY} or the supplied display name. Returns a value suitable
314 for the I<display> parameter of screenshot, or undef.
316 =item Imager::Screenshot::x11_close I<display>
318 Closes a display returned by Imager::Screenshot::x11_open().
324 screenshot() sets a number of tags in the images it returns, these are:
330 ss_left - the distance between the left side of the window and the
331 left side of the captured area. The same value as the I<left>
332 parameter when that is positive.
336 ss_top - the distance between the top side of the window the top side
337 of the captured area. The same value at the I<top> parameter when
342 ss_window_width - the full width of the window.
346 ss_window_height - the full height of the window.
350 ss_type - the type of capture done, either "Win32" or "X11".
354 To cheaply get the window size you can capture a single pixel:
356 my $im = screenshot(right => 1, bottom => 1);
357 my $window_width = $im->tags(name => 'ss_window_width');
358 my $window_height = $im->tags(name => 'ss_window_height');
362 It's possible to have more than one grab driver available, for
363 example, Win32 and X11, and which is used can have an effect on the
366 Under Win32 or OS X, if there's a screesaver running, then you grab
367 the results of the screensaver.
369 On OS X, you can grab the display from an ssh session as long as the
370 ssh session is under the same user as the currently active user on the
373 Grabbing the root window on a rootless server (eg. Cygwin/X) may not
374 grab the background that you see. In fact, when I tested under
375 Cygwin/X I got the xterm window contents even when the Windows
376 screensaver was running. The root window captured appeared to be that
377 generated by my window manager.
379 Grabbing a window with other windows overlaying it will capture the
380 content of those windows where they hide the window you want to
381 capture. You may want to raise the window to top. This may be a
382 security concern if the overlapping windows contain any sensitive
383 information - true for any screen capture.
387 Imager::Screenshot is licensed under the same terms as Perl itself.
391 Future plans include:
397 window name searches - currently screenshot() requires a window
398 identifier of some sort, it would be more usable if we could supply
399 some other identifier, either a window title or a window class name.
405 Tony Cook <tonyc@cpan.org>