MultiHead Background in GNOME

I made a cheap script to handle it for me. Needs GNOME31) and ImageMagick.

THA SCRIPT

#!/usr/bin#!/usr/bin/perl
 
use strict;
use autodie;
 
## Create and set a background for three monitors.
#
##  FIXME: THE FOLLOWING IS A LIE
# In GNOME3, you need to turn off background image handling:
#
#	gsettings set org.gnome.settings-daemon.plugins.background active false
#
# to change back
#
#	gsettings reset org.gnome.settings-daemon.plugins.background active
 
 
my @monitor_resolutions = qw/1680x1050 1920x1200 1440x900/;
my $background_dir = "/home/sam/Pictures/Backgrounds";
my $convert = "/usr/bin/convert";
my $gsettings = "/usr/bin/gsettings";
my $final_dir = "/home/sam/.desktop_image";
my $bg_color = "black";
my $tmpdir = "/tmp";
 
my @temp_files;
 
$ENV{"XAUTHORITY"} = "/home/sam/.Xauthority";
 
my %gs_env = pgrep_env($ENV{"USER"}, "gnome-shell");
$ENV{"DBUS_SESSION_BUS_ADDRESS"} =  $gs_env{"DBUS_SESSION_BUS_ADDRESS"};
 
 
# get image files
opendir my($dh), $background_dir;
my @images = grep { !/^\.\.?$/ } readdir $dh;
closedir $dh;
 
 
# make parts
foreach my $rez (@monitor_resolutions) {
	my $tmp_file = $tmpdir."/bg_".$rez."_".uniqid().".jpg";
	push @temp_files, $tmp_file;
	my $cmd = "$convert -quality 100 '$background_dir/".$images[rand @images]."' -resize $rez^ -gravity center -crop ".$rez."+0+0! $tmp_file";
	#print "$cmd\n";
	system($cmd);
}
 
# purge current images
opendir my($fdh), $final_dir;
foreach (grep { !/^\.\.?$/ } readdir $fdh) { unlink $final_dir."/".$_; }
 
my $final_image = $final_dir."/".uniqid().".jpg";
 
 
my $assemble_cmd = "$convert -quality 100 -background $bg_color ".join(" ", @temp_files)." +append $final_image";
 
system($assemble_cmd);
 
my $set_bg_cmd = "$gsettings set org.gnome.desktop.background picture-uri file://".$final_image;
system($set_bg_cmd);
 
foreach (@temp_files) {
	unlink($_);
}
 
 
 
 
sub uniqid {
	my @parts = ( "A" .. "Z", , "a" .. "z", 0 .. 9);
 
	my $return = "";
 
	for (my $i = 0 ; $i < 16 ; $i++) {
		$return .= $parts[rand @parts];
 
	}
 
 
	return $return;
 
 
 
}
 
 
 
 
sub pgrep_env {
	my ($user, $proc_name) = @_;
	chomp(my $pid = `/usr/bin/pgrep -u $user ^$proc_name\$`);
	my $raw_env;
 
	{
		local $/;
		open(IN, "/proc/$pid/environ");
		$raw_env = <IN>;
		close(IN);
	}
 
	my %return;
	foreach (split('\0', $raw_env)) {
		(my @F) = split(/\=/, $_, 2);
 
		$return{"$F[0]"} = $F[1];
	}
 
 
	return %return;
 
 
}

~~LINKBACK~~ ~~DISCUSSION~~

1)
duh