Author: dmeyer
Date: Mon Apr  9 18:11:57 2007
New Revision: 9426

Modified:
   trunk/ui/src/games/emulator.py
   trunk/ui/src/games/player.py
   trunk/ui/src/games/plugins/pcgames.py
   trunk/ui/src/games/plugins/scummvm.py

Log:
improve games code, patch by Mathias Weber

Modified: trunk/ui/src/games/emulator.py
==============================================================================
--- trunk/ui/src/games/emulator.py      (original)
+++ trunk/ui/src/games/emulator.py      Mon Apr  9 18:11:57 2007
@@ -47,9 +47,11 @@
 
 # freevo imports
 import freevo.conf
+from freevo.resources import ResourceHandler
 from freevo.ui.mainmenu import MainMenuItem, MainMenuPlugin
 from freevo.ui.directory import DirItem
 from freevo.ui.event import *
+from freevo.ui import plugin, application
 from freevo.ui.menu import Item, Action, Menu
 from freevo.ui.plugins.mediamenu import MediaMenu
 
@@ -135,7 +137,7 @@
                 Action(_('Remove %s') % self.name, self.remove)]
 
 
-class EmulatorPlayer(object):
+class EmulatorPlayer(ResourceHandler):
     """
     Generic game player.
     """
@@ -165,7 +167,6 @@
         Play game. Should work with most emulators. Will start
         [command_name] [parameters] [url]
         """
-        self._releaseJoystick()
         params = "%s %s" % (self.parameters, self.url)
         log.info('Start playing EmulatorItem (%s %s)' % \
                 (self.command_name, params))
@@ -191,7 +192,6 @@
         The game was quit. Send stop event to get back to the menu.
         """
         Event(STOP, handler=gameplayer.player.eventhandler).post()
-        self._acquireJoystick()
         log.info('emulator completed')
 
 
@@ -203,22 +203,6 @@
             self.child.stop()
 
 
-    def _releaseJoystick(self):
-        """
-        Release Joystick that it can be used by the game/emulator
-        """
-        #FIXME
-        pass
-
-
-    def _acquireJoystick(self):
-        """
-        Acquire Joysitck back that it can be used by freevo
-        """
-        #FIXME
-        pass
-
-
 
 class EmulatorMenuItem(MainMenuItem):
     """

Modified: trunk/ui/src/games/player.py
==============================================================================
--- trunk/ui/src/games/player.py        (original)
+++ trunk/ui/src/games/player.py        Mon Apr  9 18:11:57 2007
@@ -65,6 +65,7 @@
         if player == None:
             return False
         self.player = player
+        retry = kaa.notifier.Callback(self.play, item, player)
         if not self.status in (STATUS_IDLE, STATUS_STOPPED):
             # Already running, stop the current player by sending a STOP
             # event. The event will also get to the playlist behind the
@@ -83,37 +84,16 @@
         # Try to get VIDEO and AUDIO resources. The ressouces will be freed
         # by the system when the application switches to STATUS_STOPPED or
         # STATUS_IDLE.
-        blocked = self.get_resources('AUDIO', 'VIDEO')
-        if 'VIDEO' in blocked:
-            # Something has the video resource blocked. The only application
-            # possible is the tv player right now. It would be possible to
-            # ask the user what to do with a popup but we assume the user
-            # does not care about the tv and just stop it. FIXME ask user
-            Event(STOP, handler=blocked['VIDEO'].eventhandler).post()
-            # Now connect to the 'stop' signal once to repeat this current
-            # function call without the player playing
-            blocked['VIDEO'].signals['stop'].connect_once(retry)
-            return True
-        if 'AUDIO' in blocked:
-            # AUDIO is blocked, VIDEO is not. This is most likely the audio
-            # player and we can pause it. Do this if possible.
-            if not blocked['AUDIO'].has_capability(CAPABILITY_PAUSE):
-                # Unable to pause, just stop it
-                Event(STOP, handler=blocked['AUDIO'].eventhandler).post()
-                # Now connect to the 'stop' signal once to repeat this current
-                # function call without the player playing
-                blocked['AUDIO'].signals['stop'].connect_once(retry)
+        blocked = self.get_resources('AUDIO', 'VIDEO', 'JOYSTICK')
+        if blocked == False:
+            log.error("Can't get resource AUDIO, VIDEO, JOYSTICK")
+            return False
+        if len(blocked) > 0:
+            status = self.suspend_all(blocked)
+            if isinstance(status, kaa.notifier.InProgress):
+                status.connect(retry)
                 return True
-            # Now pause the current player. On its pause signal this player can
-            # play again. And on the stop signal of this player (STATUS_IDLE)
-            # the other player can be resumed again.
-            in_progress = blocked['AUDIO'].pause()
-            if isinstance(in_progress, kaa.notifier.InProgress):
-                # takes some time, wait
-                in_progress.connect(retry).set_ignore_caller_args()
-            if in_progress is not False:
-                # we paused the application, resume on our stop
-                self.signals['stop'].connect_once(blocked['AUDIO'].resume)
+            retry()
             return True
 
         # store item
@@ -144,6 +124,40 @@
         self.status = STATUS_STOPPING
 
 
+    def can_suspend(self):
+        """
+        Return true since it is possible to stop the game. If it is allways the
+        best thing to just stop a game is more questionable. Maybe False would
+        be better.
+        """
+        return True
+
+
+    def suspend(self):
+        """
+        Release the audio, video, joystick resource. (Stop the game)
+        """
+        if not self.status == STATUS_RUNNING:
+            yield False
+        self.status = STATUS_STOPPING
+        self.player.stop()
+        yield kaa.notifiert.YieldCallback(self.player.signals['end'])
+        self.free_resources()
+        yield True
+
+
+    def resume(self):
+        """
+        Replay the game? At the moment this does nothing. The game won't 
+        restart.
+        """
+        # restart the handler stoped by this handler before since we don't
+        # run anymore
+        if not self.status == STATUS_RUNNING:
+            return False
+        self.resume_all()
+        return True
+
     def eventhandler(self, event):
         """
         React on events and do the right command with the game.

Modified: trunk/ui/src/games/plugins/pcgames.py
==============================================================================
--- trunk/ui/src/games/plugins/pcgames.py       (original)
+++ trunk/ui/src/games/plugins/pcgames.py       Mon Apr  9 18:11:57 2007
@@ -75,7 +75,6 @@
         Play PcGame
         """
         log.info('Start playing PcGame (%s %s)', self.command_name, 
self.parameters)
-        self._releaseJoystick()
         self.child = kaa.notifier.Process(self.command_name)
         self.child.start(self.parameters).connect(self.completed)
         self.signals = self.child.signals
@@ -88,7 +87,6 @@
         The game was quit. Send Stop event to get back to the menu.
         """
         Event(STOP, handler=gameplayer.player.eventhandler).post()
-        self._acquireJoystick()
         log.info('Game completed')
 
 

Modified: trunk/ui/src/games/plugins/scummvm.py
==============================================================================
--- trunk/ui/src/games/plugins/scummvm.py       (original)
+++ trunk/ui/src/games/plugins/scummvm.py       Mon Apr  9 18:11:57 2007
@@ -36,7 +36,6 @@
 
 # python imports
 import logging
-from subprocess import Popen, PIPE
 
 # kaa imports
 import kaa.utils
@@ -74,7 +73,6 @@
         """
         log.info('Start playing PcGame (Scummvm: %s)', self.emulator_item)
         parameters = '%s %s' % (config.parameters, self.emulator_item)
-        self._releaseJoystick()
         self.child = kaa.notifier.Process(config.bin)
         self.child.start(parameters).connect(self.completed)
         self.signals = self.child.signals
@@ -97,24 +95,53 @@
     """
     romlist = []
     finished = False
+    parent = None
+    items = None
+    list_started = False
+
+    def completed(self, exit_code):
+        """
+        The ScummVM returned all configured games, append them to the menu.
+        Add an entry for the ScummVM it self to configure new games or the
+        ScummVM it self.
+        """
+        if self.items == None:
+            self.items = []
+        self.items.append(ScummvmItem(self.parent, 'ScummVM', ''))
+        self.parent.pushmenu(Menu(config.name, self.items, type='games'))
+        self.parent = None
+
+
+    def get_stdout(self, data):
+        """
+        Receive updates from the stdout of the ScummVM. Wait with parsing
+        until a line that starts with '---'
+        """
+        if self.items == None:
+            if data.startswith('---'):
+                self.items = []
+            return
+        # since list was started there are now the configured games coming    
+        name = data[:data.find(' ')]
+        description = data[data.find(' '):].strip()
+        self.items.append(ScummvmItem(self.parent, description, name))        
+
 
     def roms(self, parent):
         """
         Show all games.
         """
+        # allready waiting for updated menu?
+        if self.parent != None:
+            return
+        self.parent = parent
+        self.items = None
         items = []
-        # get list of scummvm
-        pipe = Popen([config.bin, '-t'], stdout=PIPE).stdout
-        # FIXME: this blocks the notifier loop. Maybe better use
-        # kaa.notifier.Process and yield_execution.
-        for item in pipe.readlines()[2:]:
-            name = item[:item.find(' ')]
-            description = item[item.find(' '):].strip()
-            items.append(ScummvmItem(parent, description, name))
-        # append the scummvm it self, to configure new games
-        items.append(ScummvmItem(parent, 'ScummVM', ''))
-        parent.pushmenu(Menu(config.name, items, type='games'))
-
+        # get list of scummvm start a new process
+        self.child = kaa.notifier.Process(config.bin)
+        self.child.start('-t').connect(self.completed)
+        self.child.signals['stdout'].connect(self.get_stdout)
+        
 
     def items(self, parent):
         """

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to