summaryrefslogtreecommitdiff
path: root/src/ivyreplay
blob: 8d34b61faed67b67ed8f462ee48b4bc076330d85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
#!/usr/bin/perl


#=================================================================================
#
# M A I N
#
#=================================================================================

use Tk;
use Tk::Font;
use Tk::ErrorDialog;
use Tk::Dialog;
use Tk::ProgressBar;
use Time::HiRes qw(gettimeofday);
use Ivy;
use Carp;
use strict;
use Getopt::Long;
use Tk::CmdLine;
use vars qw/$opt_help $opt_b $opt_bus  
            $opt_size $opt_undersize  $opt_autostart
            $opt_repeat $opt_startregexp
            $opt_stopregexp $opt_timegranularity $opt_debug/;

# geometry
my $minW = 1024;
my $minH = 768;
my $smallsized = 0;
my $coef = 1;

my $ivy_port; # undef:=> default value is treated by ivy-perl
my $appname = 'IvyReplay';
$appname =~ s/ /_/g;


# replay
my $replay_current_t0;
my $replay_current_t;
my $replay_data_t0;
my $replay_data_t;
my $replay_maxgap = 0.2;  # sec
my $replay_regulpct = [50, 20]; # %
my $replay_factor = 1000; # msec
my $replay_current_factor = $replay_factor;

my $replay_speed = 1;
my $replay_runnable = 1;
my $replay_running = 0;
my $replay_time_granularity = 1;
my $replay_time_decimalplaces = 0;
my $replay_timer;
my $mw;
my $replay_text;
my $replay_repeat;
my %replay_msg;
my $replay_time;
my $replay_min_time;
my $replay_max_time;
my $replay_last_time;
my $replay_stepbystep;
my $replay_bg = 'gray75';
my $replay_fg = 'black';
my $replay_bg_orig;
my $replay_fg_orig;
my $replay_hour;


#----------------------------------------------------------------------------------
# command line options management
#----------------------------------------------------------------------------------

Tk::CmdLine::SetArguments(-font => '7x14');
Tk::CmdLine::SetArguments();

if (not GetOptions('-help', '-b=s', '-bus=s', '-size=s', '-undersize', 
         '-repeat', '-startregexp=s', '-debug', '-timegranularity=s', 
         '-autostart', '-stopregexp=s') or $opt_help) {
         
   print "\n";
   print "Usage: ivyreplay [-b[us] bus] [-help]\n";
   print "                 [-size size] [-undersize] [-autostart]\n";
   print "                 [-repeat] [-timegranularity float>0]\n";
   print "                 [-startregexp regexp] [-stopregexp regexp]\n";
   print "                 [standard X11 options...]\n";
   print "                 inputfile\n";
   print "\n";
   print "Options :\n";
   print "     -b <[addr]:port>   Ivy bus (\$IVYBUS by default)\n";
   print "     -size <VGA|SVGA|XGA|SXGA|UXGA>    Size of the main window\n";
   print "     -undersize    If set, slightly reduces the main window\n";
   print "     -autostart    If set, messages are replayed as soon as the\n";
   print "     -repeat       If set, repeat infinitely the replay sequence\n";
   print "     -timegranularity   Time granularity (in second) for replaying\n";
   print "                        messages (1 by default)\n";
   print "     -startregexp <regexp>  Regexp to match for starting replay\n";
   print "                                  ('^ClockStart\$'' by default)\n";
   print "     -stopregexp <regexp>   Regexp to match for stopping replay\n";
   print "                                  ('^ClockStop\$'' by default)\n";
   print "\n";
    
   exit ;
   
}
 
croak "Error : an input file is expected\n" unless @ARGV > 0;

$replay_repeat = $opt_repeat;
if ($opt_bus) {
   $ivy_port = $opt_bus;
} elsif ($opt_b) {
   $ivy_port = $opt_b;
}
my $title;
my $ivy_version = $Ivy::VERSION;
my $ivy_cvsrevision = 0;
my $ivy_versionstring = "v$ivy_version";
if ($ivy_version =~ s/Revision: (.*)//) {
   $ivy_version = $1;
   $ivy_cvsrevision = 1;
   $ivy_versionstring = "v$ivy_version (cvs revision)";
}
if ($ivy_port) {
   $title = "Ivyreplay ($ivy_port) - Ivy $ivy_versionstring";
} else {
   $title = "Ivyreplay (default port) - Ivy $ivy_versionstring";
}
$replay_time_granularity = $opt_timegranularity if $opt_timegranularity > 0;
if ($replay_time_granularity >= 1) {
   $replay_time_decimalplaces = 0;
} else {
   $replay_time_decimalplaces = $replay_time_granularity;
   $replay_time_decimalplaces =~ s/^\+?\d*\.//;
   $replay_time_decimalplaces = length($replay_time_decimalplaces);
}
$replay_hour = &replayTime();
   

#===================================
#
#   Size options

if($opt_size eq "UXGA" || $opt_size eq "1600") {
       $minW = 1600;
   $minH = 1200;
   $smallsized = 0;
   $coef = 1.2; 
   Tk::CmdLine::SetArguments(-font => '10x20');
   Tk::CmdLine::SetArguments();
} elsif ($opt_size eq "SXGA" || $opt_size eq "1280") {
   $minW = 1280;
   $minH = 1024;
   $smallsized = 0;
   $coef = 1.1; 
   Tk::CmdLine::SetArguments(-font => '8x13');
   Tk::CmdLine::SetArguments();
} elsif ($opt_size eq "XGA" || $opt_size eq "1024") {
       $minW = 1024;
   $minH = 768;
   $smallsized = 1;
   $coef = 1; 
   Tk::CmdLine::SetArguments(-font => '7x14');
   Tk::CmdLine::SetArguments();
} elsif ($opt_size eq "SVGA" || $opt_size eq "800") {
       $minW = 800;
   $minH = 600;
   $smallsized = 1;
   $coef = 0.64; 
   Tk::CmdLine::SetArguments(-font => '6x10');
   Tk::CmdLine::SetArguments();
} elsif ($opt_size eq "VGA" || $opt_size eq "640"){
       $minW = 640;
   $minH = 480;
   $smallsized = 1;
   $coef = 0.48;
   Tk::CmdLine::SetArguments(-font => '5x7');
   Tk::CmdLine::SetArguments();
}

if ($opt_undersize) {
   $minW -= 10;
   $minH -= 30;
}

#=================================================================================
#
# H M I
#
#=================================================================================
my $mw = MainWindow->new();

# Set default min size
$mw->geometry($minW."x".$minH);
$mw->minsize($minW, $minH);
$mw->title($title);

# Key-Tab binding is deactivated, because this event will be used by entries
# for completion functionality
$mw->bind('<Key-Tab>', sub {Tk->break});
my $focusedtext;
$mw->bind("Tk::Text", "<FocusIn>", [sub {
    &clearSearch() if $_[1] ne $focusedtext;
    $focusedtext = $_[1];}, Ev('W')]);


#----------------------------------------------------------------------------------
# Progress bar
#----------------------------------------------------------------------------------
my $tpl = $mw->Toplevel;
$tpl->Popup;
$tpl->raise($mw);
$tpl->title("");
$tpl->geometry("300x50");
my $progressbar = 
   $tpl->ProgressBar(-from => 0,
                      -length => 200,
                      -borderwidth => 2,
                      -colors => [ 0 => 'yellow'],
                      -relief => 'sunken',
                      -resolution => 0,
                      -anchor => 'w',
                      )->pack(-fill => 'both',
                               -expand => 1,
                               );
$progressbar->value(0);
$tpl->withdraw;



#=================================================================================
#
# Ivy initialisation and Ivy bindings
#
#=================================================================================

# init Ivy bus and start it.
Ivy->init(-loopMode => 'TK',
          -appName => $appname,
          -ivyBus => $ivy_port,
          -onDieFunc => [\&quit],
          );   
my $ivy = Ivy->new();
$ivy->start;


#=================================================================================
#
# Options and arguments test
#
#=================================================================================

&build();

# load input files
my $file = $ARGV[0];

if (not open(IN, $file)) {
   $mw->Tk::Error("Can't open file '$file' ($!)");
} else {
   &showProgressbar();
   my ($step, $timefound) = &stepsnumber;
   if ($timefound) {
      &loadfileForReplay($step);
   } else {
      $mw->Tk::Error("No time information in file '$file'. ".
      "Can't be replayed.");
   }
   &replayStart() if $opt_autostart;
   &hideProgressbar();
   close(IN);
}

MainLoop;

#==================================================================================
#
#  Functions 
#
#==================================================================================


#----------------------------------------------------------------------------------
# Functions related to input/output files
#----------------------------------------------------------------------------------

sub stepsnumber {

   my $step = 0;
   my $timefound;
   my $lc = 0;
   
   # get lines number and test if exists time field
   while(<IN>) {
      chomp;
      next if (/^applications=/ or /^(marker\d+)$/ or /^(messages_number=)/
               or /^\#/);
      my ($sender, $message) = split(/\s+/, $_, 2);
      if ($message =~ /^(\d[\d\.]+\d)\s+.*/) {
         $timefound = 1;
      }
      $lc++;
   }
   $step = int($lc/10);
   $progressbar->configure(-to => $step*10);
   seek(IN, 0, 0);
   return ($step, $timefound);

} # end stepsnumber

sub build {

    %replay_msg = ();
    $replay_time = undef;
    # build replay window
    my $ctrl_fm = $mw->Frame()->pack(-side => 'bottom', -pady => 5);
    $replay_text = $mw->Scrolled('Text',
                                           -scrollbars => 'e',
                                           -spacing1 => 2,
                                           -spacing2 => 0,
                                           -spacing3 => 2,
                                           )->pack(-fill => 'both',
                                                    -expand => 1,
                                                    -side => 'bottom');
    # colors
    $replay_bg_orig = $replay_text->cget(-background);
    $replay_fg_orig = $replay_text->cget(-foreground);
    &wheelmousebindings($replay_text);
    # build speed control buttons
    my $realspeedrate = 1;
    my $ctrl_fm1 = $ctrl_fm->Frame()->pack(-side => 'left', -padx => 10);
    $ctrl_fm1->Radiobutton(-text => "x0.1", -indicatoron => 0,
                           -height => 2,
                           -width => 4,
                          -value => 0.1,
                          -variable => \$replay_speed,
                          -command => sub { $realspeedrate = 0 },
                          -selectcolor => 'white')->pack(-side => 'left');
    $ctrl_fm1->Radiobutton(-text => "x0.5", -indicatoron => 0,
                           -height => 2,
                           -width => 4,
                          -value => 0.5,
                          -variable => \$replay_speed,
                          -command => sub { $realspeedrate = 0 },
                          -selectcolor => 'white')->pack(-side => 'left');
    $ctrl_fm1->Radiobutton(-text => "x1", -indicatoron => 0,
                           -height => 2,
                           -width => 4,
                          -value => 1,
                          -variable => \$replay_speed,
                          -command => sub {
                              return if $realspeedrate == 1;
                              $realspeedrate = 1;
                              &regulationReset();
                          },
                          -selectcolor => 'white')->pack(-side => 'left');
    $ctrl_fm1->Radiobutton(-text => "x2", -indicatoron => 0,
                           -height => 2,
                           -width => 4,
                          -value => 2,
                          -variable => \$replay_speed,
                          -command => sub { $realspeedrate = 0 },
                          -selectcolor => 'white')->pack(-side => 'left');
    $ctrl_fm1->Radiobutton(-text => "x5", -indicatoron => 0,
                           -height => 2,
                           -width => 4,
                          -value => 5,
                          -variable => \$replay_speed,
                          -command => sub { $realspeedrate = 0 },
                          -selectcolor => 'white')->pack(-side => 'left');
    $ctrl_fm1->Radiobutton(-text => "x10", -indicatoron => 0,
                           -height => 2,
                           -width => 4,
                           -value => 10,
                           -variable => \$replay_speed,
                          -command => sub { $realspeedrate = 0 },
                           -selectcolor => 'white')->pack(-side => 'left');
    # build hour label
    my $ctrl_fm2 = $ctrl_fm->Frame()->pack(-side => 'left', -padx => 10);
    my $hour_lab = $ctrl_fm2->Label(-borderwidth => 1, -relief => 'ridge',
                                     -height => 2,
                                     -width => ($replay_time_decimalplaces > 0) ?
                                         10 + $replay_time_decimalplaces : 9,
                                     -textvariable => \$replay_hour,
                                     )->pack(-side => 'left');

    # build control buttons
    my $ctrl_fm3 = $ctrl_fm->Frame()->pack(-side => 'left', -padx => 10);
    $ctrl_fm3->Radiobutton(-text => "Play", -indicatoron => 0,
                           -width => 6,
                           -height => 2,
                           -value => 0,
                           -command => \&replayStart,
                           -variable => \$replay_runnable,
                           -selectcolor => 'white')->pack(-side => 'left');
    $ctrl_fm3->Radiobutton(-text => "Pause", -indicatoron => 0,
                           -width => 6,
                           -height => 2,
                           -value => 1,
                           -command => \&replayStop,
                           -variable => \$replay_runnable,
                           -selectcolor => 'white')->pack(-side => 'left');
    # build loop and step by step checkbuttons
    my $ctrl_fm4 = 
      $ctrl_fm->Frame(-relief => 'ridge', -borderwidth => 1
                     )->pack(-side => 'left', -padx => 10, -fill => 'y',
                                           -expand => 1);
    $ctrl_fm4->Checkbutton(-text => "Repeat",
                           -variable => \$replay_repeat,
                           )->pack(-side => 'left');
    
    $ctrl_fm4->Checkbutton(-text => "Step by\nstep",
                           -variable => \$replay_stepbystep,
                           -command => \&replayStop,
                            )->pack(-side => 'left', -padx => 10);    
   
    # build close button
    $ctrl_fm->Button(-text => "Close",
           -command => \&replayClose,
           -height => 1)->pack(-side => 'left', -padx => 10, -fill => 'y',
                               -expand => 1);
    $mw->update;
    $mw->minsize($mw->width, $mw->height);

} # end build


sub loadfileForReplay {

    my $step = shift;
    my $line = 0;
    
    # display messsages to replay
    my ($sender, $time, $message);
    while(<IN>) {
      chomp;
      next if /^\#/ or /^applications=/ or /^messages_number=/ 
         or /^\s*$/ or /^(marker\d+)$/;
           ($sender, $time, $message) = split(/\s+/, $_, 3);
      if ($replay_time_granularity >= 1) {
          $time = int($time);
      } else {
          $time = sprintf("%.".$replay_time_decimalplaces."f", $time);
      }
      if (defined $replay_max_time) {
          $replay_max_time = $time if $time > $replay_max_time;
      } else {
          $replay_max_time = $time;
      }
      if (defined $replay_min_time) {
          $replay_min_time = $time if $time < $replay_min_time;
      } else {
          $replay_min_time = $time;
      }
      $line++;
      $message =~ s/^\"//;
      $message =~ s/\"$//;
      push(@{$replay_msg{$time}}, $message);
      $replay_text->insert('end', &replayTime($time)." ".$message."\n", $time);
      # when user click on a message the begin time changes.
      $replay_text->tagBind($time, '<1>', [sub {
          my $ti = $_[1];
          my $replay_was_running;
          if ($replay_running) {
            $replay_was_running = 1;
            &replayStop;
          }
          $replay_text->tagConfigure($replay_last_time,
                           -foreground => $replay_fg_orig,
                           -background => $replay_bg_orig)
            if defined $replay_last_time;
          $replay_text->tagConfigure($ti,
                           -foreground => $replay_fg,
                           -background => $replay_bg);
          $replay_last_time = $ti;
          $replay_hour = &replayTime($ti);
          $replay_time = $ti;
          &regulationReset();          
          &replayStart if $replay_was_running;
      }, $time]);
      &setProgressbar($line, $step);
    }
    $mw->raise;
    $replay_time = $replay_min_time;
    $replay_text->configure(-state => 'disabled');
    # ivy bindings
    $ivy->bindRegexp($opt_startregexp, [\&replayStart]);
    $ivy->bindRegexp($opt_stopregexp, [\&replayStop]);
    

} # end loadfileForReplay



sub showProgressbar {

    $progressbar->value(0);
    $progressbar->toplevel->deiconify;
    $progressbar->toplevel->raise($mw);

} # end showProgressbar


sub hideProgressbar {
    
    $progressbar->toplevel->withdraw;
    
} # end hideProgressbar


sub setProgressbar {

    my ($line, $step) = @_;
    $progressbar->value($line);
    $progressbar->toplevel->raise($mw);
    return unless defined $step;
    $progressbar->update if ($step == 0 or $line % $step == 0);

} # end setProgressbar


#----------------------------------------------------------------------------------
# Functions related to replay 
#----------------------------------------------------------------------------------
sub replayStart {

   my $loopflag = ($_[0] == 1) ? 1 : undef;
   $replay_data_t0 = $replay_time unless defined $replay_data_t0;
   $replay_current_t0 = gettimeofday() unless defined $replay_current_t0;
   my $t = $replay_text;
   return if $replay_running and not $loopflag;
   return if $loopflag and not $replay_running;
   $replay_runnable = 0;
   $replay_running = 1;
   if ($replay_time > $replay_max_time) {
      if ($replay_repeat == 1) {
         $replay_time = $replay_min_time;
      } else {
         $t->tagConfigure($replay_last_time,
                           -foreground => $replay_fg_orig,
                           -background => $replay_bg_orig)
            if defined $replay_last_time;
         $replay_running = 0;
         $replay_runnable = 1;
         return;
      }
   }
   $replay_hour = &replayTime($replay_time);
   my $data_dt = $replay_time - $replay_data_t0;
   if (defined $replay_msg{$replay_time}) {
      for my $msg (@{$replay_msg{$replay_time}}) {
         $ivy->sendMsgs($msg);
         $t->tagConfigure($replay_last_time,
                           -foreground => $replay_fg_orig,
                           -background => $replay_bg_orig)
            if defined $replay_last_time;
         $t->tagConfigure($replay_time,
                           -foreground => $replay_fg,
                           -background => $replay_bg);
      }
      $replay_last_time = $replay_time;
      my $i = $t->tagRanges($replay_time);
      $t->see($i) if defined $i;
   }
   $replay_time += $replay_time_granularity;
   $replay_time = sprintf("%.".$replay_time_decimalplaces."f", $replay_time) if
      $replay_time_decimalplaces > 0;
   # step by step mode
   if ($replay_stepbystep) {
      while (not defined $replay_msg{$replay_time}) {
         $replay_time += $replay_time_granularity;
         $replay_time = sprintf("%.".$replay_time_decimalplaces."f", $replay_time) if
         $replay_time_decimalplaces > 0;
         last if $replay_time > $replay_max_time;
      }
      $replay_running = 0;
      $replay_runnable = 1;
      
   # continuous mode
   } else {
      my $dt = gettimeofday() - $replay_current_t0;
      my $dt2 = sprintf("%.2f", $dt);
      my $d = sprintf("%.3f", $dt-$data_dt);
      
      # if speed rate != 1, time regulation is deactivated
      if ($replay_speed != 1) {
         $replay_current_factor = $replay_factor;
         print "replay_speed=$replay_speed => time regulation deactivated\n" 
            if $opt_debug;
      
      # if replay is running behind schedule (retard)
      } elsif ($dt > $data_dt + $replay_maxgap and 
                  $replay_current_factor <= $replay_factor) {
         $replay_current_factor *= (1 - $replay_regulpct->[0]/100);
         print "data_dt=$data_dt dt=$dt2 delay=$d [--] ".
               "replay_factor=$replay_current_factor\n" if $opt_debug;
         
      # if replay is getting ahead of schedule (avance)
      } elsif ($dt < $data_dt - $replay_maxgap and 
                  $replay_current_factor >= $replay_factor) {
         $replay_current_factor *= (1 + $replay_regulpct->[1]/100);
         print "data_dt=$data_dt dt=$dt2 delay=$d [++] ".
               "replay_factor=$replay_current_factor\n" if $opt_debug;
         
      # if replay is on time
      } else {
         print "data_dt=$data_dt dt=$dt2 delay=$d\n" if $opt_debug;
         $replay_current_factor = $replay_factor;
      }
   
      $replay_timer = 
         $mw->after($replay_current_factor*$replay_time_granularity/$replay_speed,
            [\&replayStart, 1]);
   }
    
} # end replayStart

sub replayStop {

    $replay_running = 0;
    $replay_runnable = 1;
    $replay_text->afterCancel($replay_timer) if defined $replay_timer;
    &regulationReset();
    
} # end replayStop

sub replayClose {

    $ivy->bindRegexp($opt_startregexp);
    $ivy->bindRegexp($opt_stopregexp);
    &replayStop();
    $replay_text->after(400, sub {$mw->destroy});
    $replay_speed = 1;
    $replay_repeat = $opt_repeat;
    $replay_stepbystep = undef;
    $replay_hour = '--:--:--';
    
} # end replayClose

sub replayTime {

    my $time = shift;
    if (defined $time) {
      my ($s, $m, $h) = localtime($time);
      if ($replay_time_decimalplaces > 0) {
          my $dec =
         substr($time, -$replay_time_decimalplaces, $replay_time_decimalplaces);
          return sprintf("%02d:%02d:%02d.%s", $h, $m, $s, $dec);
      } else {
          return sprintf("%02d:%02d:%02d", $h, $m, $s);
      }
    } elsif ($replay_time_decimalplaces > 0) {
      return "--:--:--."."-" x $replay_time_decimalplaces;
    } else {
      return "--:--:--";
    }
   
} # end replayTime

#----------------------------------------------------------------------------------
# General functions
#----------------------------------------------------------------------------------

sub regulationReset {

   $replay_data_t0 = undef;
   $replay_current_t0 = undef;

} # end regulationReset


sub wheelmousebindings {

    my $w = shift;
    my $count = shift;
    my $count = 3 unless $count > 0;
    $w->bind('<Control-ButtonPress-4>', sub {$w->yview('scroll', -1, 'page')});
    $w->bind('<Shift-ButtonPress-4>', sub {$w->yview('scroll', -1, 'unit')});
    $w->bind('<ButtonPress-4>', sub {$w->yview('scroll', -$count, 'unit')});
    
    $w->bind('<Control-ButtonPress-5>', sub {$w->yview('scroll', 1, 'page')});
    $w->bind('<Shift-ButtonPress-5>', sub {$w->yview('scroll', 1, 'unit')});
    $w->bind('<ButtonPress-5>', sub {$w->yview('scroll', $count, 'unit')});

} # end wheelmousebindings


sub quit {

    print "Quit\n";
    exit;
    
} # end quit


__END__

=head1 NAME

ivyreplay - a graphical application for replaying Ivy messages


=head1 SYNOPSIS

B<ivyreplay> [B<-b> ivybus] [B<-help>]
          [B<-size> window size] [B<-undersize>]
          [B<-autostart>] [B<-repeat>]
          [B<-timegranularity> float>0]
          [B<-startregexp> regexp]
          [B<-stopregexp> regexp]
          [standard X11 options...]
          inputfile

=head1 DESCRIPTION

IvyReplay is dedicated to replay Ivy messages. Input file must have been generated with ivymon v1.6 or later (it must contain time information).


=head1 OPTIONS

=over

=item B<-b> ip:port

Set the bus domain and port number to be used. Use $IVYBUS variable if defined. Default is 127.255.255.255:2010.

=item B<-help>

Get some help


=item B<-size> window size

Set the size of the IvyMon window. Can be VGA or 640, SVGA or 800, XGA or 1024, SXGA or 1280, UXGA or 1600. Default is XGA (1024x768).

=item B<-undersize>

Slightly reduce the IvyMon window to fit it in screen with borders of the window manager. Option not set by default.


=item B<-repeat>

If set, the replay sequence will be repetitive. 

=item B<-timegranularity> float>0

Specify the time granularity in second for replaying messages. Set to 1 by default.

=item B<-startregexp> regexp

Specify the ivy regular expression to match in order to start the replay sequence. Default is '^ClockStart'. You can do manually the same using the B<Play> button of the Replay window.

=item B<-stopregexp> regexp

Specify the ivy regular expression to match in order to stop the replay sequence. Default is '^ClockStop'. You can do manually the same using the B<Pause> button of the Replay window.


=back


=head1 EXAMPLE

ivyreplay -b 10.192.36.255:3456 -repeat -startregexp '^ReplayOn' -stopregexp '^ReplayOff' /my/dir/log.ivy


=head1 AUTHORS

Daniel Etienne <etienne@cena.fr>