| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # xorg::screenshot Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Xorg::Screenshot; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik::Shell::Command Metabrik::System::Package); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | datadir => [ qw(datadir) ], | ||
| 20 | output => [ qw(output) ], | ||
| 21 | format => [ qw(image_format) ], | ||
| 22 | delay => [ qw(microseconds) ], | ||
| 23 | }, | ||
| 24 | attributes_default => { | ||
| 25 | output => 'screenshot-00001', | ||
| 26 | format => 'png', | ||
| 27 | delay => 100, | ||
| 28 | ignore_error => 0, | ||
| 29 | }, | ||
| 30 | commands => { | ||
| 31 | install => [ ], # Inherited | ||
| 32 | active_window => [ qw(output|OPTIONAL format|OPTIONAL) ], | ||
| 33 | full_screen => [ qw(output|OPTIONAL format|OPTIONAL) ], | ||
| 34 | select_window => [ qw(output|OPTIONAL format|OPTIONAL) ], | ||
| 35 | window_id => [ qw(window_id output|OPTIONAL format|OPTIONAL) ], | ||
| 36 | continuous_by_window_id => [ qw(window_id delay|OPTIONAL) ], | ||
| 37 | }, | ||
| 38 | require_modules => { | ||
| 39 | 'Time::HiRes' => [ ], | ||
| 40 | 'Metabrik::File::Find' => [ ], | ||
| 41 | 'Metabrik::System::File' => [ ], | ||
| 42 | }, | ||
| 43 | require_binaries => { | ||
| 44 | 'import' => [ ], | ||
| 45 | 'scrot' => [ ], | ||
| 46 | }, | ||
| 47 | need_packages => { | ||
| 48 | ubuntu => [ qw(imagemagick scrot) ], | ||
| 49 | debian => [ qw(imagemagick scrot) ], | ||
| 50 | kali => [ qw(imagemagick scrot) ], | ||
| 51 | }, | ||
| 52 | }; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _get_new_output { | ||
| 56 | my $self = shift; | ||
| 57 | my ($format) = @_; | ||
| 58 | |||
| 59 | $format ||= $self->format; | ||
| 60 | |||
| 61 | my $datadir = $self->datadir; | ||
| 62 | |||
| 63 | my $ff = Metabrik::File::Find->new_from_brik_init($self) or return; | ||
| 64 | my $files = $ff->files($datadir, 'screenshot-\d+\.'.$format) or return; | ||
| 65 | |||
| 66 | if (@$files == 0) { | ||
| 67 | return "$datadir/screenshot-00001.$format"; # First output file | ||
| 68 | } | ||
| 69 | |||
| 70 | my @sorted = sort { $a cmp $b } @$files; | ||
| 71 | my ($id) = $sorted[-1] =~ m{screenshot-(\d+)\.$format}; | ||
| 72 | |||
| 73 | return $self->output(sprintf("$datadir/screenshot-%05d.$format", $id + 1)); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub active_window { | ||
| 77 | my $self = shift; | ||
| 78 | my ($output, $format) = @_; | ||
| 79 | |||
| 80 | $format ||= $self->format; | ||
| 81 | $output ||= $self->_get_new_output($format); | ||
| 82 | |||
| 83 | $self->log->verbose("active_window: saving to file [$output]"); | ||
| 84 | |||
| 85 | my $cmd = "scrot --focused --border $output"; | ||
| 86 | $self->execute($cmd) or return; | ||
| 87 | |||
| 88 | return $output; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub full_screen { | ||
| 92 | my $self = shift; | ||
| 93 | my ($output, $format) = @_; | ||
| 94 | |||
| 95 | $format ||= $self->format; | ||
| 96 | $output ||= $self->_get_new_output($format); | ||
| 97 | |||
| 98 | $self->log->verbose("full_screen: saving to file [$output]"); | ||
| 99 | |||
| 100 | my $cmd = "scrot $output"; | ||
| 101 | $self->execute($cmd) or return; | ||
| 102 | |||
| 103 | return $output; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub select_window { | ||
| 107 | my $self = shift; | ||
| 108 | my ($output, $format) = @_; | ||
| 109 | |||
| 110 | $format ||= $self->format; | ||
| 111 | $output ||= $self->_get_new_output($format); | ||
| 112 | |||
| 113 | $self->log->verbose("select_window: saving to file [$output]"); | ||
| 114 | |||
| 115 | my $cmd = "scrot --select --border $output"; | ||
| 116 | $self->execute($cmd) or return; | ||
| 117 | |||
| 118 | return $output; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub window_id { | ||
| 122 | my $self = shift; | ||
| 123 | my ($window_id, $output, $format) = @_; | ||
| 124 | |||
| 125 | $format ||= $self->format; | ||
| 126 | $output ||= $self->_get_new_output($format); | ||
| 127 | $self->brik_help_run_undef_arg('window_id', $window_id) or return; | ||
| 128 | |||
| 129 | if ($format ne 'gif') { | ||
| 130 | return $self->log->error("window_id: only GIF format supported"); | ||
| 131 | } | ||
| 132 | |||
| 133 | my $cmd = "import -window $window_id $output"; | ||
| 134 | my $r = $self->execute($cmd) or return; | ||
| 135 | |||
| 136 | if ($r == 256) { | ||
| 137 | return $self->log->error("window_id: import failed"); | ||
| 138 | } | ||
| 139 | |||
| 140 | return $output; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub continuous_by_window_id { | ||
| 144 | my $self = shift; | ||
| 145 | my ($window_id, $delay) = @_; | ||
| 146 | |||
| 147 | $delay ||= $self->delay; | ||
| 148 | $self->brik_help_run_undef_arg('continuous_by_window_id', $window_id) or return; | ||
| 149 | |||
| 150 | my $datadir = $self->datadir; | ||
| 151 | |||
| 152 | my $sf = Metabrik::System::File->new_from_brik_init($self) or return; | ||
| 153 | my $list = $sf->glob("$datadir/continuous-*gif") or return; | ||
| 154 | $sf->remove($list) or return; | ||
| 155 | |||
| 156 | my @files = (); | ||
| 157 | my $file = ''; | ||
| 158 | my $frame = 1; | ||
| 159 | while (1) { | ||
| 160 | $file = sprintf("$datadir/continuous-%05d.gif", $frame); | ||
| 161 | my $r = $self->execute("import -window $window_id $file"); | ||
| 162 | if ($r > 1) { # It means we have been interrupted | ||
| 163 | $self->log->verbose("continuous_by_window_id: interrupted by user"); | ||
| 164 | last; | ||
| 165 | } | ||
| 166 | $self->log->verbose("continuous_by_window_id: done with [$file]"); | ||
| 167 | push @files, $file; | ||
| 168 | Time::HiRes::usleep($delay); | ||
| 169 | $frame++; | ||
| 170 | } | ||
| 171 | |||
| 172 | return \@files; | ||
| 173 | } | ||
| 174 | |||
| 175 | 1; | ||
| 176 | |||
| 177 | __END__ | ||