summaryrefslogtreecommitdiff
path: root/src/ivycontrolpanel
blob: fbf732599c61fa669901ec82c666432a21fbb223 (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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
#!/usr/bin/perl
#
#      ivycontrolpanel, an interface for controlling ivy agents
# 
#      Authors: created by Michelle Jacomi
#               rewritten by Daniel Etienne <etienne@cena.fr>
#
#

use strict;
use Carp;

# where you may find bitmap files
use lib "/usr/share/ivycontrolpanel";
# where you may find the IvyIO and Agent modules
use lib "/usr/lib/ivycontrolpanel";
#use lib ".";
# where you may find the FugueConfig module
use lib "/usr/lib/ivylaunch";

# Tk classes
use Tk;
use Tk::DialogBox;
use Tk::LabFrame;
use Getopt::Long;
# local modules
use IvyIO;
use FugueConfig;
use Agent;

use vars qw(%opt);
#=================================================================================
#
#    M A I N
#
#=================================================================================

#---------------------------------------------------------------------------------
#
# variables definition
#
#---------------------------------------------------------------------------------

# window size
my $width = 1024; 
my $height = 768; 
my $smallsized = 0;
my $coef = 1;

# fonts spec
my @fontspec34 = (-font =>
	  '-b&h-lucida-bold-r-normal-sans-34-240-100-100-p-216-iso8859-1');
my @fontspec24 = (-font =>
      '-b&h-lucida-bold-i-normal-sans-24-240-100-100-p-216-iso8859-1');
my @fontspec20 = (-font =>
      '-b&h-lucida-bold-r-normal-sans-20-140-100-100-p-127-iso8859-1');
my @fontspec17 = (-font =>
      '-adobe-helvetica-bold-r-normal--17-120-100-100-p-92-iso10646-1');

# colors spec
my $darkbg = '#1E161B';
my $bg = '#5C5655';
my $hlbg = '#7CC452';
my $fg = '#FCFAFC';
my $selcolor = 'yellow';
my $pbcolor = '#c9b900000000';
my $pingThreshold = 20;

# traffic rate spec
my @rate = (-20..-1, -0.5, 0, 0.5, 1..20);
my %rate2index;
for (my $i=0; $i<@rate; $i++) {
    $rate2index{$rate[$i]} = $i;
}

# flags
my $isplaying;
my $wasplayingBeforeSettingTime;
my $wasplayingBeforeResettingRate;
my $settingtime;

# misc
my $selectedtimelabel;
my $ivylaunch_agent;

# ivy version
my $ivy_version = $Ivy::VERSION;
my $ivy_versionstring = "v$ivy_version";
my $ivy_cvsrevision = 0;
if ($ivy_version =~ s/Revision: (.*)//) {
   $ivy_version = $1;
   $ivy_cvsrevision = 1;
   $ivy_versionstring = "v$ivy_version (cvs revision)";
}
my $newapi4_18 = 1 
   if (($ivy_cvsrevision == 1 and $ivy_version ge 1.40) or
      ($ivy_cvsrevision == 0 and $ivy_version ge 4.18));
#---------------------------------------------------------------------------------
#
# parse and test options
#
#---------------------------------------------------------------------------------

Getopt::Long::Configure('pass_through');
&usage 
   unless Getopt::Long::GetOptions(\%opt, 'b=s', 'nocursor', 
                                   'help', 'smallsize', 
                                   'position=s','pingthreshold=s');
&usage(1) if $opt{help};

my ($fuguefile) = @ARGV;


#---------------------------------------------------------------------------------
#
# build the mainwindow
#
#---------------------------------------------------------------------------------
if ($opt{smallsize}) {
	$width = 640; 
	$height = 480; 
	$smallsized = 1;
	$coef = 0.620;
	
	@fontspec34 = (-font =>
      '-b&h-lucida-bold-r-normal-sans-20-140-100-100-p-127-iso8859-1');
	@fontspec20 = (-font =>
      '-b&h-lucida-bold-r-normal-sans-12-120-75-75-p-79-iso8859-1');
	@fontspec24 = (-font =>
      '-b&h-lucida-bold-i-normal-sans-15-150-100-100-p-216-iso8859-1');
	@fontspec17 = (-font =>
     '-adobe-helvetica-medium-r-normal--11-80-100-100-p-60-iso10646-1');
}

if ($opt{pingthreshold}) {
	$pingThreshold = $opt{pingthreshold};
}

my $mw = MainWindow->new (-bg => 'black');

#---------------------------------------------------------------------------------
#
# init ivy 
#
#---------------------------------------------------------------------------------

if ($newapi4_18) {
   IvyIO::init("ivycontrolpanel", $opt{b}, 
               \&connected, \&disconnected, \&pingcb, $mw);
} else {
   IvyIO::init("ivycontrolpanel", $opt{b}, 
               \&connected, \&disconnected, undef, $mw);
}

if ($opt{nocursor}) {
   open(CURSOR, ">/tmp/cursor-ivybanner");
   print CURSOR "#define cursor_width 1\n";
   print CURSOR "#define cursor_height 1\n";
   print CURSOR "#define cursor_x_hot 0\n";
   print CURSOR "#define cursor_y_hot 0 \n";
   print CURSOR "static char cursor_bits[] = { 0x20};\n";
   close(CURSOR);
   $mw->configure(-cursor => ['@'.'/tmp/cursor-ivybanner',
         '/tmp/cursor-ivybanner',
         'black', 'black']);
   unlink("/tmp/cursor-ivybanner");
} else {
   $mw->configure(-cursor => 'circle');
}
$mw->geometry ($width."x".$height.$opt{position});
$mw->title ('ivycontrolpanel - Ivy '.$ivy_versionstring);
$mw->client ('ivycontrolpanel');
$mw->resizable(0,0);

# geometry variables
my $dxleft = 20*$coef;
my $dxright = 20*$coef;
my $dybot = 90*$coef;
my $dytop = 20*$coef;
my $ddy = 10*$coef;
my $y;
my $x;
my $btn_h = $dybot - 3*$ddy;
my $btn_w = $btn_h;
my $fm_w = $btn_w + 10*$coef;
my $fm_h = $btn_h + 10*$coef;

# widgets attributes
my @frameattr = (-relief => 'flat',
                  -highlightthickness => 3*$coef,
                  -highlightbackground => $hlbg,
                  -highlightcolor => $hlbg,
                  -height => $height - $dytop - $dybot,
                  -width => $width - $dxleft - $dxright,
                  -background => $bg);
my @labelattr = (-width => 2, -height => 1,
                  @fontspec34,
                  -relief => 'flat',
                  -highlightthickness => 0,
                  -background => $bg,
                  -foreground => $fg);
my @buttonattr = (@labelattr,
                    -borderwidth => 1,
                    -highlightbackground => $hlbg,
                    -highlightthickness => 3*$coef,
                    -width => $btn_w, -height => $btn_h,
                    -activebackground => $bg,
                    -activeforeground => $fg);

my @cbuttonattr = (-relief => 'flat',
                     -highlightthickness => 0,
                     -background => $darkbg,
                     -foreground => $fg,
                     @fontspec20,
                     -activeforeground => $fg,
                     -activebackground => $bg,
                     -selectcolor => $selcolor);
 
my $fm1 = $mw->Frame(-background => $darkbg)->pack(-expand => 1, 
                                                   -fill => 'both');


$x = $dxleft;
$y = $height - $dybot + $ddy;

# build information fields
#---------------------------------------------------------------------------------
my $fm2 = $fm1->Frame(@frameattr,
		      -background => $darkbg)->place(-x => $dxleft, 
                                          '-y' => $dytop);


$fm1->Label(-borderwidth => 0,
            -relief => 'flat',
            -highlightthickness => 0,
            -background => $darkbg,
            -foreground => $bg,
            -text => ' [ Bus ]  '.(($opt{b}) ? $opt{b} : 'default').'     '.
            '[ File ]  '.(($fuguefile) ? $fuguefile : 'none').'  ',
            @fontspec17,
            -foreground => $hlbg)->place(-x => $dxleft+20*$coef, '-y' => $dytop-10);

# build traffic rate frame
#---------------------------------------------------------------------------------
my $prevbmp = $mw->Bitmap('prev', -file => Tk::findINC('prev.bmp'),
                          -background => $bg,
                          -foreground => $hlbg);
my $nextbmp = $mw->Bitmap('next', -file => Tk::findINC('next.bmp'),
                          -background => $bg,
                          -foreground => $hlbg);

my $btn_prev = $fm1->Button(@buttonattr,
                            -image => $prevbmp,
                            )->place(-x => $x, '-y' => $y );

$x += $btn_w + 15*$coef;
my $fm_lab = $fm1->Frame(@frameattr,
                         -width => $fm_w,
                         -height => $fm_h)->place(-x => $x, '-y' => $y);
my $lab_rate = $fm_lab->Label(@labelattr,
                              -anchor => 'center',
                              -text => 1)->place(-x => 7*$coef, '-y' => 10*$coef);

$x += $fm_w + 5*$coef;
my $btn_next = $fm1->Button(@buttonattr,
                            -image => $nextbmp,
                            )->place(-x => $x, '-y' => $y);
$x += $btn_w + 15*$coef;

&setloopcommand($btn_prev, \&decrease_rate);
&setloopcommand($btn_next, \&increase_rate);


$lab_rate->bind('<ButtonPress>', [sub {
    &display_rate(1);
    IvyIO::send_rate(1);

}]);


# build hour frame
#---------------------------------------------------------------------------------
$x += 90*$coef;
my $xi;

# next/prev buttons
my $btn_tprev = 
   $fm1->Button(@buttonattr,
                -image => $prevbmp,
                )->place(-x => $x-$fm_w-5, '-y' => $y + 80*$coef);

my $fm_lab = $fm1->Frame(@frameattr,
                         -width => 3*$fm_w,
                         -height => $fm_h)->place(-x => $x, '-y' => $y);
# separators
$xi = -13*$coef ;
for (my $i=0; $i<2; $i++) {
    $xi += $fm_w;
    $fm_lab->Label(@labelattr,
		   -width => 0,
		   -anchor => 'center',
		   -text => ':')->place(-x => $xi, '-y' => $ddy);
}
# hour, minutes, seconds
$xi = 5*$coef;
my $lab_hour = $fm_lab->Label(@labelattr,
			      -anchor => 'center',
			      -text => '00')->place(-x => $xi, '-y' => $ddy);
$xi += $fm_w;
my $lab_min = $fm_lab->Label(@labelattr,
			     -anchor => 'center',
			     -text => '00')->place(-x => $xi, '-y' => 10*$coef);
$xi += $fm_w;
my $lab_sec = $fm_lab->Label(@labelattr,
			     -anchor => 'center',
			     -text => '00')->place(-x => $xi, '-y' => 10*$coef);

$lab_hour->bind('<1>', [\&settime, 0]);
$lab_min->bind('<1>', [\&settime, 1]);
$lab_sec->bind('<1>', [\&settime, 2]);

$x += 3*$fm_w + 10*$coef;

my $btn_tnext = $fm1->Button(@buttonattr,
			    -image => $nextbmp,
			    )->place(-x => $x-5, '-y' => $y + 80*$coef);



IvyIO::bind_for_clock_and_rate_event(sub {
    my ($h, $m, $s, $rate) = @_;
    #print "bind_for_clock_and_rate_event time=$h:$m:$s rate=$rate\n";
    &display_play unless $isplaying;
    &display_time($lab_hour, $h);
    &display_time($lab_min, $m);
    &display_time($lab_sec, $s);
    &display_rate($rate);
});

# build traffic control frame
#---------------------------------------------------------------------------------
$x += 90*$coef;
my $playbmp = $mw->Bitmap('play', -file => Tk::findINC('play.bmp'),
			  -background => $bg,
			  -foreground => $hlbg);
my $pausebmp = $mw->Bitmap('pause', -file => Tk::findINC('pause.bmp'),
			  -background => $bg,
			  -foreground => $hlbg);
my @buttonattr = (@buttonattr,
		   -width => $btn_w);
my @pushedbuttonattr = (@buttonattr,
			-highlightbackground => 'red',
			-foreground => 'red');

my $playbtn = $fm1->Button(@buttonattr,
			   -image => $playbmp,
			   )->place(-x => $x, '-y' => $y);

$x += $btn_w + 20*$coef;
my $pausebtn = $fm1->Button(@buttonattr,
			    -image => $pausebmp,
			    )->place(-x => $x, '-y' => $y );
$x += $btn_w + 20*$coef;

$playbtn->configure(-command => \&play);

$pausebtn->configure(-command => \&pause);

IvyIO::bind_for_play_event(\&display_play);
IvyIO::bind_for_pause_event(\&display_pause);

# Lower button
#---------------------------------------------------------------------------------

my $lowerbmp = $mw->Bitmap('lower', -file => Tk::findINC('lower.bmp'),
                         -background => $bg,
                         -foreground => $hlbg);

$x = $width - $dxright/2 - $fm_w - 90*$coef;

my $button_lower = $fm1->Button(
                               @buttonattr,
                               -image => $lowerbmp,
                              )->place(-x => $x, '-y' => $y );

$button_lower->configure( -command => sub { $mw->lower();});

# Quit button 
#---------------------------------------------------------------------------------
$x = $width - $dxright - $fm_w;

my $quitbmp = $mw->Bitmap('quit', -file => Tk::findINC('quit.bmp'),
			  -background => $bg,
			  -foreground => $hlbg);

$fm1->Button(@buttonattr,
	     -image => $quitbmp,
	     -command => \&quitdialogbox,
	     )->place(-x => $x, '-y' => $y);
$x -= $fm_w + 10*$coef;

IvyIO::bind_for_kill_all(\&killandquit);

#---------------------------------------------------------------------------------
#
# load fugue config file and create agents panel
#
#---------------------------------------------------------------------------------

my ($code, @fugueconfigdata);
if ($fuguefile) {
    ($code, @fugueconfigdata) = &FugueConfig::parse($fuguefile);
    die "Can't parse fugue config file $fuguefile\n" unless $code;
}

my $x0 = $dxleft + 20*$coef;
my $y0 = $dytop + 25*$coef;
my $rmax = 10*$coef;
my $cmin = 2;
my $wmax = ($width-2*$x0);
my $dy = 64*$coef;
my %hosts;

Agent->configure($mw, $bg, $fg, $selcolor, $pbcolor, $hlbg, $darkbg, \@fontspec20,
                  $x0, $y0, $wmax, $dy, $cmin, $rmax, $opt{b}, $coef, $pingThreshold);

for (@fugueconfigdata) {
    next unless $_->[0] eq 'global' or $_->[0] eq 'local';
    my ($type, $host, $appname, $command, $params) = @$_;
    $hosts{$host} = 1;
    if ($appname eq 'ivylaunch') {
	$ivylaunch_agent++;
    } elsif ($appname ne 'ivycontrolpanel') {
	Agent->new($appname, $host, $command, $params, 0);
    }
}

Agent->hosts(keys(%hosts));


MainLoop;

#=================================================================================
#
#    F U N C T I O N S
#
#=================================================================================

sub usage {
    my $exitflag = shift;
    print "Usage : ivycontrolpanel [-help] [-b ivybus] [-nocursor] [-smallsize]\n";
    print "                        [-pingthreshold threshold] [fugueconfigfile]\n";
    print "\n";
    print "Options :\n";
    print "   -b ivybus      bus ivy\n";
    print "   -nocursor      hide mouse cursor\n";
    print "   -smallsize     display the app in 640x480 mode\n";
    print "   -pingthreshold ms [def=20]  set the ping threshold in ms for detecting\n";
    print "                               performance problems (requires Ivy >= 4.18)\n";
    print "   -position position          set the window position\n";
    print "\n";
    exit 0 if $exitflag ;

} # end usage


sub killandquit {
    Agent->killall;
    $mw->after(1500, sub {
	 Agent->kill('ivylaunch') if $ivylaunch_agent > 0;
	 &quit;});

} # end killandquit


sub quit {
    exit(0);
    
} # quit


# function called when you press and maintain on a button
sub setloopcommand {
    my ($widget, $command) = @_;
    $widget->bind('<1>', sub {
	&$command;
	my $repeatid;
	my $afterid = $widget->after(500, sub {
	    $repeatid = $widget->repeat(100, \&$command);
	});
	$widget->bind('<ButtonRelease-1>', sub {
	    $widget->afterCancel($repeatid);
	    $widget->afterCancel($afterid);
	});
    });
    
} # end setloopcommand


sub unsetloopcommand {
    my ($widget) = @_;
    $widget->bind('<1>', '');
    $widget->bind('<ButtonRelease-1>', '');

} # end unsetloopcommand


# create a dialog box to quit and/or kill all agents on bus
sub quitdialogbox {
    my $tl = $mw->Toplevel(-background => $darkbg);
    $tl->title('ivycontrolpanel');
    $tl->resizable(0, 0);
    my $fm = $tl->Frame(-background => $darkbg,
			-highlightthickness => 3*$coef,
			-highlightbackground => $hlbg,
			)->pack(-side => 'top', -padx => 0, -pady => 0);
    my $fm1 = $fm->Frame(-background => $darkbg)->pack(-side => 'bottom');
    my @labelattr = (-relief => 'flat',
		     -background => $darkbg,
		     -foreground => $fg,
		     -borderwidth => 0,
		      -highlightthickness => 0,
		      -height => 2,
		      @fontspec24);
    my @buttonattr = (-relief => 'flat',
		      -background => $bg,
		      -foreground => $fg,
		      -borderwidth => 1,
		      -highlightbackground => $hlbg,
		      -highlightthickness => 3*$coef,
		      -height => 2,
		      -activebackground => $bg,
		      -activeforeground => $fg,
		      @fontspec24);
    my $l = $fm->Label(@labelattr, -text => "kill all agents ?",
		)->pack(-side => 'top', -padx => 20*$coef, -pady => 20*$coef,
			-expand => 1, -fill => 'both');
    $fm1->Button(@buttonattr, -text => 'yes',
		-command => sub {
		    $l->configure(-text => $l->cget(-text)."...\nwait...");
		    &killandquit;
		},
		)->pack(-side => 'left', -padx => 20*$coef, -pady => 20*$coef,
			-expand => 1, -fill => 'both');
    $fm1->Button(@buttonattr, -text => 'cancel',
		-command => sub { $tl->destroy },
		)->pack(-side => 'left', -padx => 20*$coef, -pady => 20*$coef,
			-expand => 1, -fill => 'both');
    # placement at window center
    $tl->update;
    my ($x, $y) = ($mw->rootx, $mw->rooty);
    $x += int($width/2 - $tl->width/2);
    $y += int($height/2 - $tl->height/2);
    $tl->geometry('+'.$x.'+'.$y);

    
} # end quitdialogbox


#---------------------------------------------------------------------------------
#
# Agents Functions
#
#---------------------------------------------------------------------------------
# called when an ivy agent connects.
sub connected {
    my ($agent, $host) = @_;
    if ($agent eq 'ivylaunch') {
	$ivylaunch_agent++;
    } elsif ($agent ne 'ivycontrolpanel') {
	Agent->connect($agent, $host);
    }
} # end connected


# called when an ivy agent disconnects.
sub disconnected {
   my ($agent, $host) = @_;
   if ($agent eq 'ivylaunch') {
      $ivylaunch_agent--;
   } elsif ($agent ne 'ivycontrolpanel') {
      Agent->disconnect($agent);
   }

} # end disconnected

# called when an ivy ping/pong
sub pingcb {
   my ($agent, $host, $pingvalue) = @_;
    if ($agent eq 'ivylaunch') {
	$ivylaunch_agent++;
    } elsif ($agent ne 'ivycontrolpanel') {
	Agent->pingcb($agent, $host, $pingvalue);
    }
}

#---------------------------------------------------------------------------------
#
# Functions dedicated to play/pause buttons
#
#---------------------------------------------------------------------------------
# called on user interaction to start traffic
sub play {
    if ($settingtime) {
	$mw->bell;
	return;
    }
    &display_play;
    IvyIO::send_play();

} # end play


# called on user interaction to stop traffic
sub pause {
    &display_pause;
    IvyIO::send_pause();

} # end pause


# graphical update when traffic starts
sub display_play {
    return if $isplaying == 1;
    $isplaying = 1;
    &btnpush($playbtn, $playbmp);
    &btnpull($pausebtn, $pausebmp);

} # end display_play


# graphical update when traffic stops
sub display_pause {
    return if defined($isplaying) and $isplaying == 0;
    $isplaying = 0;
    &btnpull($playbtn, $playbmp);
    &btnpush($pausebtn, $pausebmp);

} # end display_pause


# graphical update when a button is pushed
sub btnpush {
    my ($btn, $bmp) = @_;
    $btn->configure(-highlightbackground => $selcolor);
    $bmp->configure(-foreground => $selcolor);
    
} # end btnpush


# graphical update when a button is pulled
sub btnpull {
    my ($btn, $bmp) = @_;
    $btn->configure(-highlightbackground => $hlbg);
    $bmp->configure(-foreground => $hlbg);
    
} # end btnpull

#---------------------------------------------------------------------------------
#
# Rate management functions
#
#---------------------------------------------------------------------------------
# find in the predefined rate list @rate the value closest given rate
sub findclosestrate {
    my $rate = shift;
    my $sign = shift;
    my $i1;
    my $i2;
    for (my $i=0; $i<@rate; $i++) {
	if ($rate > $_) {
	    $i1 = $i;
	} else {
	    $i2 = $i;
	    last;
	}
    }
    if ($sign eq '-') {
	return $i1;
    } else {
	return $i2;
    }

} # end findclosestrate

# called on user interaction to decrease rate 
sub decrease_rate {
    my $rate = $lab_rate->cget(-text);
    if ($rate <= $rate[0]) {
	$mw->bell;
	return;
    } 
    my $index = $rate2index{$rate};
    if (defined($index)) {
	$rate = $rate[$index-1];
    } else {
	$index = &findclosestrate($rate, '-');
	if ($index == 0) {
	    $rate = $rate[$index];
	} else {
	    $rate = $rate[$index-1];
	}
    }
    &display_rate($rate);
    IvyIO::send_rate($rate);
    
} # end decrease_rate


# called on user interaction to increase rate 
sub increase_rate {
    my $rate = $lab_rate->cget(-text);
    if ($rate >= $rate[-1]) {
	$mw->bell;
	return;
    } 
    my $index = $rate2index{$rate};
    if (defined($index)) {
	$rate = $rate[$index+1];
    } else {
	$index = &findclosestrate($rate, '+');
	if ($index == $#rate) {
	    $rate = $rate[$index];
	} else {
	    $rate = $rate[$index+1];
	}
    }
    &display_rate($rate);
    IvyIO::send_rate($rate);
    
} # end increase_rate


# graphical update when rate is displayed
sub display_rate {
    my ($rate) = shift;
    $rate =~ s/\.0+$//;
    if (length($rate) > 2) {
	$lab_rate->configure(-width => 3, @fontspec24);
	$lab_rate->place(-x => 5*$coef, '-y' => 15*$coef);
    } else {
	$lab_rate->configure(-width => 2, @fontspec34);
	$lab_rate->place(-x => 7*$coef, '-y' => 10*$coef);
    }
    $lab_rate->configure(-text => $rate);
    if ($rate == 0 and $isplaying) {
	&display_pause;
	$wasplayingBeforeResettingRate = 1;
    } elsif ($isplaying == 0 and $wasplayingBeforeResettingRate == 1) {
	&play;
	$wasplayingBeforeResettingRate = 0;
    }

} # end display_rate

#---------------------------------------------------------------------------------
#
# Time management functions
#
#---------------------------------------------------------------------------------

# called on user interaction : activates buttons to increase/decrease values
# of time fields
sub settime {
    my ($lab, $field, $x, $y) = @_;
    my %placeinfo = $btn_tprev->placeInfo;
    my $y = $placeinfo{'-y'};
    if ($isplaying) {
	&pause;
	$wasplayingBeforeSettingTime = 1;
    }
    if ($selectedtimelabel eq $lab) {
	$selectedtimelabel = undef;
	$lab->configure(-background => $bg, -foreground => $fg);
	$btn_tprev->place('-y' => $y + 80*$coef);
	$btn_tnext->place('-y' => $y + 80*$coef);
	&unsetloopcommand($btn_tprev);
	&unsetloopcommand($btn_tnext);
	my $time = $lab_hour->cget(-text).':'.$lab_min->cget(-text).':'.
	    $lab_sec->cget(-text);
        IvyIO::send_time($time);
	$settingtime = 0;
	if ($wasplayingBeforeSettingTime) {
	    &play;
	    $wasplayingBeforeSettingTime = 0;
	}
    } else {
	$settingtime = 1;
	for ($lab_hour, $lab_min, $lab_sec) {
	    $_->configure(-background => $bg, -foreground => $fg);
	}
	$lab->configure(-background => $fg, -foreground => $bg);
	unless (defined $selectedtimelabel) {
	    $btn_tprev->place('-y' => $y - 80*$coef);
	    $btn_tnext->place('-y' => $y - 80*$coef);
	}
	$selectedtimelabel = $lab;
	&setloopcommand($btn_tprev, sub {&decrease_time($lab, $field)});
	&setloopcommand($btn_tnext, sub {&increase_time($lab, $field)});

    }
    
} # end settime

# called on user interaction to increase the value of a time field.
sub increase_time {
    my ($lab, $field) = @_;
    my $val = $lab->cget(-text);
    if ($field == 0 and $val == 23 or $field > 0 and $val == 59) {
	$val = 0;
    } else {
	$val += 1;
    }
    &display_time($lab, $val);

} # end increase_time

	
# called on user interaction to decrease the value of a time field.
sub decrease_time {
    my ($lab, $field) = @_;
    my $val = $lab->cget(-text);
    if ($field == 0 and $val == 0) {
	$val = 23;
    } elsif ($field > 0 and $val == 0) {
	$val = 59;
    } else {
	$val -= 1;
    }
    &display_time($lab, $val);

} # end decrease_time

# graphical update when the value of a time field is set
sub display_time {
    my ($lab, $val) = @_;
    return unless defined($val);
    $val = '0'.$val if length($val) == 1;
    $lab->configure(-text => $val);

} # end display_time

=head1 NAME

ivycontrolpanel - an interface for controlling ivy agents

=head1 SYNOPSIS

ivycontrolpanel [-help] [-b bus] [-nocursor] [-smallsize] [-position pos] [-pingthreshold ms] [fugueconfigfile]

=head1 DESCRIPTION

ivycontrolpanel displays agents detected on a given ivy bus. For each agent, a led indicates its status. By clicking on an active agent (or its led), you kill it (an ivy die message is sent to it). When ivycontrolpanel is associated with a Fugue configuration file, the agents referenced in this file can be restarted by the same way. These agents are recognizable by their larger led's border.

At bottom of window, two areas are dedicated to a specific agent, the air traffic simulator Rejeu. At bottom left, you can adjust the traffic speed. At bottom center, you have a clock; when you click on one of time fields, two scrolling arrows appears which let you adjust time; validation is done by clicking on the last selected field. Near, play/pause buttons let you start or stop the traffic simulator. Then, at bottom right, the cross button will be used to kill all agents and quit.

=head1 LISTENED MESSAGE

listen to the message 'IvyControlPanel KillAll', to kill all the agent (like quit button).

=head1 OPTIONS

=over
    
=item B<-b> bus 

Specify the ivy bus.

=item B<-nocursor> 

Hide mouse cursor (for touchscreen usage).

=item B<-smallsize> 

Set the window size to 640x480 pixels.

=item B<-position> XOFF+YOFF

Specify the window position.

=item B<-pingthreshold> duration in ms

Set the ping threshold for detecting performance problems. ivycontrolpanel periodically pings agents and compares the ping/pong duration with the given threshold. If duration exceeds it, the agent's led becomes blinking red. Default is 20 ms. Requires Ivy v4.18 or higher. 

=back
    
=head1 FILE FORMAT

The format of fugue configuration files is described in ivylaunch(1) man page.
    
=head1 SEE ALSO

ivylaunch(1), ivybanner(1)

=head1 AUTHORS

Daniel Etienne <etienne@cena.fr>

Michelle Jacomi