Scripting

X Game Launcher

Here are 2 alternative files to launch games in a new, definable x-session :

The first is very basic and simply fires up a second lightweight window manager - I use fluxbox. You will need to set up your xinit.gaming to do this and your xorg.conf.singlemon.only if you want a different screen setup to your main xorg.


#!/bin/bash
startx /home/jezm/xinit.gaming -- :1 -ac -config xorg.conf.singlemon.only

The second is a perl script - Not mine - see the header info for credits. Save this as xgame.pl and make it executable (chmod +x xgame.pl). When you run it follow the instructions.


#!/usr/bin/perl -w
# Script to run games on a separate X session Version 1.7.1
# © Laurens Buhler and Troy Henderson 2002-2004 under the terms of the GPL-2, special thanks go to: Eelco (AcTiVaTe)
# For help use: xgame --help

# Here we dump the script in a huge while loop to keep it running
while (!$quit){
&list;

# Here we look at the given script parameters
if ($ARGV[0]){
$switch = $ARGV[0];
} else {
$switch = "-";
}

if ($switch =~/-[nN]/){
if (!$ARGV[1] || $ARGV[1] !~/^\d+$/ || $ARGV[1] > @list || $ARGV[1] < 1){
print ("Invalid number, terminating...\n");
} else {
$cmdoption = 1 if $ARGV[2] && $ARGV[2]=~/-c/;
chomp ($input = $ARGV[1]);
&checkbin;
}
$quit = 1;
} elsif ($switch =~/--help/){
&help;
$quit = 1;
} elsif ($switch =~/--add/){
&gamelist;
&addition;
&gamelist;
$quit = 1;
} elsif ($switch =~/--del/){
if (@list < 1) {
print("No games to delete!\n");
} else {
&gamelist;
&delete;
&gamelist;
}
$quit = 1;
} elsif ($switch =~/--edit/){
if (@list < 1){
print("No games to edit!\n");
} else {
&gamelist;
&edit;
&gamelist;
}
$quit = 1;
} elsif ($switch =~/--prefs/){
&preferences;
$quit = 1;
} elsif ($switch =~/--list/){
if (@list < 1){
print("No games to list!\n");
} else {
&gamelist;
}
$quit = 1;
} elsif ($switch =~/--move/){
if (@list < 2){
print("Not enough games to move!\n");
} else {
&gamelist;
&move;
&gamelist;
}
$quit = 1;
} elsif ($switch =~/-d/){
($input = (split/\t/,$options[3])[1]) =~s/\n//;
if ($input !~/^\d+$/ || $input > @list || $input < 1){
print ("Invalid default game, terminating...\n");
} else {
if ($ARGV[1] && $ARGV[1]=~/-c/){
$cmdoption = 1;
splice @ARGV, 1, 0, "";
}
&checkbin;
}
$quit = 1;
} else {
&gamelist;
&options;
&input;
}
}

# Writing out the list when Xgame stops
write_out($gamelist, @list, @options);

# Here come the subroutines
sub input {
while(!$valid) {
print ("Please choose your desired game or option: ");
chomp ($input = <stdin>);
$input = lc($input);
$valid = 1;
if ($input =~ /^\D$/){
if ($input eq "a"){
undef $valid;
&addition;
&gamelist;
&options;
} elsif ($input eq "p"){
undef $valid;
&preferences;
&gamelist;
&options;
} elsif ($input eq "d" && @list > 0){
undef $valid;
&delete;
&gamelist;
&options;
} elsif ($input eq "e" && @list > 0){
undef $valid;
&edit;
&gamelist;
&options;
} elsif ($input eq "m" && @list > 1){
undef $valid;
&move;
&gamelist;
&options;
} elsif ($input eq "q"){
print ("Goodbye!\n");
$quit = 1;
} else {
undef $valid;
print ("Option not available!\n");
}
} else {
if ($input !~/^\d+$/ || $input > @list || $input < 1){
undef $valid;
print ("Invalid number/option!\n");
} else {
&checkbin;
}
}
}
}

sub list {
# Checking if the gamelist exists
$gamelist = "$ENV{HOME}/.xgamelist";
write_out($gamelist, "") if !-f "$gamelist";

# Here we grab the list of games
@list = grep {!/^\s*$/} cat($gamelist);
if (grep (/\[options\]/, @list)){
for (0 .. 3){
unshift @options, (pop @list);
}
} else {
@options = ("[options]\t\n", "xconfig\t\n", "xinitops\t\n", "default\t\n");
}
}

sub gamelist {
system "clear";
# Here we process that gamelist then display it
print "\t-=<Your Games>=-\n\n";
$number = 1;
foreach (@list){
/(.*?)\t/;
print "[" . $number++ . "]\t$1\n";
}
print "\n";
}

sub checkbin {
# And this is where we process the choice
@choice = split/\t/,$list[--$input];

# Here we make sure the exe is correct and fire up if so
@exe = split/\s+/,$choice[1];
chomp $exe[0];
if ($exe[0] =~/\//){
chomp ($executable = qx|echo $exe[0]|);
if (-f "$executable"){
&startgame;
} else {
undef $valid;
die ("Invalid executable!\n");
}
} elsif (qx|which $exe[0] 2>&1| =~/which\: no $exe[0] in/){
undef $valid;
die ("Invalid executable!\n");
} else {
&startgame;
}
}

sub startgame {
print ("Starting $choice[0]...\n");

# Checking for another instance of xgame
chomp ($xinit = (qx|ls ~/.xinitrc.* 2>&1|)[0]);
$xinit = "$ENV{HOME}/.xinitrc" if $xinit !~ /\.xinitrc\.\w{6,6}/;

# Backing up existing .xinitrc to random temp file
chomp ($tmpxinit = qx|mktemp ~/.xinitrc.XXXXXXX|);
if (-f $xinit){
write_out($tmpxinit, cat($xinit));
} else {
write_out($tmpxinit, "");
}
# Creating new .xinitrc from input and defined options
if ($cmdoption){
undef $cmdoption;
for (0, 0, 0){
splice @ARGV, $_, 1;
}
$choice[1] =~s/^(.*)$/$1 @ARGV/;
}
chomp ($choice = $choice[1]);

# The new .xinitrc get's written
($xinitopts = (split/\t/,$options[2])[1]) =~s/\n//;
write_out("$ENV{HOME}/.xinitrc", "$xinitopts\n", "exec $choice");

# Now we fire up the X server with the specified game
if ((qx|ls /tmp/.X*lock 2>&1|)[-1] =~ /(\d+)/){
($xnumber = $1)++;
} else {
$xnumber = 0;
}
($xconfig = (split/\t/,$options[1])[1]) =~s/\n//;
$xconfig =~s/(.*)/-xf86config $1/ if $xconfig ne "";

system "startx -- :$xnumber $xconfig";

# Placing the backup back to where it belongs
unlink $xinit;
if (cat($tmpxinit) ne ""){
rename ($tmpxinit, "$ENV{HOME}/.xinitrc") or die "Error moving file: $!\n";
} else {
unlink $tmpxinit;
}
$quit = 1;
}

sub help {
print ("Usage: xgame [OPTION] <enter> or: xgame <enter>\n");
print ("Options:\n\t-d,\t\t use the specified default game\n");
print ("\t-n,\t\t instantly run the choosen game (-n <number>)\n");
print ("\t-n/-d <> -c <>,\t run the chosen/default game with commandline arguments (-c <commandline args>)\n");
print ("\t--add,\t\t add a game to the gamelist\n");
print ("\t--del,\t\t delete a game from the gamelist\n");
print ("\t--edit,\t\t edit a game on the gamelist\n");
print ("\t--move,\t\t move a game on the gamelist\n");
print ("\t--list,\t\t display the gamelist\n");
print ("\t--prefs,\t change the preferences\n");
print ("\t--help,\t\t this message\n");
print ("Gamelist specific:\n");
print ("\tThe gamelist is located at ~/.xgamelist.\n");
print ("\tIt must be edited and maitained at all times through Xgame.\n");
print ("\tNot doing this could result in failure!\n");
}

sub options {
print "\t-=<Options>=-\n\n[A]\tAdd a game to the gamelist\n";
print "[D]\tDelete a game from the gamelist\n[E]\tEdit a game on the gamelist\n" if @list > "0";
print "[M]\tMove a game on the gamelist\n" if @list > "1";
print "[P]\tXgame Preferences\n[Q]\tQuit\n\n";
}

sub preferences {
while(!$pref_done){
($xconfig = (split/\t/,$options[1])[1]) =~s/\n//;
($xinitopts = (split/\t/,$options[2])[1]) =~s/\n//;
($input = (split/\t/,$options[3])[1]) =~s/\n//;
system "clear";
print "\t-=<Preferences>=-\n\n";
print "[1]\tX server config file is \"$xconfig\"\n";
print "[2]\tExtra startcommand on game launch \"$xinitopts\"\n";
print "[3]\tDefault game specified by it's number \"$input\"\n";
print "[S]\tSort the gamelist alphabetically\n";
print "[Q]\tExit these preferences\n\n";
print "Please choose your desired preference: ";
chomp ($pref_input = <stdin>);
$pref_input = lc($pref_input);
$pref_done = 1;
if ($pref_input =~ /(1)/ || $pref_input =~ /(2)/ || $pref_input =~ /(3)/){
@_ = ("", "Please give the new X config file \(example: xorg.conf\) or hit <enter> to leave empty: ", "Please specify the extra startcommand on game launch or hit <enter> to leave empty: ", "Please specify the default game or hit <enter> to leave empty: ");
print $_[$1];
chomp ($newpref = <stdin>);
$options[$1] =~s/(.*?\t).*/$1$newpref/;
undef $pref_done;
} elsif ($pref_input eq "s"){
@list = sort {uc($a) cmp uc($b)} @list;
undef $pref_done;
} elsif ($pref_input ne "q"){
undef $pref_done;
}
}
undef $pref_done;
}

sub addition {
# this is where we add games to the list
print ("Please give the name of the game you'd like to add: ");
chomp ($game = <stdin>);
print ("Please give the command related to the game: ");
chomp ($bin = <stdin>);

# here the game get's added
push (@list, "$game\t$bin\n");
}

sub delete {
# Here we check which game the user wants deleted
print "Please choose the game you'd like to delete: ";
chomp ($del = <stdin>);
while ($del !~/^\d+$/ || $del > @list || $del < 1){
print "Invalid number, please choose again: ";
chomp ($del = <stdin>);
}

# Here we create a new list without the choosen game
splice @list, --$del, 1;
}

sub move {
# First we ask which game has to be moved
print "Please choose the game you'd like to move: ";
chomp ($move = <stdin>);
while ($move !~/^\d+$/ || $move > @list || $move < 1){
print "Invalid number, please choose again: ";
chomp ($move = <stdin>);
}

# Then we ask the destination for the game
print "Please choose the destination: ";
chomp ($destin = <stdin>);
while ($destin !~/^\d+$/ || $destin > @list || $destin < 1){
print "Invalid number, please choose again: ";
chomp ($destin = <stdin>);
}

#Here we write the new gamelist
$moving = $list[--$move];
splice @list, $move, 1;
splice @list, --$destin, 0, $moving;
}

sub edit {
# First we ask which game has to be edited
print "Please choose the game you'd like to edit: ";
chomp ($edit = <stdin>);
while ($edit !~/^\d+$/ || $edit > @list || $edit < 1){
print "Invalid number, please choose again: ";
chomp ($edit = <stdin>);
}
@display = split/\t/,$list[--$edit];
chomp $display[1];
print "You choose to edit: \"$display[0]\" with command: \"$display[1]\"\n";

# Here we grab the name to be used
print "Please give the new name or hit enter to use the existing one: ";
$name = <stdin>;
$name = $display[0] if $name eq "\n";
chomp $name;

# Here we grab the command to be used
print "Please give the new command or hit enter to use the existing one: ";
$command = <stdin>;
$command = $display[1] if $command eq "\n";
chomp $command;

# Now onto changing the entry in the gamelist
$list[$edit] = "$name\t$command\n";
}

sub cat {
open MYFILE, $_[0] or die "$!";
@_ = <MYFILE>;
close MYFILE;
return (wantarray) ? @_ : join("", @_);
}

sub write_out {
$_ = shift @_;
open WRITE, ">", $_ or die "Error writing '$_': $!\n";
print WRITE @_;
close WRITE;
}