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, $chat); # scrolling list windows
49 our $bot; # the cmd entry window
50 our $date; # the current date
53 our $annwin; # the announcement window handle
54 our $dxwin; # the dx spot window handle
55 our $wwvwin; # wwv window handle
56 our $wcywin; # wcy window handle
57 our $chatwin; # chat window handle
59 require "$root/local/DXVars.pm" if -e "$root/local/DXVars.pm";
61 # read in the user data
62 our $userfn = "$ENV{HOME}/.gtkconsole_data";
63 our $user = read_user_data();
66 our $host = 'localhost';
70 # read in gtkconsole file
73 Gtk2::Rc->set_default_files("$root/gtkconsole/gtkconsolerc", "$ENV{HOME}/.gtkconsolerc", ".gtkconsolerc");
74 Gtk2::Rc->reparse_all;
76 # sort out a callsign, host and port, looking in order
78 # 2. any defaults in the user data;
79 # 3. poke about in any spider tree that we can find
83 $call = uc shift @ARGV;
84 $host = shift @ARGV if @ARGV;
85 $port = shift @ARGV if @ARGV;
89 $call = $main::myalias;
93 my $node = $user->{clusters}->{$user->{node}};
95 if ($node->{call} || $user->{call}) {
96 $host = $node->{host};
97 $port ||= $node->{port};
101 if (-e "$root/local/Listeners.pm") {
102 require "$root/local/Listeners.pm";
103 $host = $main::listen->[0]->[0];
104 $port = $main::listen->[0]->[1];
105 $host ||= '127.0.0.1';
106 $host = "127.0.0.1" if !$host && ($host eq '0.0.0.0' || $host eq '::');
115 die "You need a callsign ($call), a hostname($host) and a port($port) to proceed" unless $call && $host && $port;
121 gtk_create_main_screen();
123 $main->set_title("gtkconsole $VERSION - DXSpider Console - $call \@ $host:$port");
125 # connect and send stuff
126 my $sock = IO::Socket::INET->new(PeerAddr=>$host, PeerPort=>$port);
127 die "Cannot connect to $/$port ($!)\n" unless $sock;
129 sendmsg($passwd) if $passwd;
131 sendmsg('set/page 500');
132 sendmsg('set/nobeep');
134 my $sock_helper = Gtk2::Helper->add_watch($sock->fileno, 'in', \&tophandler, $sock);
148 $_[0]->set_text(cldatetime(time));
154 my ($self, $data) = @_;
155 my $msg = $self->get_text;
166 my ($fd, $condx, $socket) = @_;
168 my $offset = defined $rbuf ? length $rbuf : 0;
169 my $l = sysread($socket, $rbuf, 1024, $offset);
172 while ($rbuf =~ s/^([^\015\012]*)\015?\012//) {
190 # this is truely evil and I bet there is a better way...
193 if ($line =~ /^'\w{2,4}',/) {
194 $list = eval qq([$line]);
196 $list = ['cmd', $line];
200 my $cmd = shift @$list;
201 my $handle = "handle_$cmd";
202 if (__PACKAGE__->can($handle)) {
203 __PACKAGE__->$handle($list);
205 unshift @$list, $cmd;
206 __PACKAGE__->handle_def($list);
215 my ($t, $ts) = (time, '');
217 $s = ref $ref ? join ', ',@$ref : $ref;
219 if (($cmd->{lasttime}||0) != $t) {
221 $cmd->{lasttime} = $t;
225 $cmd->add_data([$ts, $s]);
232 my ($t, $ts) = (time, '');
234 $s = ref $ref ? join(', ', @$ref) : $ref;
235 if (($cmd->{lasttime}||0) != $t) {
237 $cmd->{lasttime} = $t;
239 $cmd->add_data([$ts, $s]);
246 my ($t, $ts) = (time, '');
248 if (($dx->{lasttime}||0) != $t) {
250 $dx->{lasttime} = $t;
252 $dx->add_data([$ts, @$ref[0,1,15,3,4,16], stim($ref->[2]) ]);
260 my ($t, $ts) = (time, '');
262 # $s = ref $ref ? (join ', ',@$ref) : $ref;
264 if (($ann->{lasttime}||0) != $t) {
266 $ann->{lasttime} = $t;
270 $ann->add_data([$ts, @$ref[3,1,2]]);
279 # $s = ref $ref ? join ', ',@$ref : $ref;
283 $wcy->add_data([tim(), @$ref[10,4,5,3,6,2,7,8,9,1] ]);
292 # $s = ref $ref ? join ', ',@$ref : $ref;
295 $wwv->add_data([tim(), @$ref[6,2,3,4,5,1] ]);
304 my ($t, $ts) = (time, '');
306 $s = ref $ref ? (join ', ',@$ref) : $ref;
308 if (($ann->{lasttime}||0) != $t) {
310 $ann->{lasttime} = $t;
314 $chat->add_data([$ts, @$ref[3,1,2]]);
327 $sock->print("$msg\n");
332 my $t = shift || time;
333 return sprintf "%02d:%02d:%02d", (gmtime($t))[2,1,0];
338 my $t = shift || time;
339 return sprintf "%02d:%02d", (gmtime($t))[2,1];
342 # get a zulu time in cluster format (2300Z)
346 $t = defined $t ? $t : time;
348 my ($sec,$min,$hour) = $dst ? localtime($t): gmtime($t);
349 my $buf = sprintf "%02d%02d%s", $hour, $min, ($dst) ? '' : 'Z';
353 # get a cluster format date (23-Jun-1998)
357 $t = defined $t ? $t : time;
359 my ($sec,$min,$hour,$mday,$mon,$year) = $dst ? localtime($t) : gmtime($t);
361 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
365 # return a cluster style date time
370 my $date = cldate($t, $dst);
371 my $time = ztime($t, $dst);
372 return "$date $time";
380 my $fh = new IO::File $userfn;
390 print "$userfn missing or unreadable, starting afresh!\n";
394 'LOCAL' => {host => '127.0.0.1', port => 7300},
395 'GB7DJK' => {host => 'gb7djk.dxcluster.net', port => 7300},
396 'WR3D' => {host => 'wr3d.dxcluster.net', port => 7300},
397 'GB7BAA' => {host => 'gb7baa.dxcluster.net', port => 7300},
400 call => $main::myalias,
411 my $fh = new IO::File ">$userfn";
413 my $dd = new Data::Dumper([ $u ]);
416 $fh->print($dd->Dumpxs);
423 sub def_menu_callback
428 sub gtk_create_main_screen
430 $main = new Gtk2::Window('toplevel');
431 my $scr = $main->get_screen;
432 $scr_width = $scr->get_width;
433 $scr_width = 700 if $scr_width > 700;
434 $scr_height = int ($scr->get_height * 0.66);
435 $main->set_default_size($scr_width, $scr_height);
436 $main->signal_connect('delete_event', sub { Gtk2->main_quit; });
439 my $vbox = new Gtk2::VBox(0, 1);
444 item_type => '<Branch>',
447 callback => sub { Gtk2->main_quit; },
448 callback_action => 1,
449 accelerator => '<ctrl>Q',
455 item_type => '<Branch>',
458 callback_action => 9,
465 my $menu = Gtk2::SimpleMenu->new(menu_tree => $menutree, default_callback => \&def_menu_callback, user_data => $user);
466 $vbox->pack_start($menu->{widget}, 0, 1, 0);
469 # a paned hbox is packed as the bottom of the vbox
470 # my $bhpane = Gtk2::HPaned->new;
471 # $vbox->pack_end($bhpane, 1, 1, 0);
473 # now create the lh and rh panes
474 # my $lhvpane = Gtk2::VPaned->new;
475 # my $rhvpane = Gtk2::VPaned->new;
476 # $bhpane->pack1($lhvpane, 1, 0);
477 # $bhpane->pack2($rhvpane, 1, 0);
481 # my $lhvbox = Gtk2::VBox->new(0, 1);
482 $cmd = Screen::List->new(fields => [
484 Information => 'ttlong',
486 size => [$scr_width, $scr_height * 0.66],
488 $vbox->pack_start($cmd->widget, 1, 1, 0);
491 # callsign and current date and time
492 my $hbox = new Gtk2::HBox;
493 my $calllabel = new Gtk2::Label($call);
494 my $date = new Gtk2::Label(cldatetime(time));
495 $date->{tick} = Glib::Timeout->add(1000, \&updatetime, $date);
496 $hbox->pack_start( $calllabel, 0, 1, 0 );
497 $hbox->pack_end($date, 0, 1, 0);
498 $vbox->pack_start($hbox, 0, 1, 0);
499 $vbox->pack_start(Gtk2::HSeparator->new, 0, 1, 0);
502 $bot = new Gtk2::Entry;
503 $bot->set_editable(1);
504 $bot->signal_connect('activate', \&bothandler);
505 $bot->can_default(1);
506 $vbox->pack_end($bot, 0, 1, 0);
507 # $lhvpane->pack2($lhvbox, 1, 0);
516 $annwin = new Gtk2::Window('toplevel');
517 $ann = Screen::List->new(fields =>[
521 Announcement => 'ttlesslong',
524 frame => 'Announcements',
525 size => [$scr_width * 0.85, $scr_height * 0.25],
527 $annwin->add($ann->widget);
531 $chatwin = new Gtk2::Window('toplevel');
532 $chat = Screen::List->new(fields =>[
536 Chat => 'ttlesslong',
540 size => [$scr_width * 0.85, $scr_height * 0.25],
542 $chatwin->add($chat->widget);
546 $dxwin = new Gtk2::Window('toplevel');
547 $dx = Screen::List->new(fields => [
552 'Remarks' => 'ttshort',
557 policy => [qw(never automatic)],
561 size => [$scr_width * 0.9, $scr_height * 0.25],
563 # $rhvpane->pack1($dx->widget, 1, 0);
564 $dxwin->add($dx->widget);
568 $wwvwin = new Gtk2::Window('toplevel');
569 # my $rhvbox = Gtk2::VBox->new(0, 1);
570 $wwv = Screen::List->new( fields =>[
576 Remarks => 'ttshort',
580 policy => ['never', 'automatic'],
583 $wwvwin->add($wwv->widget);
586 # $rhvbox->pack_start($wwv->widget, 1, 1, 0);
589 $wcywin = new Gtk2::Window('toplevel');
590 $wcy = Screen::List->new(fields => [
604 policy => ['never', 'automatic'],
608 # $rhvbox->pack_start($wcy->widget, 1, 1, 0);
609 # $rhvbox->set_size_request($scr_width * 0.45, $scr_height * 0.33);
610 # $rhvpane->pack2($rhvbox, 1, 0);
611 $wcywin->add($wcy->widget);