| 1 | #!/usr/bin/env perl |
| 2 | #MoonPhase-Applet (Harvie 2oo9) |
| 3 | |
| 4 | use strict; |
| 5 | use warnings; |
| 6 | use Astro::MoonPhase; |
| 7 | use Gtk2 -init; |
| 8 | |
| 9 | my $icons = '/usr/share/icons/moonphase-icons'; #directory with moon phase icons |
| 10 | |
| 11 | sub timediff { |
| 12 | my $t = $_[0]; |
| 13 | my $days = int($t/(60*60*24)); $t %=(60*60*24); |
| 14 | my $hours = int($t/(60*60)); $t %=(60*60); |
| 15 | my $mins = int($t/(60)); $t %=(60); |
| 16 | return $days."d ".$hours."h ".$mins."m ".$t."s"; |
| 17 | } |
| 18 | |
| 19 | |
| 20 | my $statusicon = Gtk2::StatusIcon->new(); |
| 21 | #$statusicon->signal_connect(activate => sub { Gtk2->main_quit; }); #exit on click... |
| 22 | |
| 23 | opendir(my $dir,$icons); |
| 24 | my @filenames = readdir($dir); |
| 25 | closedir($dir); |
| 26 | |
| 27 | @filenames = sort(@filenames); shift(@filenames); shift(@filenames); #remove . and .. |
| 28 | #@filenames = sort(sub {$b cmp $a}, @filenames); pop(@filenames); pop(@filenames); #reversed |
| 29 | |
| 30 | my @phase_name = ("NEW MOON", "FIRST QUARTER", "FULL MOON", "LAST QUARTER"); |
| 31 | |
| 32 | sub imgno { |
| 33 | my ($r) = @_; |
| 34 | return int(( (($r*@filenames)+.5)%@filenames )+.5); |
| 35 | } |
| 36 | |
| 37 | |
| 38 | if($ARGV[0] eq '--rotate') { |
| 39 | my $i = 0; |
| 40 | $SIG{'ALRM'} = sub { |
| 41 | $i=($i+10)%100; |
| 42 | my $img = "$icons/".$filenames[imgno($i/100)]; |
| 43 | my $inf = "$i%\t$img"; |
| 44 | $statusicon->set_from_file($img); |
| 45 | $statusicon->set_tooltip_text($inf); |
| 46 | print $inf."\n"; |
| 47 | alarm(1); |
| 48 | }; |
| 49 | alarm(1); |
| 50 | Gtk2->main; |
| 51 | die; |
| 52 | } |
| 53 | |
| 54 | $SIG{'ALRM'} = sub { |
| 55 | my $time = time(); #seconds_since_1970 |
| 56 | my ($MoonPhase, $MoonIllum, $MoonAge, $MoonDist, $MoonAng, $SunDist, $SunAng) = phase($time); |
| 57 | #$MoonPhase = ($MoonPhase+0.5)%1; |
| 58 | #my $imgno = int(( (($MoonPhase*@filenames)+.5)%@filenames )+.5); |
| 59 | my $imgno = imgno($MoonPhase); |
| 60 | my $img = $filenames[$imgno]; |
| 61 | |
| 62 | my $prediction = ''; |
| 63 | my ($phase, @times) = phaselist(time(), (time()+60*60*24*31)); |
| 64 | while (@times) { |
| 65 | my $phasetime = shift(@times); |
| 66 | $prediction .= "$phase_name[$phase] coming at:\n ".scalar(localtime($phasetime))." (".timediff($phasetime-$time)." remaining)\n"; |
| 67 | $phase = ($phase + 1) % 4; |
| 68 | } |
| 69 | |
| 70 | my $tooltip = "Astro::MoonPhase Applet (by harvie 2oo9)\n". |
| 71 | "Local time is: ".scalar(localtime($time))."\n\n". |
| 72 | "$prediction\n". |
| 73 | "MoonPhase = ".($MoonPhase * 100)." %\n". |
| 74 | "MoonIllum = ".($MoonIllum * 100)." %\n". |
| 75 | "MoonAge = $MoonAge days\n". |
| 76 | "MoonDist = $MoonDist km\n". |
| 77 | "MoonAng = $MoonAng degrees\n". |
| 78 | "SunDist = $SunDist km\n". |
| 79 | "SunAng = $SunAng degrees\n". |
| 80 | "Icon = $img ($imgno/".scalar(@filenames)." images total)\n\n". |
| 81 | "See man Astro::MoonPhase for explanation\n". |
| 82 | "Tip: You can make fullmoon gathering with friends each month.\n"; |
| 83 | $img = "$icons/$img"; |
| 84 | |
| 85 | print "\033[2J\033[0;0H$tooltip"; |
| 86 | |
| 87 | $statusicon->set_tooltip_text($tooltip); |
| 88 | $statusicon->set_from_file($img); |
| 89 | |
| 90 | alarm(1); |
| 91 | }; |
| 92 | alarm(1); |
| 93 | |
| 94 | Gtk2->main; |
| 95 | |
| 96 | |