ChangeLog entry for previous commits.
[fvwm-themes.git] / bin / fvwm-themes-root.in
blob5c6201e7d7e4ae6bcb0b6475e016da2f0d528a9b
1 #!@PERL@ -w
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 use strict;
18 use Getopt::Long;
20 my $prefix = $ENV{'prefix'} || '@prefix@';
21 my $ROOT_PREFIX = $ENV{'ROOT_PREFIX'} || '@ROOT_PREFIX@';
22 $ROOT_PREFIX = $ENV{'DESTDIR'} if $ENV{'DESTDIR'};
23 my $datadir = "@datadir@";
24 my $ftDataDir = "@FT_DATADIR@";
26 my $version = '@VERSION@';
27 my $scriptName = ($0 =~ m:([^/]+)$:, $1);
29 my $type = 'tile';
30 my $bg = 'rgb:c0/c0/c0';
31 my $fg = 'rgb:40/40/40';
32 my $fallbackTileImage = "$ftDataDir/images/background/stone.xpm";
34 GetOptions(
35         "help|h|?"     => \&showHelp,
36         "version|V"    => \&showVersion,
37         "tile|t"       => sub { $type = 'tile' },
38         "center|c"     => sub { $type = 'center' },
39         "adjust|fit|f" => sub { $type = 'adjust' },
40         "aspect|a"     => sub { $type = 'aspect' },
41         "solid|s"      => sub { $type = 'solid' },
42         "bg"           => \$bg,
43         "fg"           => \$fg,
46 # the additional argument should be either image or color
47 my $image = $type ne 'solid'? shift: undef;
48 my $color = $type eq 'solid'? shift: undef;
50 unless (defined $image || defined $color) {
51         print STDERR "Usage: fvwm-themes-root [options] image|color\n";
52         print STDERR "Try fvwm-themes-root --help for more information.\n";
53         exit(0);
56 my $HelperImageTypeAliases = {
57         tif => 'tiff',
58         jpg => 'jpeg',
61 my $HelperImageTypeSupport = {
62         'fvwm-root' => 'png|xpm|xbm',
63         'xsetroot'  => 'xbm',
64         'xsetbg'    => 'jpeg|tiff',  # does not support some types of JPG
65         'display'   => '*',
66         'wmsetbg'   => '*',
67         'xv'        => '*',
70 my $HelperSetRootCommands = {
71         tile => [
72                 'fvwm-root -r $image',
73                 'wmsetbg --tile --back-color $fg $image',
74                 'xsetroot -bg $bg -fg $fg -bitmap $image',
75                 'xv -root -quit $image',
76                 'xsetbg $image',
77         ],
78         center => [
79                 'wmsetbg --center --back-color $fg $image',
80                 'display -window root -backdrop -foreground $fg $image',
81                 'xsetbg -center $image -border $fg',
82                 'xv -root -quit -rmode 5 -rfg $fg $image',
83         ],
84         adjust => [
85                 'wmsetbg --scale $image',
86                 'xv -root -quit -max $image',
87                 'display -window root -geometry `xwininfo -root | grep geometry | cut -d" " -f4`\! $image',
88         ],
89         aspect => [
90                 'wmsetbg --maxscale --back-color $fg $image',
91                 'xv -root -quit -maxpect $image',
92                 'xsetbg -fullscreen $image -border $fg',
93         ],
94         solid => [
95                 'xsetroot -solid $color',
96                 'fvwm-themes-images --setroot --sky --sky-colors $color,$color',
97                 'xv -root -quit -crop 0 0 0 0 -rmode 5 -rfg $color',  # 1x1 pixel
98         ],
101 sub isExeInPath ($) {
102         my $exe = shift;
103         foreach my $dir ('/usr/local/bin', '/usr/bin', '/usr/bin/X11') {
104                 return 1 if -x "$dir/$exe";
105         }
106         return 0;
109 my $commands = $HelperSetRootCommands->{$type};
110 die "Internal error #1\n" unless defined $commands;
112 my $found = 0;
113 my $command;
114 my ($exe, $args);
115 foreach (@$commands) {
116         $command = $_;  # using $command directly in foreach fails in 5.8.0
117         ($exe, $args) = split(/ /, $command, 2);
119         # check if exists in $PATH
120         next unless isExeInPath($exe);
121         if ($type eq 'solid') {
122                 $found = 1;
123                 last;
124         } else {
125                 $image =~ /\.([^\.]+)(?:\.gz|\.bz2)?$/;
126                 my $ext = $1;
127                 $ext = $HelperImageTypeAliases->{$ext}
128                         if defined $ext && defined $HelperImageTypeAliases->{$ext};
129                 my $regexp = $HelperImageTypeSupport->{$exe};
130                 die "Internal error #2\n" unless defined $regexp;
131                 if ($regexp eq "*" || defined $ext && $ext =~ /^$regexp$/) {
132                         $found = 1;
133                         last;
134                 }
135         }
138 if (!$found) {
139         my $what = defined $color? "solid color $color": "image $image";
140         warn "[fvwm-themes-root]: No utilities found to set root $what\n"
141                 . "\tFalling back to default image $fallbackTileImage\n";
142         $image = $fallbackTileImage;
143         $command = $HelperSetRootCommands->{tile}->[0] || "false";
146 $command =~ s/(\$\w+)/'"' . eval("qq($1)") . '"'/sge;
147 $command =~ s/rgb:[\w\/]+/gray40/sg if $exe eq 'wmsetbg';  # dumb workaround
148 print $command, "\n" if $ENV{DEBUG};
149 system($command);
151 #-----------------------------------------------------------------------------
152 # The starndard functions
154 sub showHelp {
155         print "The fvwm-themes root utility.\n";
156         print "Usage: $scriptName [OPTIONS] image|color\n";
157         print "Options:\n";
158         print "\t--help              show this help and exit\n";
159         print "\t--version           show the version and exit\n";
160         print "\t--tile              display image tiled\n";
161         print "\t--center            display image centered\n";
162         print "\t--adjust            display image adjusted to the full size\n";
163         print "\t--aspect            display image adjusted keeping aspect\n";
164         print "\t--solid             display solid color\n";
165         print "\t--bg color          use bg in some modes that need it\n";
166         print "\t--fg color          use fg in some modes that need it\n";
167         exit 0;
170 sub showVersion {
171         print "$version\n";
172         exit 0;
175 sub wrongUsage {
176         print STDERR "Try '$scriptName --help' for more information.\n";
177         exit -1;
180 __END__
182 # ---------------------------------------------------------------------------
184 =head1 NAME
186 fvwm-themes-root - fvwm-themes utility for setting root image/color
188 =head1 SYNOPSIS
190 B<fvwm-themes-root>
191 [ B<--help|h> ]
192 [ B<--version|V> ]
193 I<image> | I<color>
195 =head1 DESCRIPTION
197 The aim of this script is to set the root image in fvwm-themes without
198 worrying about exact image viewer installed on a user system.
200 =head1 OPTIONS
202 B<--help>    - show the help and exit
204 B<--version> - show the version and exit
206 =head1 AUTHORS
208 Mikhael Goikhman <migo@homemail.com>.
210 =head1 COPYING
212 The script is distributed by the same terms as fvwm-themes itself.
213 See GNU General Public License for details.
215 =head1 BUGS
217 Report bugs to fvwm-themes-devel@lists.sourceforge.net.
219 =cut
221 # ==========================================================================