#!/usr/bin/perl -w

use strict;

use Getopt::Long qw(:config no_ignore_case bundling);
use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

sub click;
sub expose;
sub debug;
sub quit;

my $debug_messages = undef;

my $window = Gtk2::Window->new('popup');
my $black = Gtk2::Gdk::Color->new( 0, 0, 0 );
my $gc;
my $def_width = 100;
my $offset = 100;

my ($click_top_x, $click_top_y, $click_bot_x, $click_bot_y );
my ($tablet_width, $tablet_height);
my @devices;

@devices = split( /[ \t]+.*\n/m, `xsetwacom list dev` );
($tablet_width, $tablet_height) =
    split( /\s+/, `xsetwacom getdefault $devices[0] bottomx bottomy`);

# Reset calibration parameters
for my $dev (@devices)
{
    system( qw/xsetwacom set/, $dev, qw/topx 0/);
    system( qw/xsetwacom set/, $dev, qw/topy 0/);
    system( qw/xsetwacom set/, $dev, 'bottomx', $tablet_width);
    system( qw/xsetwacom set/, $dev, 'bottomy', $tablet_height);
}

debug( join(', ', @devices ), "\n" );
debug( "tablet_width=$tablet_width, tablet_height=$tablet_height\n" );

$window->set_events( qw(button-press-mask) );
$window->signal_connect( button_press_event => \&click );
$window->signal_connect( expose_event => \&expose );
$window->parse_geometry( "${def_width}x$def_width+$offset+$offset" );
$window->app_paintable( 1 );
#$window->double_buffered( 0 );

$window->show();

Gtk2->main();

sub expose
{
    my ($widget, $event) = @_;
    my ($win_width, $win_height) = $widget->get_size();
    my $radius = 3;

    if( !defined( $gc ) )
    {
	$gc = Gtk2::Gdk::GC->new( $window->window );
	$gc->set_foreground( $black );
    }

    $widget->window->draw_line( $gc, $win_width/2, 0, $win_width/2,
	$win_height );
    $widget->window->draw_line( $gc, 0, $win_height/2, $win_width,
	$win_height/2 );
    $widget->window->draw_arc( $gc, 0, $win_width/2 - $radius,
	$win_height/2 - $radius, 2*$radius, 2*$radius, 0, 360*64 );
}

sub click
{
    my ($widget, $event) = @_;
    my ($win_x, $win_y) = $widget->get_position();

    if( !defined( $click_top_x ) )
    {
	$click_top_x = $win_x + $event->x;
	$click_top_y = $win_y + $event->y;
	debug( "click_top_x=$click_top_x, click_top_y=$click_top_y\n" );

	$widget->parse_geometry( "-$offset-$offset" );
    }

    else
    {
	my ($topx, $topy, $botx, $boty);
	my ($left_xoff, $left_yoff, $right_xoff, $right_yoff);
	my ($screen_width, $screen_height, $screen);

	$click_bot_x = $win_x + $event->x;
	$click_bot_y = $win_y + $event->y;

	debug( "click_bot_x=$click_bot_x, click_bot_y=$click_bot_y\n" );

	$screen = $widget->get_screen();
	$screen_width = $screen->get_width();
	$screen_height = $screen->get_height();

	$left_xoff = $offset + $def_width / 2;
	$left_yoff = $offset + $def_width / 2;
	$right_xoff = $offset + $def_width / 2;
	$right_yoff = $offset + $def_width / 2;
	
	#$padw = ($click_bot_x - $click_top_x) / ($screen_width - $win_width)
	#	* $screen_width;
	#$padh = ($click_bot_y - $click_top_y) / ($screen_height - $win_height)
	#	* $screen_height;

	$topx = ($click_top_x - $left_xoff) * $tablet_width / $screen_width;
	$botx = ($click_bot_x + $right_xoff) * $tablet_width / $screen_width;

	$topy = ($click_top_y - $left_yoff) * $tablet_height / $screen_height;
	$boty = ($click_bot_y + $right_yoff) * $tablet_height / $screen_height;

	debug( "topx=$topx, topy=$topy, botx=$botx, boty=$boty\n" );

	open( FILE, ">$ENV{HOME}/.calibrate" )
	    or quit( "Could not open $ENV{HOME}/.calibrate\n" );
	for my $dev (@devices)
	{
	    system( qw/xsetwacom set/, $dev, 'topx', int( $topx ) );
	    system( qw/xsetwacom set/, $dev, 'topy', int( $topy ) );
	    system( qw/xsetwacom set/, $dev, 'bottomx', int( $botx ) );
	    system( qw/xsetwacom set/, $dev, 'bottomy', int( $boty ) );

	    print FILE "xsetwacom set $dev topx ", int($topx), "\n";
	    print FILE "xsetwacom set $dev topy ", int($topy), "\n";
	    print FILE "xsetwacom set $dev bottomx ", int($botx), "\n";
	    print FILE "xsetwacom set $dev bottomy ", int($boty), "\n";
	}
	close(FILE);

	Gtk2->main_quit();
    }

    return FALSE;
}

sub debug
{
    print STDERR @_
	if( defined( $debug_messages ) );
}

sub quit
{
    print STDERR @_;
    Gtk2->main_quit();
}
