#!/usr/bin/perl

use Gtk;
#use Gnome;
use Gtk::Gdk::ImlibImage;

$NAME='main.pl';

set_locale Gtk;

init Gtk;
init Gtk::Gdk::ImlibImage;
#init Gnome $NAME;

################################################
# GLOBAL VARS (don't touch, you are stupid ;-) #
################################################


#Stupid Vars

my $VERSION="0.01b";
my $CREDITS="GnGeo GUI $VERSION by Julien Delange <julien@gunnm.org>\n";


my @menu_items = ( { path        => '/_File',
		     type        => '<Branch>' },
		   { path        => '/File/Quit',
		     callback    => sub { Gtk->exit( 0 ); } },

		   { path        => '/_Options',
		     type        => '<Branch>' },
		   { path        => '/Options/Configure',
		     callback    => \&create_preferences_window },

		   { path        => '/Options/Configure GnGeo' ,
		     callback    => \&create_gngeoprefs_window  },


		   { path        => '/_Help',
		     type        => '<LastBranch>' },
		   { path        => '/_Help/About' ,
		     callback    => \&create_about_window  } );




my $directory;
my $canvas;

#Window
my $window;
my $menu;
my $listitem;
my $image,$image_data;
my $box_pic;

#HBox and VBox

#Separator
my $separator;

#Labels
my $label_games;
my $label_rouge;

#Lists
my $list_games;


#Files
my $config_file;
my $config_gngeo_file;
my $gamelist_file;
my $picture_directory;

#Preferences
my %gui_prefs;
my %gngeo_prefs;

#Hash Table of pictures
my %pictures;

#Hash Table of Games
my %games; 
my %games_pic;
my $gamename;
my $gamefile;

#Help table
my %help;

 
sub load_pic
{
    my $croot = $canvas->root;
    my $cgroup = $croot->new($croot, "Gnome::CanvasGroup");
    my $img = Gtk::Gdk::ImlibImage->load_image("img.xpm") || die;
    my $imgitem = $cgroup->new($cgroup, "Gnome::CanvasImage",
				'image' => $img,
				'x' => 50,
				'y' => 50,
				width => $img->rgb_width,
				height => $img->rgb_height,
				);
}

sub init_vars
{

    mkdir ($ENV{$HOME}."/.gngeo",0755) if (! -d $ENV{$HOME}."/.gngeo");
    $directory="/data/main/roms/neogeo";
    $config_file=$ENV{"HOME"}."/.gngeo/gngeogui";
    $config_gngeo_file=$ENV{"HOME"}."/.gngeo/gngeorc";    
    $gamelist_file="gamelist";
    $picture_directory="picture";
    %gui_prefs=("rompath","",
		"gamelist","/usr/share/gngeogui/gamelist",
		"logo_pic","/usr/share/gngeogui/logo.png",
		"gngeopath","/usr/bin/gngeo",
		"imgpath",""
		);
    %gngeo_prefs=("libglpath", "/usr/lib/libGL.so",
		  "rompath" , "/usr/games/lib/xmame",
		  "romrc" , "/usr/games/lib/xmame/romrc",
		  "fullscreen", "false",
		  "blitter", "soft",
		  "effect", "none",
		  "raster", "false",
		  "scale", "1",
		  "interpolation","false",
		  "sound","true",
		  "showfps","false",
		  "autoframeskip","true",
		  "sleepidle","false",
		  "joystick","true",
		  "debug","false",
		  "hwsurface","false",
		  "convtitle","true",
		  "pal","false",
		  "invertjoy","false",
		  "samplerate", "22050",
		  "country", "europe",
		  "system", "arcade",
		  "p1joy","2,3,0,1,5,4,0,1",
		  "p2joy","2,3,0,1,5,4,0,1",
		  "p1key","119,120,113,115,38,34,273,274,276,275",
		  "p2key","108,109,111,112,233,39,264,261,260,262",
		  );
    
    
    &parse_gngeoconfig;
    &parse_config;    

    if(! -r $gui_prefs{"gamelist"}){print_error("Error Gamelist","Incorrect gamelist file. Please set up in the configuration"); print $gui_prefs{"gamelist"};}
    else{&parse_gamelist();}

    print_error("Error Rompath","Incorrect Rompath. Please set up in the configuration") if(! -d $gui_prefs{"rompath"});

    print_error("Error GnGeo path","Incorrect Gngeopath. Please set up in the configuration") if(! -x $gui_prefs{"gngeopath"});
}

sub fill_list
{
    my $label;
    my $filename;
    my $listitem;
    my $i;
    
    $list_games->clear_items(0,-1);
    
#    opendir(DIR,$gui_prefs{"rompath"}) || die "directory does not exist";
    
    foreach (keys(%games))
    {
	$filename=$gui_prefs{"rompath"}."/".$_;
	if (-f $filename)
	{
	    
	    $listitem=new Gtk::ListItem();	    
	    $label=new Gtk::Label($games{$_} || $_);
	    $listitem->add($label);
	    $listitem->signal_connect("select",\&change_current_game);
	    $label->show();
	    $list_games->add($listitem);
	    
	    $listitem->show();  
	}
	
    }
#    closedir(DIR);
}

sub parse_gamelist
{
    my $key, $value,$pic;
    open(FILE,$gui_prefs{"gamelist"})||die "cannot open gamelist file";
    
    print "Parse gamelist file ...\n";
    while (<FILE>)
    {
	chomp;
	($key,$value,$pic)=split(/->/);
	$games{$key}=$value;
	$games_pic{$key}=$pic;
	print "$key, $value, $pic \n";
    }
    foreach (keys(%games))
    {
	print $games{$_};
       
    }
}

sub parse_config
{
    my $key, $value;
   
    &write_config() if (! -f $config_file);
    
    print "Parse conf for this gui ...";
    open(FILE,$config_file);
    
    while(<FILE>)
    {
	chomp;
	($key,$value)=split(/ /);
	$gui_prefs{$key}=$value;
    }
    
    close(FILE);
    
}

sub write_config
{

    open(FILE,">$config_file");
    print "Write conf for this gui ...\n";
    foreach (keys(%gui_prefs))
    {
	print FILE $_ . " " . $gui_prefs{$_}."\n";
    }
    close(FILE);
}

sub parse_gngeoconfig
{
    my $key, $value;
        
    if (! -f $config_gngeo_file)
    {
	&write_gngeoconfig();
    }
    
    print "Parse gngeo conf ...\n";

    open(FILE,$config_gngeo_file);

    while(<FILE>)
    {
	chomp;
	($key,$value)=split(/ /);
	$gngeo_prefs{$key}=$value;
    }
    
    close(FILE);
    
}

sub write_gngeoconfig
{

    open(FILE,">$config_gngeo_file");
    print "Write gngeo conf ...\n";
    foreach (keys(%gngeo_prefs))
    {
	print FILE $_ . " " . $gngeo_prefs{$_}."\n";
    }
    close(FILE);
}

sub menu_init
{
    my $menubar;
    my $accel_group;
    my $item_factory;
    $accel_group = new Gtk::AccelGroup();
    $item_factory = new Gtk::ItemFactory( 'Gtk::MenuBar',
					  '<main>',
					  $accel_group );
    $item_factory->create_items( @menu_items );
    $window->add_accel_group( $accel_group );
    return ( $item_factory->get_widget( '<main>' ) );
}

sub create_preferences_window
{
    my $window_pref;
    my $button;
    my $box, $box_buttons;
    
    my $button_help;
    
    my $label_princ;
    my @labels;
    my @entries;
    
    my $temp, $temp_box;
    
    $window->hide();

    $window_pref = new Gtk::Window();
    $button_save = new Gtk::Button("Save preferences");
    $button_cancel = new Gtk::Button("Cancel");

    $box = new Gtk::VBox();
    $box_buttons = new Gtk::HBox();
    $label_princ=new Gtk::Label("Preferences");

    $box->pack_start($label_princ,0,0,0);
    
    $labels{"rompath"} = new Gtk::Label("Set RomPATH");
    $entries{"rompath"} = new Gtk::Entry(60);
    $entries{"rompath"}->set_text($gui_prefs{"rompath"});
    $help{"rompath"}="Type the path, which contains you roms.\n(example : /home/user/roms/neogeo/)";

    $labels{"gngeopath"} = new Gtk::Label("GnGeo path");
    $entries{"gngeopath"} = new Gtk::Entry(60);
    $entries{"gngeopath"}->set_text($gui_prefs{"gngeopath"});
    $help{"gngeopath"}="Type here the full path to gngeo binary.\nMost of time, it's /usr/bin/gngeo or /usr/local/bin/gngeo";


    $labels{"gamelist"} = new Gtk::Label("Gamelist file");
    $entries{"gamelist"} = new Gtk::Entry(60);
    $entries{"gamelist"}->set_text($gui_prefs{"gamelist"});
    $help{"gamelist"}="Type the path of the gamelist file.\nThis is the file, which contains the lsit of the game, with the name of the rom.\nMore informations can be found in the manpage of gngeogui";

    $labels{"logo_pic"} = new Gtk::Label("GnGeoGui Logo");
    $entries{"logo_pic"} = new Gtk::Entry(60);
    $entries{"logo_pic"}->set_text($gui_prefs{"logo_pic"});
    $help{"logo_pic"}="Type a path of a picture.\nThis picture will be show when a game doesn't have any screenshot";
    $labels{"imgpath"} = new Gtk::Label("Pics Path");
    $entries{"imgpath"} = new Gtk::Entry(60);
    $entries{"imgpath"}->set_text($gui_prefs{"imgpath"});
    $help{"imgpath"}="Set the path to the pictures, which will be shown as screenshots of each game\nBe careful, the game have to be the same as the rom\n(ex : your rom is called rom.zip, the pic have to call rom.jpg, rom.png or rom.xpm";

    $i=0;
    foreach (keys (%labels)) {
	$temp_box=new Gtk::HBox();

	$labels{$_}->set_usize(90,10);
	$labels{$_}->set_alignment(1,1);

	$button_help=new Gtk::Button("help?") if ($help{$_});
	$button_help->signal_connect("clicked", \&print_help, $window_pref, $help{$_} );
	
	
	$temp_box->pack_start($labels{$_},0,0,0);
	$temp_box->pack_start($entries{$_},0,0,0);
	$temp_box->pack_start($button_help,0,0,0);

	$entries{$_}->show();
	$labels{$_}->show();
	$temp_box->show();
	$button_help->show();
	$box->pack_start($temp_box,0,0,0);
    }

    $button_save->signal_connect("clicked",\&processing_save_prefs,\$window_pref, \%entries);
    $button_cancel->signal_connect("clicked",sub{$window_pref->destroy(); $window->show();});
    
    $box_buttons->pack_start($button_cancel,0,0,0);
    $box_buttons->pack_start($button_save,0,0,0);
    $window_pref->signal_connect( "delete_event", sub { $window_pref->destroy(); $window->show(); });   
    
    $box->pack_start($box_buttons,0,0,0);
    

    $label_princ->show();
    $button_cancel->show();
    $button_save->show();
    $box_buttons->show();
    $box->show();
    $window_pref->add($box);
    $window_pref->show();
}

sub processing_save_prefs
{
    shift;

    my $old_win=shift;
    ${$old_win}->destroy();
    
    my $prefs=shift;
    
    foreach (sort (keys (%$prefs)))
    {
	$gui_prefs{$_}=${$prefs}{$_}->get_chars(0,-1);
    }

    print "Writing new GUI conffile ...\n";
    &write_config();
    &fill_list();
    
    $window->show();

}

sub create_gngeoprefs_window
{
    my $window_pref;
    my $button;
    my $box, $box_buttons;

    my $label_princ;
    my %labels;
    my %entries;
    
    my $temp, $temp_box;

    $window->hide();

    $window_pref = new Gtk::Window();
    $button_save = new Gtk::Button("Save preferences");
    $button_cancel = new Gtk::Button("Cancel");
    
    $box = new Gtk::VBox();
    $box_buttons = new Gtk::HBox();
    $label_princ=new Gtk::Label("Preferences");

    $box->pack_start($label_princ,0,0,0);
    
    $labels{"libglpath"} = new Gtk::Label("Path to libGL");
    $entries{"libglpath"} = new Gtk::Entry(30);
    $entries{"libglpath"}->set_text($gngeo_prefs{"libglpath"});
    $help{"libglpath"}="Libglpath";

    $labels{"rompath"} = new Gtk::Label("RomPath");
    $entries{"rompath"} = new Gtk::Entry(30);
    $entries{"rompath"}->set_text($gngeo_prefs{"rompath"});
    $help{"rompath"}="You have to put in this input the \npath to your bios and roms";

    $labels{"romrc"} = new Gtk::Label("RomRC");
    $entries{"romrc"} = new Gtk::Entry(30);
    $entries{"romrc"}->set_text($gngeo_prefs{"romrc"});
    $help{"romrc"}="You have to tell where is the romrc file\n(Maybe in /usr/share/gngeo/romrc";

    $labels{"fullscreen"} = new Gtk::Label("Fullscreen");
    $entries{"fullscreen"} = new Gtk::Entry(30);
    $entries{"fullscreen"}->set_text($gngeo_prefs{"fullscreen"});
    $help{"fullscreen"}="Type 'true' if you want that gngeo run in full screen, or 'false'";

    $labels{"blitter"} = new Gtk::Label("Blitter");
    $entries{"blitter"} = new Gtk::Entry(30);
    $entries{"blitter"}->set_text($gngeo_prefs{"blitter"});


    $labels{"effect"} = new Gtk::Label("Effect");
    $entries{"effect"} = new Gtk::Entry(30);
    $entries{"effect"}->set_text($gngeo_prefs{"effect"});
    $help{"effect"}="Tell the effect you want.\nIt can be 'none', 'scanline','sai','supersai','eagle'\n or other. See the manual page of gngeo to know what effect you want";

    $labels{"raster"} = new Gtk::Label("Raster");
    $entries{"raster"} = new Gtk::Entry(30);
    $entries{"raster"}->set_text($gngeo_prefs{"raster"});

    $labels{"scale"} = new Gtk::Label("Scale");
    $entries{"scale"} = new Gtk::Entry(30);
    $entries{"scale"}->set_text($gngeo_prefs{"scale"});

    $labels{"interpolation"} = new Gtk::Label("Interpolation");
    $entries{"interpolation"} = new Gtk::Entry(30);
    $entries{"interpolation"}->set_text($gngeo_prefs{"interpolation"});

    $labels{"sound"} = new Gtk::Label("Sound");
    $entries{"sound"} = new Gtk::Entry(30);
    $entries{"sound"}->set_text($gngeo_prefs{"sound"});
    $help{"sound"}="Put 'true', and sound will be enable in gngeo, or 'false'";

    $labels{"showfps"} = new Gtk::Label("ShowFPS");
    $entries{"showfps"} = new Gtk::Entry(30);
    $entries{"showfps"}->set_text($gngeo_prefs{"showfps"});
    $help{"showfps"}="Type 'true', and gngeo will show you the number of frame per second.\nElse, type 'false'";

    $labels{"autoframeskip"} = new Gtk::Label("AutoFrameSkip");
    $entries{"autoframeskip"} = new Gtk::Entry(30);
    $entries{"autoframeskip"}->set_text($gngeo_prefs{"autoframeskip"});
    $help{"autoframeskip"}="It will skip some frames of the game.\nIt is useful on some machines.";

    $labels{"sleepidle"} = new Gtk::Label("SleepIdle");
    $entries{"sleepidle"} = new Gtk::Entry(30);
    $entries{"sleepidle"}->set_text($gngeo_prefs{"sleepidle"});

    $labels{"joystick"} = new Gtk::Label("Joystick");
    $entries{"joystick"} = new Gtk::Entry(30);
    $entries{"joystick"}->set_text($gngeo_prefs{"joystick"});

    $labels{"hwsurface"} = new Gtk::Label("Hardware Surface");
    $entries{"hwsurface"} = new Gtk::Entry(30);
    $entries{"hwsurface"}->set_text($gngeo_prefs{"hwsurface"});

    $labels{"convtitle"} = new Gtk::Label("Convtitle");
    $entries{"convtitle"} = new Gtk::Entry(30);
    $entries{"convtitle"}->set_text($gngeo_prefs{"convtitle"});

    $labels{"pal"} = new Gtk::Label("Pal System ? (buggy)");
    $entries{"pal"} = new Gtk::Entry(30);
    $entries{"pal"}->set_text($gngeo_prefs{"pal"});

    $labels{"invertjoy"} = new Gtk::Label("InvertJoy");
    $entries{"invertjoy"} = new Gtk::Entry(30);
    $entries{"invertjoy"}->set_text($gngeo_prefs{"invertjoy"});

    $labels{"samplerate"} = new Gtk::Label("Samplerate");
    $entries{"samplerate"} = new Gtk::Entry(30);
    $entries{"samplerate"}->set_text($gngeo_prefs{"samplerate"});

    $labels{"country"} = new Gtk::Label("Country");
    $entries{"country"} = new Gtk::Entry(30);
    $entries{"country"}->set_text($gngeo_prefs{"country"});

    $labels{"system"} = new Gtk::Label("Wich system ??");
    $entries{"system"} = new Gtk::Entry(30);
    $entries{"system"}->set_text($gngeo_prefs{"system"});

    $labels{"p1joy"} = new Gtk::Label("Config for Joy 1");
    $entries{"p1joy"} = new Gtk::Entry(30);
    $entries{"p1joy"}->set_text($gngeo_prefs{"p1joy"});

    $labels{"p2joy"} = new Gtk::Label("Config for Joy 2");
    $entries{"p2joy"} = new Gtk::Entry(30);
    $entries{"p2joy"}->set_text($gngeo_prefs{"p2joy"});

   
    foreach (sort(keys (%labels))) {
	$temp_box=new Gtk::HBox();
	
	$labels{$_}->set_usize(125,10);
	$labels{$_}->set_alignment(1,1);

	$button_help=new Gtk::Button("help?");
	$button_help->signal_connect("clicked", \&print_help, $window_pref, $help{$_} ) if ($help{$_});

	$temp_box->pack_start($labels{$_},0,0,0);
	$temp_box->pack_start($entries{$_},0,0,0);
	$temp_box->pack_start($button_help,0,0,0);
	
	$button_help->show();
	$entries{$_}->show();
	$labels{$_}->show();
	$temp_box->show();
	$box->pack_start($temp_box,0,0,0);
    }

    $button_cancel->signal_connect("clicked", sub { $window_pref->destroy(); $window->show(); } );
    $button_save->signal_connect("clicked", \&processing_save_gngeoprefs, $window_pref, \%entries );
    $window_pref->signal_connect( "delete_event",sub { $window_pref->destroy(); $window->show(); }  );       
    $box_buttons->pack_start($button_cancel,0,0,0);
    $box_buttons->pack_start($button_save,0,0,0);
    
    $box->pack_start($box_buttons,0,0,0);
    

    $label_princ->show();
    $button_cancel->show();
    $button_save->show();
    $box_buttons->show();
    $box->show();
    $window_pref->add($box);
    $window_pref->show();
}

sub processing_save_gngeoprefs
{
    shift;
    
    my $temp;
    my $win=shift;
    $win->destroy();
    
    my $prefs = shift;

    foreach (keys (%$prefs))
    {
	    $gngeo_prefs{$_}=${$prefs}{$_}->get_chars(0,-1);
    }
    print "Writing new gngeo configuration ...\n";
    &write_gngeoconfig();

    $window->show();
}


sub update_image
{
    my $w,$h,$p,$m;
    my $image_name=shift;

    $box_pic->remove($image) if $image!=undef;

    #create image
    $image_data=load_image Gtk::Gdk::ImlibImage($image_name);
    ($w,$h) = ($image_data->rgb_width,$image_data->rgb_height);
    $image_data->render($w,$h);
    
    ($p,$m)=($image_data->move_image,$image_data->move_mask);
    print "hello $p , lol $m \n";
    $image= new Gtk::Pixmap($p,$m);
    $box_pic->pack_start($image,1,1,1);
    $box_pic->set_homogeneous(4);
    $image->show();
    print "insertion image";
}


#Lanch the game ! Youhou :-)
sub launch_game
{
    my $command;
    
    $command = $gui_prefs{"gngeopath"}." ".$gui_prefs{"rompath"}."/".$gamename;
    print $command;
    system($command);
}


sub change_current_game
{
    
    my @dlist;
    my $selected, $position;
    my $picfile;

    $list=shift;


    @dlist = $list_games->selection;
    $selected = $dlist[ 0 ] if ( @dlist );    
    
    if(defined($selected)){
	my $position = $list_games->child_position($selected);
	$gamename=(keys(%games))[$position];

	$gamefile = $gui_prefs{"rompath"}."/".$gamefile;
	
	$picfile = $gui_prefs{"imgpath"}."/".$games_pic{$gamename};
	$picfile = $gui_prefs{"logo_pic"} if (! $games_pic{$gamename});
	update_image($picfile) if ( -f $picfile);
	
	print "\njeu : $gamename \n fichier image $picfile \n";
    }
}

sub print_help
{
    my $text;
    my $window_help, $label, $button, $box;

   

    $text=@_[2];

    $window_help = new Gtk::Window();
    $window_help->set_title("Help");

    $label=new Gtk::Label($text);
    
    $button = new Gtk::Button("Close");
    $button->signal_connect('clicked',sub{$window_help->destroy();});
    
    $box = new Gtk::VBox();
    $box->pack_start($label,0,0,0);
    $box->pack_start($button,0,0,0);
    
    $label->show();
    $button->show();
    $box->show();
    $window_help->add($box);
    $window_help->show();

}


sub print_error
{
    my $info, $titre;
    my $window_error, $label, $button, $box;

    ($titre,$info)=@_;

    $window_error = new Gtk::Window();
    $window_error->set_title($titre || "Error");
    $window_error->set_usize(200,200);

    $label=new Gtk::Label($info || "Error");
    
    $button = new Gtk::Button("Close");
    $button->signal_connect('clicked',sub{$window_error->destroy(); $window->show();});
    
    $box = new Gtk::VBox();
    $box->pack_start($label,0,0,0);
    $box->pack_start($button,0,0,0);
    
    $label->show();
    $button->show();
    $box->show();
    $window_error->add($box);
    $window_error->show();

    print "$titre : $info \n";
}

sub create_about_window
{
    my $window_about;
    my $box;
    my $label;
    my $text;
    my $button;
    
    $window_about = new Gtk::Window();
    $window_about->set_title("About GnGeoGui");
    $window_about->set_usize(250,200);
    
    $label = new Gtk::Label("GnGeo GUI By Julien Delange");
    
    $button=new Gtk::Button("Close");
    $button->signal_connect('clicked', sub {$window->show(); $window_about->hide();});

    $text = new Gtk::Text( undef, undef );
    $text->set_editable(0);
    $text->insert(undef,undef,undef, $CREDITS);
    $text->insert( undef, undef, undef, "\n----\nThis software is under GPL license " );
    $box = new Gtk::VBox();
    $box->pack_start($label,0,0,0);
    $box->pack_start($text,0,0,0);
    $box->pack_start($button,0,0,0);
    $window_about->add($box);

    $label->show();
    $box->show();
    $text->show();
    $button->show();
    $window_about->show();
    
    $window->hide();
}



sub create_main_window{
    
    my $box_main;
    my $box_princ;
    my $box_list;
 
    my $box_menu;
    my $menu;
    my $window_list;
    my $launch_button;

    $window = new Gtk::Window;
    $window->set_title("Gngeo GUI");
    $window->set_usize(420,375);
#   $canvas = Gnome::Canvas->new(); 
    #create label and other stuff
    $label_games = new Gtk::Label("Game List");
    $launch_button = new Gtk::Button("Launch Game");    

    #create main list
    $list_games=new Gtk::List();

    
    #Declare new Boxes
    $box_main = new Gtk::VBox(0,0);
    $box_princ = new Gtk::HBox(0,0);
    $box_list = new Gtk::VBox(0,0);
    $box_pic = new Gtk::VBox(0,0);
    
    $menu=&menu_init;

    $window_list=new Gtk::ScrolledWindow(undef,undef);
    $window_list->set_usize(100,300);
    $window_list->add_with_viewport($list_games);
    $box_list->pack_start($label_games,0,0,0);
    $box_list->pack_start($window_list,0,0,0);
    $box_list->pack_start($launch_button,0,0,0);


    
    $box_princ->pack_start($box_list,0,0,0);
    $box_princ->pack_start($box_pic,1,1,1);

    $box_main->pack_start($menu,0,0,0);
    $box_main->pack_start($box_princ,0,0,0);
    
    $box_princ->set_homogeneous(0);
    
    #Show Widgets
   
    $launch_button->show();
    $box_list->show();
    $box_princ->show();
    $box_pic->show();
    $box_main->show();
    $menu->show();
    $list_games->show();
    $window_list->show();

 #   $canvas->show();
    $label_games->show(); 


# callback registration
    $window->signal_connect( "delete_event", sub{Gtk->exit(0);} );   
    $launch_button->signal_connect("clicked", \&launch_game);
    $list_games->signal_connect("selection_changed", \&change_current_game);
    
# set window attributes and show it
    $window->border_width( 5 );
    $window->add($box_main );
    $window->show();
}


&init_vars;

#&parse_gamelist;

&create_main_window;    
#&load_pic;
&fill_list;

#update_image("gngeogui.png") if ( -f "gngeogui.png");

# Gtk event loop

main Gtk;   
# Should never get here
    exit( 0 );
