File size: 5,939 Bytes
7578e9d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | use utf8;
use strict;
use warnings;
use Net::DBus::Reactor;
use Net::DBus;
package Shutter::Screenshot::Wayland;
use Shutter::Screenshot::History;
#Object wrapper around the portal so full/monitor captures can be repeated
#(e.g. via the redoshot/F5 shortcut) just like the X11 screenshooters.
sub new {
my $class = shift;
my $self = {
_sc => shift,
_interactive => shift, # whether to use the desktop environment's GUI (only for the time till DEs support non-interactive calls)
_target => shift, # screenshot mode (full, select, window, awindow) to send to xdg-portal
_monitor => shift, # undef captures the whole desktop
};
$self->{_gdk_screen} = Gtk3::Gdk::Screen::get_default();
bless $self, $class;
return $self;
}
sub redo_capture {
my $self = shift;
return 3 unless defined $self->{_history};
return $self->xdg_portal;
}
sub get_history {
my $self = shift;
return $self->{_history};
}
sub get_error_text {
my $self = shift;
return $self->{_error_text};
}
sub xdg_portal {
my $self = shift;
my $d = $self->{_sc}->get_gettext;
# Fall back to fullscreen
$self->{_target} = 1 unless defined $self->{_target};
my $reactor = Net::DBus::Reactor->main;
my $bus = Net::DBus->find;
my $me = $bus->get_unique_name;
$me =~ s/\./_/g;
$me =~ s/^://g;
my $pixbuf;
my $portal_error;
eval {
my $portal_service = $bus->get_service('org.freedesktop.portal.Desktop');
my $portal = $portal_service->get_object('/org/freedesktop/portal/desktop', 'org.freedesktop.portal.Screenshot');
my $num;
my $output;
my $cb = sub {
($num, $output) = @_;
$reactor->shutdown;
};
my $token = 'shutter' . rand;
$token =~ s/\.//g;
my $request = $portal_service->get_object("/org/freedesktop/portal/desktop/request/$me/$token", 'org.freedesktop.portal.Request');
my $conn = $request->connect_to_signal(Response => $cb);
my %options = (handle_token => $token);
# set the interactive flag unless xdg-portal doesn't support non-interactive calls and we want full-screen capture
$options{interactive} = Net::DBus::dbus_boolean($self->{_interactive}) unless $self->{_interactive} eq 1 && $self->{_target} eq 1;
# only define a target if xdg-portal supports non-interactive calls
$options{target} = Net::DBus::dbus_uint32($self->{_target}) if $self->{_interactive} ne 1;
my $request_path = $portal->Screenshot('', \%options);
if ($request->get_object_path ne $request_path) {
$request->disconnect_from_signal(Response => $conn);
$request = $portal_service->get_object($request_path, 'org.freedesktop.portal.Request');
$conn = $request->connect_to_signal(Response => $cb);
}
$reactor->run;
$request->disconnect_from_signal(Response => $conn);
if (!defined $num || $num != 0) {
if (defined $num && $num == 1) {
# portal Response: 1 = user cancelled -> treat as abort (code 5), not error
return 5;
}
$portal_error = "Response " . (defined $num ? $num : "timeout") . " from XDG portal";
return;
}
unless (defined $output && defined $output->{uri}) {
$portal_error = "XDG portal returned no screenshot URI";
return;
}
my $giofile = Glib::IO::File::new_for_uri($output->{uri});
print "xdg portal: got temp file ".$giofile->get_path."\n" if $self->{_sc}->get_debug;
$pixbuf = Gtk3::Gdk::Pixbuf->new_from_file($giofile->get_path);
$giofile->delete;
};
if (defined $self->{_monitor}) {
$pixbuf = crop_to_monitor($pixbuf, $self->{_gdk_screen}, $self->{_monitor});
}
#a history marker makes this capture repeatable through redoshot
$self->{_history} = Shutter::Screenshot::History->new($self->{_sc});
if ($@) {
$self->{_error_text} = $@;
return 9;
}
if (defined $portal_error) {
$self->{_error_text} = $portal_error;
return 9;
}
# get name
if ($self->{_target} eq 1) {
if (defined $self->{_monitor}) {
$self->{_action_name} = $self->{_gdk_screen}->get_monitor_plug_name($self->{_monitor});
} else {
$self->{_action_name} = $d->get("Workspaces");
}
} elsif ($self->{_target} eq 2 || $self->{_target} eq 8) {
$self->{_action_name} = $d->get("Window");
} elsif ($self->{_target} eq 4) {
my $selection_text = $d->get("Selection");
my $swidth = $pixbuf->get_width;
my $sheight = $pixbuf->get_height;
if (defined $swidth && defined $sheight) {
$self->{_action_name} = "${selection_text}_${swidth}x${sheight}";
} else {
$self->{_action_name} = $selection_text;
}
}
return $pixbuf;
}
#The XDG portal always returns the whole desktop spanning every monitor. Crop
#that pixbuf down to a single monitor's area so users can capture just one.
sub crop_to_monitor {
my ($pixbuf, $gdk_screen, $monitor) = @_;
return $pixbuf unless defined $pixbuf && defined $gdk_screen && defined $monitor;
my $geo = $gdk_screen->get_monitor_geometry($monitor);
return $pixbuf unless $geo;
#Monitor geometry is in logical pixels while the portal captures device
#pixels; derive the scale from the full desktop size to stay correct on HiDPI.
my $screen_w = $gdk_screen->get_width || $pixbuf->get_width;
my $screen_h = $gdk_screen->get_height || $pixbuf->get_height;
my $scale_x = $pixbuf->get_width / $screen_w;
my $scale_y = $pixbuf->get_height / $screen_h;
my $x = int($geo->{x} * $scale_x);
my $y = int($geo->{y} * $scale_y);
my $w = int($geo->{width} * $scale_x);
my $h = int($geo->{height} * $scale_y);
#clamp to the captured area
$x = 0 if $x < 0;
$y = 0 if $y < 0;
$w = $pixbuf->get_width - $x if $x + $w > $pixbuf->get_width;
$h = $pixbuf->get_height - $y if $y + $h > $pixbuf->get_height;
return $pixbuf if $w <= 0 || $h <= 0;
my $cropped = Gtk3::Gdk::Pixbuf->new('rgb', $pixbuf->get_has_alpha, 8, $w, $h);
$pixbuf->copy_area($x, $y, $w, $h, $cropped, 0, 0);
return $cropped;
}
sub get_action_name {
my $self = shift;
return $self->{_action_name};
}
1;
|