| 1 | package Catalyst::Blinker; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Catalyst::Blinker - show a little X11 blinker while Catalyst is reloading | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | I'm developing under Catalyst and am using the Catalyst test server that | ||
| 13 | is running on my local machine and is relaoding every time I'm saving changes | ||
| 14 | to the source tree. This is a cool feature, but I have to wait in front of the | ||
| 15 | browser until the application is loaded full and can answer http requests; if I hit | ||
| 16 | F5 in the browser before it is up and running I get a failure and have to hit it | ||
| 17 | again. I got tired of this and made this little module, that notifies me if it's | ||
| 18 | safe or not to operate the app. | ||
| 19 | |||
| 20 | The thing is very primitive. While Catalyst app is loading, it blinks yellow. If | ||
| 21 | it has loaded ok, it shows green light for a couple of seconds and disappears. | ||
| 22 | If there was a failure (syntax failure in my case most of the time), it shows red | ||
| 23 | light which doesn't go away until the problem is fixed. | ||
| 24 | |||
| 25 | Wasn't tested on anything other than ubuntu. Most probably wouldn't run on win32 | ||
| 26 | out of box, because the module expects pipes and forks to be working. | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | In your script/myapp.pl, add this, if you want blinker on by default: | ||
| 31 | |||
| 32 | use Catalyst::Blinker; | ||
| 33 | Catalyst::Blinker->start; | ||
| 34 | |||
| 35 | if you want it off by default, do this instead: | ||
| 36 | |||
| 37 | if ( $ENV{BLINKER} ) { | ||
| 38 | require Catalyst::Blinker; | ||
| 39 | Catalyst::Blinker->start; | ||
| 40 | } | ||
| 41 | |||
| 42 | before | ||
| 43 | |||
| 44 | Catalyst::ScriptRunner->run('MyApp', 'Server'); | ||
| 45 | |||
| 46 | In both cases, the module recognizes $ENV{BLINKER} and parses the passed options (see below), | ||
| 47 | f.ex. | ||
| 48 | |||
| 49 | $ export BLINKER=x=1600,y=-150 | ||
| 50 | |||
| 51 | =head1 API | ||
| 52 | |||
| 53 | =head2 start(@options) | ||
| 54 | |||
| 55 | Does the necessary hacks for a fork-based test catalyst server running on a X11 desktop. | ||
| 56 | Dies if there's no X11. | ||
| 57 | |||
| 58 | The modules does not do anything unless start() is called. It is safe to | ||
| 59 | use it everywhere. | ||
| 60 | |||
| 61 | =head2 options | ||
| 62 | |||
| 63 | =over | ||
| 64 | |||
| 65 | =item size INT | ||
| 66 | |||
| 67 | size of the blinker | ||
| 68 | |||
| 69 | =item x INT | ||
| 70 | |||
| 71 | Horizontal position of the blinker. If less than zero, the right desktop border is used. | ||
| 72 | |||
| 73 | =item y INT | ||
| 74 | |||
| 75 | Vertical position of the blinker. If less than zero, the bottom desktop border is used. | ||
| 76 | |||
| 77 | =back | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | our $VERSION = '1.0'; | ||
| 82 | |||
| 83 | my %blinkopt = ( | ||
| 84 | size => 20, | ||
| 85 | x => 0, | ||
| 86 | y => 0, | ||
| 87 | ); | ||
| 88 | |||
| 89 | sub start | ||
| 90 | { | ||
| 91 | if ( $ENV{BLINKER} ) { | ||
| 92 | # use as this: | ||
| 93 | # env BLINKER=size=200,y=-20,x=1024 | ||
| 94 | my @opt = ( $ENV{BLINKER} =~ /^(1|yes|default)$/ ) ? () | ||
| 95 | : (split /[,=]/, $ENV{BLINKER}); | ||
| 96 | die "bad options to env BLINKER, (@opt), has to be a hash\n" if @opt % 2; | ||
| 97 | my %opt = @opt; | ||
| 98 | for ( keys %opt ) { | ||
| 99 | $blinkopt{$_} = $opt{$_}, next if exists $blinkopt{$_}; | ||
| 100 | die "unknown blinker option '$_'\n"; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | # options | ||
| 105 | shift; | ||
| 106 | my %opt = ( %blinkopt, @_ ); | ||
| 107 | |||
| 108 | # use this if you're tired to hit F5 until it loads | ||
| 109 | require IO::Handle; | ||
| 110 | pipe(my $r, my $w); | ||
| 111 | autoflush $w, 1; | ||
| 112 | |||
| 113 | { | ||
| 114 | no warnings 'redefine'; | ||
| 115 | |||
| 116 | my $failed; | ||
| 117 | |||
| 118 | require Catalyst::Script::Server; | ||
| 119 | my $css_run = \&Catalyst::Script::Server::_run_application; | ||
| 120 | *Catalyst::Script::Server::_run_application = sub { | ||
| 121 | my $ret; | ||
| 122 | eval { $ret = $css_run->(@_); }; | ||
| 123 | if ( $@ ) { | ||
| 124 | $failed = 1; | ||
| 125 | print $w "fail\n"; | ||
| 126 | die $@; | ||
| 127 | } else { | ||
| 128 | return $ret; | ||
| 129 | } | ||
| 130 | }; | ||
| 131 | |||
| 132 | my $css_pla = \&Catalyst::Script::Server::_plack_loader_args; | ||
| 133 | *Catalyst::Script::Server::_plack_loader_args = sub { | ||
| 134 | my %args = $css_pla->(@_); | ||
| 135 | my $sr = delete $args{server_ready}; | ||
| 136 | $args{server_ready} = sub { | ||
| 137 | $failed = 0; | ||
| 138 | print $w "start\n"; | ||
| 139 | $sr ? $sr->(@_) : (); | ||
| 140 | }; | ||
| 141 | return %args; | ||
| 142 | }; | ||
| 143 | |||
| 144 | my $child = 0; | ||
| 145 | require Catalyst::Restarter::Forking; | ||
| 146 | my $crf_kc = \&Catalyst::Restarter::Forking::_kill_child; | ||
| 147 | *Catalyst::Restarter::Forking::_kill_child = sub { | ||
| 148 | print $w "stop\n" if not($failed) and $$ == $child; | ||
| 149 | $crf_kc->(@_) | ||
| 150 | }; | ||
| 151 | |||
| 152 | my $crf_fork = \&Catalyst::Restarter::Forking::_fork_and_start; | ||
| 153 | *Catalyst::Restarter::Forking::_fork_and_start = sub { | ||
| 154 | $child = $$; | ||
| 155 | $crf_fork->(@_); | ||
| 156 | }; | ||
| 157 | } | ||
| 158 | |||
| 159 | my $pid = fork; | ||
| 160 | die "fork error:$!" unless defined $pid; | ||
| 161 | if ( $pid ) { | ||
| 162 | close $r; | ||
| 163 | return; | ||
| 164 | } | ||
| 165 | |||
| 166 | # child here | ||
| 167 | eval "use Prima qw(Application);"; | ||
| 168 | die $@ if $@; | ||
| 169 | |||
| 170 | close $w; | ||
| 171 | my $size = $opt{size}; | ||
| 172 | my $center = $size / 2; | ||
| 173 | my $countdown_to_hide = 0; | ||
| 174 | my $circle = Prima::Image->new( | ||
| 175 | width => $size, | ||
| 176 | height => $size, | ||
| 177 | type => 1, | ||
| 178 | color => 0xffffff, | ||
| 179 | backColor => 0, | ||
| 180 | ); | ||
| 181 | $circle->begin_paint; | ||
| 182 | $circle->clear; | ||
| 183 | $circle->fill_ellipse($center,$center,$size-2,$size-2); | ||
| 184 | $circle->end_paint; | ||
| 185 | my $dx = ( $opt{x} <= 0 ) ? $::application->width-$size-2+$opt{x} : $opt{x}; | ||
| 186 | my $dy = ( $opt{y} <= 0 ) ? $::application->height-$size-2+$opt{y} : $opt{y}; | ||
| 187 | my $blinker = Prima::Widget->new( | ||
| 188 | origin => [ $dx, $dy ], | ||
| 189 | size => [ $size, $size ], | ||
| 190 | visible => 1, | ||
| 191 | syncPaint => 1, | ||
| 192 | backColor => cl::LightRed(), | ||
| 193 | selectable => 0, | ||
| 194 | shape => $circle, | ||
| 195 | buffered => 1, | ||
| 196 | onPaint => sub { | ||
| 197 | my ( $self, $canvas ) = @_; | ||
| 198 | $canvas->clear; | ||
| 199 | $canvas->lineWidth(4); | ||
| 200 | my ( $c, $x ) = ( 8, $size-2 ); | ||
| 201 | while ( $c < 16 ) { | ||
| 202 | $canvas->color($canvas->backColor & ( $c * 0x101010) ); | ||
| 203 | $canvas->ellipse($center,$center,$x,$x); | ||
| 204 | $canvas->lineWidth(2); | ||
| 205 | $x--; | ||
| 206 | $c++; | ||
| 207 | } | ||
| 208 | }, | ||
| 209 | ); | ||
| 210 | $blinker->bring_to_front; | ||
| 211 | my $blinkstate = 0; | ||
| 212 | my $blinkcolor = 16; | ||
| 213 | my $timer = Prima::Timer->new( | ||
| 214 | timeout => 100, | ||
| 215 | onTick => sub { | ||
| 216 | my $self = shift; | ||
| 217 | if ( $blinkcolor == 8 && $countdown_to_hide > 0 ) { | ||
| 218 | # green waits and hides the blinker | ||
| 219 | unless (--$countdown_to_hide) { | ||
| 220 | $self->stop; | ||
| 221 | $blinker->hide; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | my $c; | ||
| 226 | if ( $countdown_to_hide < 8 ) { | ||
| 227 | # normal up-down blinking | ||
| 228 | $blinkstate = 0 if ++$blinkstate > 15; | ||
| 229 | } else { | ||
| 230 | # green glows steadily, then nicely fades out | ||
| 231 | $blinkstate = 7; | ||
| 232 | } | ||
| 233 | |||
| 234 | $c = ( $blinkstate > 7 ) ? ( 15 - $blinkstate ) : $blinkstate; | ||
| 235 | $c = (( $c << 4 ) | 0x80) << $blinkcolor; | ||
| 236 | $c |= ( $c >> 8 ) if $c > 0x10000; # makes it yellow, as red doesn't blink at all | ||
| 237 | $blinker->backColor($c); | ||
| 238 | }, | ||
| 239 | ); | ||
| 240 | $timer->start; | ||
| 241 | my $reader = Prima::File->new( | ||
| 242 | file => $r, | ||
| 243 | mask => fe::Read(), | ||
| 244 | onRead => sub { | ||
| 245 | my $cmd = <$r>; | ||
| 246 | chomp $cmd; | ||
| 247 | if ( $cmd eq 'start' ) { | ||
| 248 | $blinkstate = 0; | ||
| 249 | $countdown_to_hide = 32; | ||
| 250 | $blinkcolor = 8; | ||
| 251 | } elsif ( $cmd eq 'stop') { | ||
| 252 | $timer->start; | ||
| 253 | $blinkstate = 0; | ||
| 254 | $blinkcolor = 16; | ||
| 255 | $blinker->backColor(0x808000); | ||
| 256 | $blinker->show; | ||
| 257 | $blinker->bring_to_front; | ||
| 258 | } elsif ( $cmd eq 'fail') { | ||
| 259 | $blinker->backColor(cl::LightRed()); | ||
| 260 | $blinker->show; | ||
| 261 | $blinker->bring_to_front; | ||
| 262 | $timer->stop; | ||
| 263 | } | ||
| 264 | }, | ||
| 265 | ); | ||
| 266 | run Prima; | ||
| 267 | exit; | ||
| 268 | } | ||
| 269 | |||
| 270 | 1; | ||
| 271 | |||
| 272 | =head1 AUTHOR | ||
| 273 | |||
| 274 | Dmitry Karasik <dmitry@karasik.eu.org> | ||
| 275 | |||
| 276 | =head1 LICENSE | ||
| 277 | |||
| 278 | Copyright (C) 2013, Novozymes. | ||
| 279 | |||
| 280 | =cut |