3 # A GTK based console program
5 # usage: gtkconsole [<callsign>] [<host> <port>]
7 # Copyright (c) 2006-2007 Dirk Koopman G1TLH
14 our $VERSION = '$Revision$';
15 $VERSION =~ s|[^\d\.]+||g;
19 # search local then perl directories
21 # root of directory tree for this system
23 $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
35 use vars qw(@modules $font);
37 @modules = (); # is the list of modules that need init calling
38 # on them. It is set up by each 'use'ed module
39 # that has Gtk stuff in it
42 our @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
45 our $main; # the main screen
46 our $scr_width; # calculated screen dimensions
48 our ($dx, $cmd, $ann, $wcy, $wwv); # scrolling list windows
49 our $bot; # the cmd entry window
50 our $date; # the current date
52 require "$root/local/DXVars.pm" if -e "$root/local/DXVars.pm";
54 # read in the user data
55 our $userfn = "$ENV{HOME}/.gtkconsole_data";
56 our $user = read_user_data();
63 # read in gtkconsole file
66 Gtk2::Rc->set_default_files("$root/gtkconsole/gtkconsolerc", "$ENV{HOME}/.gtkconsolerc", ".gtkconsolerc");
67 Gtk2::Rc->reparse_all;
69 # sort out a callsign, host and port, looking in order
71 # 2. any defaults in the user data;
72 # 3. poke about in any spider tree that we can find
76 $call = uc shift @ARGV;
77 $host = shift @ARGV if @ARGV;
78 $port = shift @ARGV if @ARGV;
81 unless ($call && $host) {
82 my $node = $user->{clusters}->{$user->{node}};
84 if ($node->{call} || $user->{call}) {
85 $call = $node->{call} || $user->{call} || $main::myalias;
86 $host = $node->{passwd};
87 $host = $node->{host};
88 $port = $node->{port};
92 unless ($call && $host) {
93 if (-e "$root/local/Listeners.pm") {
94 require "$root/local/Listeners.pm";
95 $host = $main::listen->[0]->[0];
96 $port = $main::listen->[0]->[1];
97 $host ||= '127.0.0.1';
98 $host = "127.0.0.1" if $host eq '0.0.0.0';
104 $host = $user->{clusters}->{$user->{node}}->{host};
105 $port = $user->{clusters}->{$user->{node}}->{port};
111 die "You need a callsign ($call), a hostname($host) and a port($port) to proceed" unless $call && $host;
117 gtk_create_main_screen();
119 $main->set_title("gtkconsole $VERSION - DXSpider Console - $call \@ $host:$port");
121 # connect and send stuff
122 my $sock = IO::Socket::INET->new(PeerAddr=>$host, PeerPort=>$port);
123 die "Cannot connect to $/$port ($!)\n" unless $sock;
125 sendmsg($passwd) if $passwd;
127 sendmsg('set/page 500');
128 sendmsg('set/nobeep');
130 my $sock_helper = Gtk2::Helper->add_watch($sock->fileno, 'in', \&tophandler, $sock);
144 $_[0]->set_text(cldatetime(time));
150 my ($self, $data) = @_;
151 my $msg = $self->get_text;
162 my ($fd, $condx, $socket) = @_;
164 my $offset = length $rbuf;
165 my $l = sysread($socket, $rbuf, 1024, $offset);
168 while ($rbuf =~ s/^([^\015\012]*)\015?\012//) {
186 # this is truely evil and I bet there is a better way...
189 if ($line =~ /^'\w{2,4}',/) {
190 $list = eval qq([$line]);
192 $list = ['cmd', $line];
196 my $cmd = shift @$list;
197 my $handle = "handle_$cmd";
198 if (__PACKAGE__->can($handle)) {
199 __PACKAGE__->$handle($list);
201 unshift @$list, $cmd;
202 __PACKAGE__->handle_def($list);
211 my ($t, $ts) = (time, '');
213 $s = ref $ref ? join ', ',@$ref : $ref;
215 if (($cmd->{lasttime}||0) != $t) {
217 $cmd->{lasttime} = $t;
221 $cmd->add_data([$ts, $s]);
228 my ($t, $ts) = (time, '');
230 $s = ref $ref ? join(', ', @$ref) : $ref;
231 if (($cmd->{lasttime}||0) != $t) {
233 $cmd->{lasttime} = $t;
235 $cmd->add_data([$ts, $s]);
242 my ($t, $ts) = (time, '');
244 if (($dx->{lasttime}||0) != $t) {
246 $dx->{lasttime} = $t;
248 $dx->add_data([$ts, @$ref[0,1,15,3,4,16], stim($ref->[2]) ]);
256 my ($t, $ts) = (time, '');
258 $s = ref $ref ? join ', ',@$ref : $ref;
260 if (($ann->{lasttime}||0) != $t) {
262 $ann->{lasttime} = $t;
266 $ann->add_data([$ts, @$ref[3,1,2]]);
274 $s = ref $ref ? join ', ',@$ref : $ref;
278 $wcy->add_data([tim(), @$ref[10,4,5,3,6,2,7,8,9,1] ]);
286 $s = ref $ref ? join ', ',@$ref : $ref;
289 $wwv->add_data([tim(), @$ref[6,2,3,4,5,1] ]);
301 $sock->print("$msg\n");
306 my $t = shift || time;
307 return sprintf "%02d:%02d:%02d", (gmtime($t))[2,1,0];
312 my $t = shift || time;
313 return sprintf "%02d:%02d", (gmtime($t))[2,1];
316 # get a zulu time in cluster format (2300Z)
320 $t = defined $t ? $t : time;
322 my ($sec,$min,$hour) = $dst ? localtime($t): gmtime($t);
323 my $buf = sprintf "%02d%02d%s", $hour, $min, ($dst) ? '' : 'Z';
327 # get a cluster format date (23-Jun-1998)
331 $t = defined $t ? $t : time;
333 my ($sec,$min,$hour,$mday,$mon,$year) = $dst ? localtime($t) : gmtime($t);
335 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
339 # return a cluster style date time
344 my $date = cldate($t, $dst);
345 my $time = ztime($t, $dst);
346 return "$date $time";
354 my $fh = new IO::File $userfn;
364 print "$userfn missing or unreadable, starting afresh!\n";
368 'LOCAL' => {host => '127.0.0.1', port => 7300},
369 'GB7DJK' => {host => 'gb7djk.dxcluster.net', port => 7300},
370 'WR3D' => {host => 'wr3d.dxcluster.net', port => 7300},
371 'GB7BAA' => {host => 'gb7baa.dxcluster.net', port => 7300},
374 call => $main::myalias,
385 my $fh = new IO::File ">$userfn";
387 my $dd = new Data::Dumper([ $u ]);
391 $fh->print($dd->Dumpxs);
398 sub def_menu_callback
403 sub gtk_create_main_screen
405 $main = new Gtk2::Window('toplevel');
406 my $scr = $main->get_screen;
407 $scr_width = int ($scr->get_width > 1280 ? 1280 : $scr->get_width) * 0.99;
408 $scr_height = int $scr->get_height * 0.5;
409 $main->set_default_size($scr_width, $scr_height);
410 $main->signal_connect('delete_event', sub { Gtk2->main_quit; });
413 my $vbox = new Gtk2::VBox(0, 1);
418 item_type => '<Branch>',
421 callback => sub { Gtk2->main_quit; },
422 callback_action => 1,
423 accelerator => '<ctrl>Q',
429 item_type => '<Branch>',
432 callback_action => 9,
439 my $menu = Gtk2::SimpleMenu->new(menu_tree => $menutree, default_callback => \&def_menu_callback, user_data => $user);
440 $vbox->pack_start($menu->{widget}, 0, 1, 0);
443 # a paned hbox is packed as the bottom of the vbox
444 my $bhpane = Gtk2::HPaned->new;
445 $vbox->pack_end($bhpane, 1, 1, 0);
447 # now create the lh and rh panes
448 my $lhvpane = Gtk2::VPaned->new;
449 my $rhvpane = Gtk2::VPaned->new;
450 $bhpane->pack1($lhvpane, 1, 0);
451 $bhpane->pack2($rhvpane, 1, 0);
457 $ann = Screen::List->new(fields =>[
461 Announcement => 'ttlesslong',
464 frame => 'Announcements',
465 size => [$scr_width * 0.45, $scr_height * 0.33],
468 $lhvpane->pack1($ann->widget, 1, 0);
471 my $lhvbox = Gtk2::VBox->new(0, 1);
472 $cmd = Screen::List->new(fields => [
474 Information => 'ttlong',
476 size => [$scr_width * 0.45, $scr_height * 0.66],
478 $lhvbox->pack_start($cmd->widget, 1, 1, 0);
481 # callsign and current date and time
482 my $hbox = new Gtk2::HBox;
483 my $calllabel = new Gtk2::Label($call);
484 my $date = new Gtk2::Label(cldatetime(time));
485 $date->{tick} = Glib::Timeout->add(1000, \&updatetime, $date);
486 $hbox->pack_start( $calllabel, 0, 1, 0 );
487 $hbox->pack_end($date, 0, 1, 0);
488 $lhvbox->pack_start($hbox, 0, 1, 0);
489 $lhvbox->pack_start(Gtk2::HSeparator->new, 0, 1, 0);
492 $bot = new Gtk2::Entry;
493 $bot->set_editable(1);
494 $bot->signal_connect('activate', \&bothandler);
495 $bot->can_default(1);
496 $lhvbox->pack_end($bot, 0, 1, 0);
497 $lhvpane->pack2($lhvbox, 1, 0);
505 $dx = Screen::List->new(fields => [
510 'Remarks' => 'ttshort',
515 policy => [qw(never automatic)],
519 size => [$scr_width * 0.45, $scr_height * 0.45],
521 $rhvpane->pack1($dx->widget, 1, 0);
524 my $rhvbox = Gtk2::VBox->new(0, 1);
525 $wwv = Screen::List->new( fields =>[
531 Remarks => 'ttshort',
535 policy => ['never', 'automatic'],
538 $rhvbox->pack_start($wwv->widget, 1, 1, 0);
541 $wcy = Screen::List->new(fields => [
555 policy => ['never', 'automatic'],
559 $rhvbox->pack_start($wcy->widget, 1, 1, 0);
560 $rhvbox->set_size_request($scr_width * 0.45, $scr_height * 0.33);
561 $rhvpane->pack2($rhvbox, 1, 0);