fceux/gfceu

575 lines
17 KiB
Plaintext
Raw Normal View History

2006-07-30 04:00:49 +00:00
#!/usr/bin/python
# gfceu - Graphical launcher for FCE Ultra.
# Designed on Ubuntu, with platfrom independence in mind.
2008-06-06 00:32:42 +00:00
version = "0.6.1svn"
title = "gfceux"
2006-07-30 04:00:49 +00:00
# Copyright (C) 2006 Lukas Sabota <punkrockguy318@comcast.net>
##
"""
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
2006-10-14 22:13:58 +00:00
# # # # # # # #
2006-07-30 04:00:49 +00:00
# Python imports
2006-10-14 22:13:58 +00:00
2006-07-30 04:00:49 +00:00
import sys
import os
import pickle
2006-10-14 21:41:08 +00:00
import shutil
2006-07-30 04:00:49 +00:00
from optparse import OptionParser
from subprocess import Popen
2006-10-14 22:13:58 +00:00
# # # # # # # #
2006-07-30 04:00:49 +00:00
# Messaging Functions
def gfceu_print(text, use_gtk=False):
2006-07-30 04:00:49 +00:00
"""
2008-06-17 15:04:13 +00:00
gfceu_print()
2006-07-30 04:00:49 +00:00
This function prints messages to the user. This is generally used for status
2008-06-17 15:04:13 +00:00
messages. If a GTK message_box is requried, the use_gtk flag can be enabled.
2006-07-30 04:00:49 +00:00
"""
print text
2006-07-30 04:00:49 +00:00
if use_gtk:
msgbox = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_INFO,
buttons=gtk.BUTTONS_CLOSE)
msgbox.set_markup(text)
2006-07-30 04:00:49 +00:00
msgbox.run()
msgbox.destroy()
def gfceu_error(message, code, use_gtk=True, fatal=True):
"""
gfceu_error()
2008-06-17 15:04:13 +00:00
Presents the user with an error message and optionally quits the program.
2006-07-30 04:00:49 +00:00
"""
2008-06-17 15:04:13 +00:00
print title + ' error code '+str(code)+': ' + message
2006-07-30 04:00:49 +00:00
if use_gtk:
msgbox = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_ERROR,
buttons=gtk.BUTTONS_CLOSE)
2008-06-06 00:32:42 +00:00
msgbox.set_markup(title + ' ERROR Code '+str(code)+':\n'+message)
2006-07-30 04:00:49 +00:00
msgbox.run()
msgbox.destroy()
if fatal:
sys.exit(code)
2006-10-14 22:13:58 +00:00
# # # # # # # #
2006-07-30 04:00:49 +00:00
# Import libraries
try:
import pytgtk
pygtk.require("2.12")
2006-07-30 04:00:49 +00:00
except:
pass
try:
import gtk
except ImportError:
gfceu_error('The PyGTK libraries cannot be found.\n\
Ensure that PyGTK (>=2.12) is installed on this system.\n\
2006-07-30 04:00:49 +00:00
On Debian based systems (like Ubuntu), try this command:\n\
sudo apt-get install python-gtk2 libgtk2.0-0', 1, False)
try:
import gnomevfs
have_gnomevfs = True
except ImportError:
gfceu_error('The gnomevfs libraries cannot be found.\n\
To enable ROM loading over the network, ensure that gnomevfs is installed on\
this system.\n\
On Debian based systems (like Ubuntu), try this command:\n\
sudo apt-get install python-gtk2 libgnomevfs2-0', 5, False, False)
have_gnomevfs = False
2006-10-14 22:13:58 +00:00
# # # # # # # #
2006-07-30 04:00:49 +00:00
# GFCEU Functions
class game_options:
# sound
2006-07-30 04:00:49 +00:00
sound_check = True
soundq_check = True
soundrate_entry = "11000"
# video
2006-07-30 04:00:49 +00:00
fullscreen_check = False
xscale_spin = 2
yscale_spin = 2
bpp_combo = 32
2006-07-30 04:00:49 +00:00
extra_entry = ''
romfile = ''
opengl_check = False
2008-06-17 15:04:13 +00:00
# network
2006-07-30 04:00:49 +00:00
join_radio = False
join_add = ''
join_port = 4046
join_pass = ''
host_radio = False
host_port = 4046
host_pass = ''
no_network_radio = True
network_rom = False
2006-10-14 22:13:58 +00:00
2006-07-30 04:00:49 +00:00
def load_options():
global options, optionsfile
try:
ifile = file(optionsfile, 'r')
options = pickle.load(ifile)
pickle.load(ifile)
except:
return
ifile.close()
def save_options():
2006-10-14 21:41:08 +00:00
global options, optionsfile, appconfigdir
2006-07-30 04:00:49 +00:00
ofile = file(optionsfile, 'w')
pickle.dump(options, ofile)
ofile.close()
def give_widgets():
"""
give_widgets()
This function takes data from the options struct and relays it to
the GTK window
"""
global xml, options
try:
widgets.get_object("rom_entry").set_text(options.romfile)
# sound
widgets.get_object("sound_check").set_active(options.sound_check)
widgets.get_object("soundq_check").set_active(options.soundq_check)
widgets.get_object("soundrate_entry").set_text(options.soundrate_entry)
2006-07-30 04:00:49 +00:00
widgets.get_object("fullscreen_check").set_active(options.fullscreen_check)
widgets.get_object("opengl_check").set_active(options.opengl_check)
widgets.get_object("xscale_spin").set_value(options.xscale_spin)
widgets.get_object("yscale_spin").set_value(options.yscale_spin)
2006-07-30 04:00:49 +00:00
widgets.get_object("extra_entry").set_text(options.extra_entry)
2006-07-30 04:00:49 +00:00
# Usability point:
# Users will probably not want to remember their previous network setting.
# Users may accidently be connecting to a remote server/hosting a game when
# they were unaware.
# No network is being set by default
widgets.get_object("no_network_radio").set_active(True)
widgets.get_object("join_add").set_text(options.join_add)
widgets.get_object("join_port").set_value(float(options.join_port))
widgets.get_object("join_pass").set_text(options.join_pass)
widgets.get_object("host_port").set_value(float(options.host_port))
widgets.get_object("host_pass").set_text(options.host_pass)
2006-07-30 04:00:49 +00:00
2008-06-17 15:04:13 +00:00
except:
2006-07-30 04:00:49 +00:00
# When new widgets are added, old pickle files might break.
options = game_options()
give_widgets()
2006-10-14 21:41:08 +00:00
def setup_environment ():
"""
2006-10-14 22:13:58 +00:00
Configures the environment if this is the first time the application
has been run. For instance, it checks for the options file and creates
it if it doesn't exist. It also converts between the old version and
the new version of this application, which stores the options file in
a separate directory.
2006-10-14 21:41:08 +00:00
"""
2008-06-17 15:04:13 +00:00
global appconfigdir, optionsfile
2006-10-14 21:41:08 +00:00
if not os.path.exists(appconfigdir):
# this is the first time the application is run.
# create the directory
gfceu_print("Creating application settings directory")
2006-10-14 21:41:08 +00:00
os.mkdir(appconfigdir)
2006-07-30 04:00:49 +00:00
def set_options():
"""
set_options()
This function grabs all of the data from the GTK widgets
and stores it in the options object.
"""
global xml
options.romfile = widgets.get_object("rom_entry").get_text()
# sound
options.sound_check = widgets.get_object("sound_check").get_active()
options.soundq_check = widgets.get_object("soundq_check").get_active()
options.soundrate_entry = widgets.get_object("soundrate_entry").get_text()
2006-07-30 04:00:49 +00:00
# video
options.fullscreen_check = widgets.get_object("fullscreen_check").get_active()
options.opengl_check = widgets.get_object("opengl_check").get_active()
options.xscale_spin = widgets.get_object("xscale_spin").get_value()
options.yscale_spin = widgets.get_object("yscale_spin").get_value()
2006-07-30 04:00:49 +00:00
options.extra_entry = widgets.get_object("extra_entry").get_text()
2006-07-30 04:00:49 +00:00
options.join_radio = widgets.get_object("join_radio").get_active()
options.host_radio = widgets.get_object("host_radio").get_active()
options.no_network_radio = widgets.get_object("no_network_radio").get_active()
options.join_add = widgets.get_object("join_add").get_text()
options.join_port = widgets.get_object("join_port").get_value()
options.join_pass = widgets.get_object("join_pass").get_text()
options.host_port = widgets.get_object("host_port").get_value()
options.host_pass = widgets.get_object("host_pass").get_text()
2006-07-30 04:00:49 +00:00
def launch(rom_name, local=False):
2008-06-06 00:32:42 +00:00
global xml, options, fceu_server_binary, fceux_binary
2006-07-30 04:00:49 +00:00
set_options()
2008-06-08 03:33:53 +00:00
sound_options = ''
2006-07-30 04:00:49 +00:00
if options.sound_check:
2008-06-08 03:33:53 +00:00
sound_options += '--sound 1 '
2006-07-30 04:00:49 +00:00
else:
2008-06-08 03:33:53 +00:00
sound_options += '--sound 0 '
if options.soundq_check:
2008-06-08 03:33:53 +00:00
sound_options += '--soundq 1 '
else:
2008-06-08 03:33:53 +00:00
sound_options += '--soundq 0 '
if options.soundrate_entry:
2008-06-08 03:33:53 +00:00
sound_options += '--soundrate ' + options.soundrate_entry + ' '
else:
soundrate = ' '
2008-06-08 03:33:53 +00:00
# video
video_options = ''
2006-07-30 04:00:49 +00:00
if options.fullscreen_check:
video_options += '--fullscreen 1 '
2006-07-30 04:00:49 +00:00
else:
video_options += '--fullscreen 0 '
2006-07-30 04:00:49 +00:00
if options.opengl_check:
video_options += '--opengl 1 '
else:
video_options += '--opengl 0 '
video_options += ' --xscale ' + str(options.xscale_spin)
video_options += ' --yscale ' + str(options.yscale_spin)
video_options += ' '
2008-06-17 15:04:13 +00:00
# Netplay is fucked right now
"""
2006-07-30 04:00:49 +00:00
if options.join_radio:
if options.join_pass == '':
netpass = ''
else:
2008-06-06 00:32:42 +00:00
netpass = '--pass ' + '"' + options.join_pass + '" '
network = '-net "' + options.join_add + '"'\
' --port '+ str(options.join_port) + ' ' + netpass
2006-07-30 04:00:49 +00:00
else:
network = ''
if options.host_radio:
if options.host_pass == '':
netpass = ' '
else:
2008-06-06 00:32:42 +00:00
netpass = ' --pass ' + '"' + options.host_pass + '" '
network = '--net localhost --port '+\
2008-06-17 15:04:13 +00:00
str(options.host_port) + netpass + ' '
2006-07-30 04:00:49 +00:00
if local:
network = ''
2008-06-17 15:04:13 +00:00
"""
network = ''
command = fceux_binary + ' ' + sound_options + video_options +\
network + options.extra_entry + ' '+ rom_name
gfceu_print('Command: ' + command)
2006-07-30 04:00:49 +00:00
2008-06-17 15:04:13 +00:00
# more code to disable because netplay is fucked
"""
2006-07-30 04:00:49 +00:00
if options.host_radio:
xterm_binary = find_binary("xterm")
if xterm_binary == None:
gfceu_error("Cannot find xterm on this system. You will not \n\
be informed of server output.", 102, True, False)
args = [fceu_server_binary]
else:
args = [xterm_binary, "-e", fceu_server_binary]
args.append('--port')
2006-12-10 14:02:36 +00:00
args.append(str(options.host_port))
2006-07-30 04:00:49 +00:00
if options.host_pass:
args.append("--password")
args.append(options.host_pass)
pid = Popen(args).pid
2008-06-17 15:04:13 +00:00
"""
widgets.get_object("main_window").hide()
2006-07-30 04:00:49 +00:00
# os.system() is a blocker, so we must force
# gtk to process our events.
while gtk.events_pending():
2006-07-30 04:00:49 +00:00
gtk.main_iteration_do()
os.system(command)
2008-06-17 15:04:13 +00:00
widgets.get_object("main_window").show()
# another part of netplay code
"""
2006-07-30 04:00:49 +00:00
if options.host_radio:
os.kill(pid, 9)
2008-06-17 15:04:13 +00:00
"""
2006-07-30 04:00:49 +00:00
2008-06-06 00:32:42 +00:00
def find_binary(file):
2006-07-30 04:00:49 +00:00
path = os.getenv('PATH')
directories= []
directory = ''
# check for '$' so last entry is processed
for x in path + '$':
if x != ':' and x != '$':
directory = directory + x
else:
directories.append(directory)
directory = ''
for x in directories:
2008-06-06 00:32:42 +00:00
if os.path.isfile(os.path.join(x, file)):
return os.path.join(x,file)
if os.path.isfile(os.path.join(os.curdir,file)):
return os.path.join(os.curdir, file)
2006-07-30 04:00:49 +00:00
return None
##############################################################################
# Globals
options = None
appconfigdir = os.getenv('HOME') + '/.'+ title
optionsfile = appconfigdir + 'gfceu_options.dat'
fceux_binary = None
fceu_server_binary = None
class WidgetsWrapper:
def __init__(self):
# Search for the glade file
# Check first in the directory of this script.
global widgets
if os.path.isfile('gfceu.xml'):
glade_file = 'gfceu.xml'
# Then check to see if its installed on a *nix system
elif os.path.isfile(os.path.join(os.path.dirname(sys.argv[0]), '../share/gfceu/gfceu.xml')):
glade_file = os.path.join(os.path.dirname(sys.argv[0]), '../share/gfceu/gfceu.xml')
else:
print 'ERROR.'
print 'Could not find the glade interface file.'
print 'Try reinstalling the application.'
sys.exit(1)
try:
print "Using: " + glade_file
widgets = gtk.Builder()
widgets.add_from_file(glade_file)
widgets.connect_signals(self)
except:
gfceu_error("Couldn't load the glade UI file", 24)
def launch_button_clicked(self, arg1):
2006-10-14 22:13:58 +00:00
global xml
global options
options.romfile = widgets.get_object("rom_entry").get_text()
if widgets.get_object("rom_entry").get_text() == '':
gfceu_print('Please specify a ROM to open in the main tab.', True)
2006-07-30 04:00:49 +00:00
return
2006-10-14 22:13:58 +00:00
if options.network_rom:
2006-07-30 04:00:49 +00:00
try:
2006-10-14 22:13:58 +00:00
myvfs = gnomevfs.Handle(options.romfile)
# FIXME
# Smarter way of handling this? Copying direct error information?
except gnomevfs.HostNotFoundError:
gfceu_error("Remote ROM host not found.", 7, True, False)
return
2006-07-30 04:00:49 +00:00
except:
2006-10-14 22:13:58 +00:00
gfceu_error("Failed to open the network ROM.", 6, True, False)
return
myfile = file('/tmp/gfceu.nes', 'wb')
while 1:
try:
myfile.write(myvfs.read(1024))
except gnomevfs.EOFError:
break
except:
gfceu_error("Failed to open the network ROM.", 10, True, False)
return
myvfs.close()
myfile.close()
romfile = '/tmp/gfceu.nes'
else:
romfile = options.romfile
launch('"'+romfile+'"')
def about_button_clicked(self, menuitem, data=None):
2006-10-14 22:13:58 +00:00
global xml
widgets.get_object("about_dialog").set_name('GNOME FCE Ultra '+version)
widgets.get_object("about_dialog").run()
widgets.get_object("about_dialog").hide()
2006-10-14 22:13:58 +00:00
def browse_button_clicked(self, menuitem, data=None):
2006-10-14 22:13:58 +00:00
global xml,options
set_options()
chooser = gtk.FileChooserDialog("Open...", None,
gtk.FILE_CHOOSER_ACTION_OPEN,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK))
if have_gnomevfs:
chooser.set_property("local-only", False)
else:
chooser.set_property("local-only", True)
chooser.set_default_response(gtk.RESPONSE_OK)
2006-07-30 04:00:49 +00:00
2006-10-14 22:13:58 +00:00
filter=gtk.FileFilter()
filter.set_name("NES Roms")
filter.add_mime_type("application/x-nes-rom")
filter.add_mime_type("application/zip")
filter.add_pattern("*.nes")
filter.add_pattern("*.zip")
chooser.add_filter(filter)
filter = gtk.FileFilter()
filter.set_name("All files")
filter.add_pattern("*")
chooser.add_filter(filter)
2006-07-30 04:00:49 +00:00
2006-10-14 22:13:58 +00:00
if options.romfile == '':
folder = os.getenv('HOME')
else:
folder = os.path.split(options.romfile)[0]
chooser.set_current_folder (folder)
response = chooser.run()
chooser.hide()
if response == gtk.RESPONSE_OK:
if chooser.get_filename():
x = chooser.get_filename()
widgets.get_object("rom_entry").set_text(x)
2006-10-14 22:13:58 +00:00
options.romfile = x
options.network_rom = False
elif chooser.get_uri():
x = chooser.get_uri()
widgets.get_object("rom_entry").set_text(x)
2006-10-14 22:13:58 +00:00
options.romfile = x
options.network_rom = True
def gamepad_clicked(self, widget, data=None):
2006-10-14 22:13:58 +00:00
d = {'gp1_button' : '1',
'gp2_button' : '2',
'gp3_button' : '3',
'gp4_button' : '4'}
command = '-inputcfg gamepad' + d[widget.name] + ' /dev/null'
launch(command, True)
def config_help_button_clicked(self, menuitem, data=None):
2006-10-14 22:13:58 +00:00
msgbox = gtk.MessageDialog(parent=None, flags=0,
type=gtk.MESSAGE_INFO, buttons=gtk.BUTTONS_CLOSE)
msgbox.set_markup("Once a gamepad is seleceted, a titlebar will be displayed\
indicating a NES button. Press the button or key you would like to have\
associated with the button indicated on the titlebar. This process\
will repeat until all buttons on the gamepad are configured.")
msgbox.run()
msgbox.hide()
2006-07-30 04:00:49 +00:00
def join_radio_clicked(self, menuitem, data=None):
2006-10-14 22:13:58 +00:00
global options
widgets.get_object("join_frame").set_sensitive(True)
widgets.get_object("host_frame").set_sensitive(False)
2006-10-14 22:13:58 +00:00
options.join_radio = True
options.host_radio = False
options.no_network_radio = False
2006-07-30 04:00:49 +00:00
def host_radio_clicked(self, menuitem, data=None):
2006-10-14 22:13:58 +00:00
global fceu_server_binary
if widgets.get_object("host_radio").get_active():
2006-10-14 22:13:58 +00:00
fceu_server_binary = find_binary('fceu-server')
if fceu_server_binary == None:
if os.name == 'nt':
gfceu_error("The fceu server software cannot be found. \n\
Ensure that it is installed in the same directory as \n\
GFCE Ultra.", 102, True, False)
else:
gfceu_error("The fceu server software cannot be found on \n\
this system. Ensure that it is installed and in your path.",
101, True, False)
widgets.get_object("no_network_radio").set_active(True)
2006-10-14 22:13:58 +00:00
options.no_network_radio = True
return False
gfceu_print("Using: "+fceu_server_binary)
widgets.get_object("join_frame").set_sensitive(False)
widgets.get_object("host_frame").set_sensitive(True)
2006-10-14 22:13:58 +00:00
options.join_radio = False
options.host_radio = True
options.no_network_radio = False
def no_network_radio_clicked(self, menuitem, data=None):
widgets.get_object("join_frame").set_sensitive(False)
widgets.get_object("host_frame").set_sensitive(False)
2006-07-30 04:00:49 +00:00
options.join_radio = False
2006-10-14 22:13:58 +00:00
options.host_radio = False
options.no_network_radio = True
def end(self, menuitem, data=None):
2006-10-14 22:13:58 +00:00
global xml, options, optionsfile
set_options()
save_options()
gtk.main_quit()
widgets = None
2006-10-14 22:13:58 +00:00
# # # # # # # #
# main
2006-07-30 04:00:49 +00:00
if __name__ == '__main__':
# Parse options
fceux_binary = find_binary('fceux')
2006-07-30 04:00:49 +00:00
parser = OptionParser(version='%prog '+ version)
parser.add_option('-b', '--binary', action="store", type="string", dest="fceux_binary")
2006-07-30 04:00:49 +00:00
parser.parse_args()
2008-06-06 00:32:42 +00:00
if fceux_binary == None:
2006-07-30 04:00:49 +00:00
gfceu_error('Could not find the fceu binary.\n\
Ensure that FCE Ultra is installed and in the $PATH.\n\
On Debian based systems (like Ubuntu), try the following command:\n\
sudo apt-get install fceu', 4, True)
else:
gfceu_print('Using: '+fceux_binary)
2008-06-06 00:32:42 +00:00
wrap = WidgetsWrapper()
widgets.get_object("main_window").show_all()
2006-10-14 21:41:08 +00:00
setup_environment()
2006-07-30 04:00:49 +00:00
2006-10-14 22:13:58 +00:00
options = game_options()
2006-07-30 04:00:49 +00:00
load_options()
give_widgets()
try:
gtk.main()
except KeyboardInterrupt:
sys.exit(0)