#!/usr/bin/env perl # fancy-xearth --- wrapper to set xearth parameters in pleasing fashion # Author: Noah Friedman # Created: 1996-03-05; rewritten in perl 2011-04-26 # Public domain # $Id: fancy-xearth,v 1.19 2016/10/28 22:08:01 friedman Exp $ # Commentary: # Code: $^W = 1; # enable warnings use FindBin; use lib "$FindBin::Bin/../../lib/perl"; use lib "$ENV{HOME}/lib/perl"; use NF::FileUtil qw(:open); use POSIX; use strict; my $ent_cursor = ' #define ent_width 24 #define ent_height 24 #define ent_x_hot 21 #define ent_y_hot 0 static char ent_bits[] = { 0x00, 0x00, 0x38, 0x00, 0x00, 0x24, 0x00, 0xc0, 0x23, 0x00, 0x40, 0x10, 0x00, 0x40, 0x08, 0x00, 0x40, 0x04, 0x00, 0x24, 0x02, 0x00, 0x1a, 0x02, 0x00, 0x11, 0x06, 0x80, 0x70, 0x0a, 0x40, 0x48, 0x10, 0x20, 0x28, 0x10, 0x10, 0x10, 0x08, 0x08, 0x03, 0x04, 0x84, 0x04, 0x02, 0x42, 0x04, 0x01, 0x21, 0x82, 0x00, 0x1f, 0x41, 0x00, 0x80, 0x30, 0x00, 0x00, 0x09, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };'; my $ent_mask = ' #define ent_width 24 #define ent_height 24 #define ent_x_hot -1 #define ent_y_hot -1 static char ent_bits[] = { 0x00, 0x00, 0x38, 0x00, 0x00, 0x3c, 0x00, 0xc0, 0x3f, 0x00, 0xc0, 0x1f, 0x00, 0xc0, 0x0f, 0x00, 0xc0, 0x07, 0x00, 0xe4, 0x03, 0x00, 0xfe, 0x03, 0x00, 0xff, 0x07, 0x80, 0xff, 0x0f, 0xc0, 0xcf, 0x1f, 0xe0, 0xef, 0x1f, 0xf0, 0xff, 0x0f, 0xf8, 0xff, 0x07, 0xfc, 0xfc, 0x03, 0x7e, 0xfc, 0x01, 0x3f, 0xfe, 0x00, 0x1f, 0x7f, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };'; sub set_cursor { my $curs_file = "/tmp/ent-d.xbm$$"; my $fh = xsysopen ($curs_file, "w"); (my $text = $ent_cursor) =~ s/^\s+//mg; print $fh $text; close ($fh); my $mask_file = "/tmp/ent-m.xbm$$"; $fh = xsysopen ($mask_file, "w"); ($text = $ent_mask) =~ s/^\s+//mg; print $fh $text; close ($fh); system ("xsetroot", "-cursor", $curs_file, $mask_file); unlink ($curs_file); unlink ($mask_file); } sub gcd # greatest common divisor { my ($x, $y) = (shift, shift); while ($x > 0 && $y > 0) { my ($min, $max) = sort { $a <=> $b } ($x, $y); return $min if ($max % $min == 0); $x = $max - $min; $y = $min; } return 0; } my %xrdb; sub xrdb_symbol_value { return unless @_; unless (%xrdb) { local $_ = `xrdb -symbols -screen`; s/^-D//mg; s/"//mg; map { my ($key, $val) = split (/=/, $_, 2); $xrdb{lc $key} = $val if $val; } split (/[\r\n]+/, $_); } my $sym = lc shift; return $xrdb{$sym} if exists $xrdb{$sym}; return; } sub aspect_ratio { my ($w, $h) = @_; return $w/$h unless wantarray; my $d = gcd ($w, $h); return ($w/$d, $h/$d); } sub xplanet { my $width = xrdb_symbol_value ('width'); my $height = xrdb_symbol_value ('height'); my $aspect = aspect_ratio ($width, $height); # Make initial offset of center of planet one quarter of the average of # width and height. my $offset = int (($width + $height) / 8); my $height_pct = 13; # Inititally use 13% of height $height_pct = $height_pct * $aspect if ($width < $height); # But adjust by aspect ratio if width smaller. # Make sure that center offset leaves the edge of the planet at least 150 # pixels from the top and left edges of the display. my $radius = int ($height_pct * $height / 100); my $edge = $offset - $radius; $offset += (150 - $edge) if $edge < 150; my @args = ("-config", "default", "-verbosity", "-1", "-searchdir", "$ENV{HOME}/etc/images/xplanet", "-center", "+$offset+$offset", "-radius", "$height_pct", "-range", "6", "-base_magnitude", "12", "-light_time", "-vroot", "-north", "body", #"-latitude", "21", #"-localtime", "8", # center over 8am local time "-latitude", "40", "-longitude", "300", # center over fixed point ); if (@_ && $_[0] eq '-once') { shift; push @args, (qw(-num_times 1)); } else { push @args, (qw(-wait 3600 -fork)); } push @args, @_; #print "xplanet @args\n"; system ("xplanet", @args); } sub xearth { my $width = xrdb_symbol_value ('width'); my $height = xrdb_symbol_value ('height'); my $mag = 0.26; my $minsize = $width < $height ? $width : $height; my $radius = $mag * $minsize / 2; my $offset = $minsize / 3; my $xshift = ($width / 2) - $offset; my $yshift = ($height / 2) - $offset; my @args = ("-nolabel", "-stars", "-onepix", "-nomarkers", "-night", "15", "-wait", "3600", "-nice", "19", "-mag", "$mag", "-shift", "-$xshift,-$yshift", "-pos", "sunrel,24.64,41.3", "-gamma", "1.0", "-proj", "orthographic", "-bigstars", "20", ); push @args, (qw(-fork)) unless (@_ && $_[0] eq '-once'); push @args, @_; system ("xearth", @args); } sub main { set_cursor (); if (@_ && exists { xearth => 1, xplanet => 1 }->{$_[0]}) { no strict qw(refs); my $fn = shift; &$fn (@_); } else { xplanet (@_); } } main (@ARGV); # eof