#!/usr/bin/perl

use lib qw(/usr/lib/libDrakX);

use common;
use interactive;
use standalone;
use modules;
use detect_devices;
use Xconfig;
use mouse;
use c;

$::isEmbedded = ($::XID, $::CCPID) = "@ARGV" =~ /--embedded (\w+) (\w+)/;
local $_ = join '', @ARGV;

/-h/ and die "usage: mousedrake [--auto] [--testing]\n";

$::auto = /-auto/;
$::testing = /-testing/;

my $in = 'interactive'->vnew('su', 'mouse');

-r '/etc/modules.conf' and modules::mergein_conf('/etc/modules.conf');

undef $::Plug;
begin:
my ($curr_env) = Xconfig::getinfoFromXF86Config('');
my ($mouse) = mouse::detect() unless $::noauto;
my $time_tag2;

#- now try to merge $curr_env->{mouse} with $mouse.
$mouse->{XMOUSETYPE} eq $curr_env->{mouse}{XMOUSETYPE} ||
    $mouse->{XMOUSETYPE} eq 'PS/2' && ($curr_env->{mouse}{XMOUSETYPE} =~ m|PS/2| ||
				       $curr_env->{mouse}{auxmouse}{XMOUSETYPE} =~ m|PS/2|) and $mouse = $curr_env->{mouse};

$::isEmbedded and kill USR2, $::CCPID;
if (!$mouse || !$::auto) {
    $mouse ||= mouse::fullname2mouse("serial|Generic 2 Button Mouse");
    if ($::isEmbedded && ref($in) =~ /gtk/) {
	require my_gtk;
	my $time_tag = Gtk->timeout_add(100, sub {
					    defined $::Plug && defined $::Plug->child or return 1;
					    test_mouse($mouse,$::Plug->child);
					    0;
					});
    }
    my $name = $in->ask_from_treelistf('mousedrake', _("Please, choose the type of your mouse."), '|',
				       sub { join '|', map { translate($_) } split '\|', $_[0] },
				       [ mouse::fullnames ], 
				       $mouse->{type} . '|' . $mouse->{name});
    Gtk->timeout_remove($time_tag2) if $::isEmbedded  && ref($in) =~ /gtk/;
    $name or $::isEmbedded ? do { kill(USR1, $::CCPID); goto begin } : $in->exit(0);
    my $mouse_chosen = mouse::fullname2mouse($name);
    $mouse->{type} eq $mouse_chosen->{type} && $mouse->{name} eq $mouse_chosen->{name} or $mouse = $mouse_chosen;

    if ($mouse->{device} eq "usbmouse") {
	my ($c) = grep { $_->{driver} =~ /usb-[ou]hci/ } detect_devices::pci_probe(0) or die _("no serial_usb found\n");
	eval { modules::load($c->{driver}, "serial_usb") };
    }

    $mouse->{XEMU3} = 'yes' if $mouse->{nbuttons} < 3 && (!$::noauto || $in->ask_yesorno('', _("Emulate third button?"), 1));

    $mouse->{device} = $in->ask_from_listf(_("Mouse Port"),
					   _("Please choose on which serial port your mouse is connected to."),
					   \&mouse::serial_port2text,
					   [ mouse::serial_ports ],
					   $mouse->{device},
					  ) || goto begin if $mouse->{type} eq 'serial';
}

mouse::write_conf($mouse);
-e "/var/lock/subsys/gpm" and system "service", "gpm", "restart";

$::isEmbedded ? kill(USR1, $::CCPID) : $in->exit(0);
goto begin;

sub test_mouse {
    my ($mouse, $hbox) = @_;

   my ($width, $height, $offset) = (210, round_up(min(350, $::windowheight - 150), 6), 25);

    my $darea = new Gtk::DrawingArea;
    $darea->set_events([ 'button_press_mask', 'button_release_mask' ]);  #$darea must be unrealized.
    my_gtk::gtkpack($hbox, my_gtk::gtkset_border_width(my_gtk::gtkpack(new Gtk::VBox(0,10), my_gtk::gtksize(my_gtk::gtkset_usize($darea, $width+1, $height+1), $width, $height)),10));


    my ($m3_image, $m3_mask) = my_gtk::gtkcreate_xpm($darea, 'mouse_3b.xpm');
    my ($m3_imagep, $m3_maskp) = my_gtk::gtkcreate_xpm($darea, 'mouse_3b+.xpm');
    my ($m3_left, $m3_left_mask) = my_gtk::gtkcreate_xpm($darea, 'mouse_left.xpm');
    my ($m3_right, $m3_right_mask) = my_gtk::gtkcreate_xpm($darea, 'mouse_right.xpm');
    my ($m3_middle, $m3_middle_mask) = my_gtk::gtkcreate_xpm($darea, 'mouse_middle.xpm');
    my $image = $m3_image;
    $mouse->{nbuttons} > 3 and $image = $m3_imagep;
    my $drawarea; $drawarea = sub { $darea->window->draw_pixmap ($darea->style->bg_gc('normal'),
						      $image, 0, 0,
						      ($darea->allocation->[2]-$width)/2, ($darea->allocation->[3]-$height)/2,
						      210, 350)
		     };

      my $paintButton = sub {
	  my ($nb, $pressed) = @_;
	  my $x = 60 + $nb*33;
	  $drawarea->();
	  if ($nb == 0) {
	      $darea->window->draw_pixmap ($darea->style->bg_gc('normal'),
					   $m3_left, 0, 0,
					   ($darea->allocation->[2]-$width)/2+31, ($darea->allocation->[3]-$height)/2 + 52,
					   59, 91);
	  } elsif ($nb == 2) {
	      $darea->window->draw_pixmap ($darea->style->bg_gc('normal'),
					   $m3_right, 0, 0,
					   ($darea->allocation->[2]-$width)/2+117, ($darea->allocation->[3]-$height)/2 + 52,
					   61, 91);
	  } elsif ($nb == 1) {
	      if($mouse->{nbuttons} > 3) {
		  $darea->window->draw_pixmap ($darea->style->bg_gc('normal'),
					       $m3_middle, 0, 0,
					       ($darea->allocation->[2]-$width)/2+98, ($darea->allocation->[3]-$height)/2 + 67,
					       13, 62);
	      } else {
		  $darea->window->draw_arc ( $darea->style->black_gc,
					     1, ($darea->allocation->[2]-$width)/2 + $x, ($darea->allocation->[3]-$height)/2 + 90, 20, 25,
					     0, 360*64);
	      }
	  } elsif ($nb == 4) {
	      $darea->window->draw_pixmap ($darea->style->bg_gc('normal'),
					   $m3_middle, 0, 0,
					   ($darea->allocation->[2]-$width)/2+98, ($darea->allocation->[3]-$height)/2 + 67,
					   13, 62)
	  } elsif ($nb == 5) {
	      $darea->window->draw_pixmap ($darea->style->bg_gc('normal'),
					   $m3_middle, 0, 0,
					   ($darea->allocation->[2]-$width)/2+98, ($darea->allocation->[3]-$height)/2 + 67,
					   13, 62)
	  }
      };
      $darea->signal_connect(button_press_event => sub {
  			       my $b = $_[1]{button};
  			       $time = $default_time;
  			       $b >= 4 ?
  				 $paintWheel->($b == 4 ? -1 : 1) :
  				 $paintButton->($b - 1);
  			       $expose2 = 0;
  			   });
      $darea->signal_connect(button_release_event => sub { 
				 $drawarea->()
  			   });
      $darea->signal_connect(expose_event => sub { $drawarea->() });
}