#! /usr/bin/perl -w

# Waider 26/09/2000
# October 2002: Added HINTS parsing
package Monitor::IrDA;

# Should probably pull this from someone's defines, such as irda.ph
# require 'irda.ph';
my %hints = (
			 0x0100 => 'PNP',
			 0x0200 => 'PDA',
			 0x0400 => 'COMPUTER',
			 0x0800 => 'PRINTER',
			 0x1000 => 'MODEM',
			 0x2000 => 'FAX',
			 0x4000 => 'LAN',
#			 0x8000 => 'EXTENSION', i.e look at second byte
			 0x0001 => 'TELEPHONY',
			 0x0002 => 'FILESERVER',
			 0x0004 => 'COMM',
			 0x0008 => 'MESSAGE',
			 0x0010 => 'HTTP',
			 0x0020 => 'OBEX',
			);

my %short = (
			 'PNP' => '', # not important
			 'PDA' => 'P',
			 'COMPUTER' => 'C',
			 'PRINTER' => 'L', # lpr
			 'MODEM' => 'M',
			 'FAX' => 'F',
			 'LAN' => 'N', # Network
			 'TELEPHONY' => 'T',
			 'FILESERVER' => 'S',
			 'COMM' => 'I', # IrCOMM
			 'MESSAGE' => 'G', # no mnemonic!
			 'HTTP' => 'H',
			 'OBEX' => 'O',
			);

use Monitor;
@ISA = "Monitor";

sub newdata {
  my $mon = shift;
  my $text = "";
  my $tip = "";
  my @bits;
  my $lasttime = $mon->{'lastrun'};

  if ( open( DISCOVERY, "</proc/net/irda/discovery" )) {
	while (<DISCOVERY>) {
	  next if !/^nickname: (.*?), hint: (.*?),/;
	  $text .= "$1\n";
	  my $hints = $2 || "0";

	  # Decode the hint
	  if ( $hints ) {
		for my $m ( sort keys %hints ) {
		  if ( $hints & $m ) {
			$text .= $short{$hints{$m}} || "";
			push @bits, $hints{$m};
		  }
		}
#        my $m = new Gtk::Menu;
#        my $mi = new Gtk::MenuItem( "test" );
#        $mi->signal_connect( 'activate', \&foo, "bar" );
#        $m->append( $mi );
#        $mon->{'main'}->add( $m );
#        $m->show;
#        if ( !defined( $mon->{'sig'})) {
#            $mon->{'sig'} = $mon->{'main'}->signal_connect( 'button_release_event', \&event, "event" );
#       }
	  }
	}
	close( DISCOVERY );
  }

  if ( $text eq "" ) {
	$text = "Nothing\nin range";
  }

  if ( @bits ) {
	$mon->set_tooltip( "Hints: " . join( ", ", @bits  ));
  }

  $text;
}

sub event {
    my ( $widget, $data, $event ) = @_;

    print "Event: " . $event->{type} . "\n";

    0;
}

1;
# Hints
# Siemens S45: 0xb124
# b1 24 = 1011 0001 = 0x80 & 0x20 & 0x10 & 0x01 = EXT, FAX, MDM, PNP
#         0010 0100 = 0x20 & 0x04               = OBEX, COMM
#
# Pilot: 0x8220
# 82 20 = 1000 0010 = 0x80 & 0x02 = EXT, PDA
#         0010 0000 = 0x20        = OBEX

