File: gfceu

package info (click to toggle)
fceux 2.3.0%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 8,904 kB
  • sloc: cpp: 108,328; ansic: 10,341; sh: 3,386; python: 424; pascal: 391; perl: 82; makefile: 70; lisp: 60
file content (549 lines) | stat: -rwxr-xr-x 16,738 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#!/usr/bin/python2
# gfceu - Graphical launcher for FCE Ultra.
# Designed on Ubuntu, with platfrom independence in mind.
version = "0.6.2svn"
# 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.
"""

  # # # # # # # #
# Python imports

import sys
import os
import pickle
import shutil
from optparse import OptionParser
from subprocess import Popen

  # # # # # # # #
# Messaging Functions
def gfceu_message(message, use_gtk=False):
  """
  gqfceu_message()
  
  This function prints messages to the user.  This is generally used for status
  messages.  However, it can be used for important messages as well.  If a
  GTK message_box is requried, the use_gtk flag can be enabled
  """
  print 'gfceu message:  '+message
  if use_gtk:
    msgbox = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_INFO,
      buttons=gtk.BUTTONS_CLOSE)
    msgbox.set_markup(message)
    msgbox.run()
    msgbox.destroy()

def gfceu_error(message, code, use_gtk=True, fatal=True):
  """
  gfceu_error()
  
  TODO:  This can be reworked to use the raise/except methods already defined
  in the standard python language.  One of these days...
  """
  print '# # # #'
  print 'gfceu ERROR code '+str(code)+':'
  print message
  print '# # # #'
  if use_gtk:
    msgbox = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_ERROR,
      buttons=gtk.BUTTONS_CLOSE)
    msgbox.set_markup('gfceu ERROR Code '+str(code)+':\n'+message)
    msgbox.run()
    msgbox.destroy()
  if fatal:
    sys.exit(code)
  

  # # # # # # # #
# Import libraries
try:
  import pytgtk
  pygtk.require("2.6")
except:
  pass
try:
  import gtk
except ImportError:
  gfceu_error('The PyGTK libraries cannot be found.\n\
  Ensure that PyGTK (>=2.0) is installed on this system.\n\
  On Debian based systems (like Ubuntu), try this command:\n\
  sudo apt-get install python-gtk2 libgtk2.0-0', 1, False)

try:
  import gtk.glade
except ImportError:
  gfceu_error('The glade libraries cannot be found.\n\
  Ensure that libglade is installed on this system.\n\
  On Debian based systems (like Ubuntu), try this command:\n\
  sudo apt-get install libglade2-0 python-gtk2 python-glade2', 2, 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


  # # # # # # # #
# GFCEU Functions

class game_options:
  sound_check = True
  fullscreen_check = False
  extra_entry = ''
  romfile = ''
  opengl_check = False
  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
  

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():
  global options, optionsfile, appconfigdir
  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['rom_entry'].set_text(options.romfile)
    widgets['sound_check'].set_active(options.sound_check)
    
    widgets['fullscreen_check'].set_active(options.fullscreen_check)
    widgets['opengl_check'].set_active(options.opengl_check)
    
    widgets['extra_entry'].set_text(options.extra_entry)
        
    # 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['no_network_radio'].set_active(True)
    widgets['join_add'].set_text(options.join_add)
    widgets['join_port'].set_value(float(options.join_port))
    widgets['join_pass'].set_text(options.join_pass)
    widgets['host_port'].set_value(float(options.host_port))
    widgets['host_pass'].set_text(options.host_pass)

  except AttributeError:   
  # When new widgets are added, old pickle files might break.
    options = game_options()
    give_widgets() 
  
def setup_environment ():
  """
  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.
  """
       
  global appconfigdir, old_optionsfile, optionsfile
       
    
  if not os.path.exists(appconfigdir):
    # this is the first time the application is run.
    # create the directory
    gfceu_message("Creating application settings directory")
    os.mkdir(appconfigdir)
   
  if os.path.exists(old_optionsfile):
    # for full backwards compatibility, this file is processed, but moved 
    # to the new directory and filename for future compatibility
    gfceu_message("Old version of options file found, converting to new version")
    shutil.move(old_optionsfile,optionsfile)

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['rom_entry'].get_text()
  options.sound_check = widgets['sound_check'].get_active()
  
  options.fullscreen_check = widgets['fullscreen_check'].get_active()
  options.opengl_check = widgets['opengl_check'].get_active()
  
  options.extra_entry = widgets['extra_entry'].get_text()
  
  options.join_radio = widgets['join_radio'].get_active()
  options.host_radio = widgets['host_radio'].get_active()
  options.no_network_radio = widgets['no_network_radio'].get_active()
  options.join_add = widgets['join_add'].get_text()
  options.join_port = widgets['join_port'].get_value()
  options.join_pass = widgets['join_pass'].get_text()
  options.host_port = widgets['host_port'].get_value()
  options.host_pass = widgets['host_pass'].get_text()

  
def launch(passed, local=False):
  global xml, options, fceu_server_binary, fceu_binary, aoss_binary
  set_options()
  
  if options.sound_check:
    sound = '-sound 1 '
  else:
    sound = '-sound 0 '
    
  if options.fullscreen_check:
    fullscreen = '-fs 1 '
  else:
    fullscreen = '-fs 0 '
    
  if options.join_radio:
    if options.join_pass == '':
      netpass = ''
    else:
      netpass = '-netpassword ' + '"' + options.join_pass + '" '
    network = '-connect "' + options.join_add + '"'\
    ' -netport '+ str(options.join_port) + ' ' + netpass
  else:
    network = ''
  
  if options.host_radio:
    if options.host_pass == '':
      netpass = ' '
    else:
      netpass = ' -netpassword ' + '"' + options.host_pass + '" '
    network = '-connect localhost -netport '+\
    str(options.host_port) + netpass + ' '
      
    
  if local:
    network = ''
  
  if options.opengl_check:
    opengl = '-opengl 1 '
  else:
    opengl = '-opengl 0 '
  
  command =  aoss_binary + ' ' + fceu_binary +' '+ sound + fullscreen +\
  network + opengl + options.extra_entry + ' '+ passed
  gfceu_message('Command: ' + command)

  
  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')
    args.append(str(options.host_port))
    if options.host_pass:
      args.append("--password")
      args.append(options.host_pass)
    pid = Popen(args).pid
  
  widgets['main_window'].hide()
  
  # os.system() is a blocker, so we must force
  # gtk to process our events.
  while gtk.events_pending():
    gtk.main_iteration_do()
  
  os.system(command)
  if options.host_radio:
    os.kill(pid, 9)
  widgets['main_window'].show()

def find_binary(this_binary):
  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:
    if os.path.isfile(os.path.join(x, this_binary)):
      return os.path.join(x,this_binary)

  if os.path.isfile(os.path.join(os.curdir,this_binary)):
    return os.path.join(os.curdir, this_binary)
        
  return None
  
  # # # # # # # #
# GTK Signal Handlers
class GladeHandlers:
  def launch_button_clicked(arg1):
    global xml
    global options
    options.romfile = widgets['rom_entry'].get_text()
    if widgets['rom_entry'].get_text() == '':
      gfceu_message('Please specify a ROM to open in the main tab.', True)
      return
    if options.network_rom:
      try:
        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
      except:
        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(arg1):
    global xml
    widgets['about_dialog'].set_name('GNOME FCE Ultra '+version)
    widgets['about_dialog'].run()
    widgets['about_dialog'].hide()

  def browse_button_clicked(widget):
    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)
    
    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)
    

    
    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['rom_entry'].set_text(x)
        options.romfile = x
        options.network_rom = False
      elif chooser.get_uri():
        x = chooser.get_uri()
        widgets['rom_entry'].set_text(x)
        options.romfile = x
        options.network_rom = True

  def gamepad_clicked(widget):
    print widget.name
    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(arg1):
    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()


  def join_radio_clicked(arg1):
    global options
    widgets['join_frame'].set_sensitive(True)
    widgets['host_frame'].set_sensitive(False)
    options.join_radio = True
    options.host_radio = False
    options.no_network_radio = False
    
  def host_radio_clicked(arg1):
    global fceu_server_binary
    if widgets['host_radio'].get_active():
      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['no_network_radio'].set_active(True)
        options.no_network_radio = True
        return False

      gfceu_message("Using: "+fceu_server_binary)
      widgets['join_frame'].set_sensitive(False)
      widgets['host_frame'].set_sensitive(True)
      options.join_radio = False
      options.host_radio = True
      options.no_network_radio = False

  def no_network_radio_clicked(arg1):
    widgets['join_frame'].set_sensitive(False)
    widgets['host_frame'].set_sensitive(False)
    options.join_radio = False
    options.host_radio = False
    options.no_network_radio = True
  
  def end(widget,arg=0):
    global xml, options, optionsfile
    set_options()
    save_options()
    gtk.main_quit()
  
##############################################################################
# Globals
options = None
appconfigdir = os.getenv('HOME') + '/.gfceu'
old_optionsfile = os.getenv('HOME')+'/.gfceu_options'
optionsfile = appconfigdir + 'gfceu_options.dat'
fceu_binary = None
aoss_binary = None
fceu_server_binary = None
#version is defined earlier in the code
#have_vfs is defined earlier in the code

class WidgetsWrapper:
  def __init__(self):
    # Search for the glade file
    # Check first in the directory of this script.
    if os.path.isfile('gfceu.glade'):
      glade_file = 'gfceu.glade'
    # Then check to see if its installed on a *nix system
    elif os.path.isfile(os.path.join(os.path.dirname(sys.executable), '../share/gfceu/gfceu.glade')):
      glade_file = os.path.join(os.path.dirname(sys.executable), '../share/gfceu/gfceu.glade')
    else:
      print 'ERROR.'
      print 'Could not find the glade interface file.'
      print 'Try reinstalling the application.'
      sys.exit(1)
    
    self.widgets = gtk.glade.XML(glade_file)
    self.widgets.signal_autoconnect(GladeHandlers.__dict__)

  def __getitem__(self, key):
    return self.widgets.get_widget(key)


  # # # # # # # #
# main
if __name__ == '__main__':
  # Parse options
  parser = OptionParser(version='%prog '+ version)
  parser.parse_args()
  
  fceu_binary = find_binary('fceu')
  if fceu_binary == None:
    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_message('Using: '+fceu_binary)
  aoss_binary = find_binary('aoss')
  if aoss_binary == None:
    gfceu_error('Could not find the the ALSA OSS wrapper.\n\
    GFCEU will not be able to share the sound device with other applications.\n\
    On Debian based systems (like Ubuntu), try the following command:\n\
    sudo apt-get install alsa-oss', 76, True, False)

  widgets = WidgetsWrapper()
  widgets['main_window'].show_all()
  setup_environment()

  options = game_options()
  load_options()
  give_widgets()
  try:
    gtk.main()
  except KeyboardInterrupt:
    sys.exit(0)