Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/olpcgames/eventwrap.py
diff options
context:
space:
mode:
Diffstat (limited to 'olpcgames/eventwrap.py')
-rw-r--r--olpcgames/eventwrap.py70
1 files changed, 36 insertions, 34 deletions
diff --git a/olpcgames/eventwrap.py b/olpcgames/eventwrap.py
index 402109c..d20091c 100644
--- a/olpcgames/eventwrap.py
+++ b/olpcgames/eventwrap.py
@@ -1,25 +1,25 @@
"""Provides substitute for Pygame's "event" module using gtkEvent
-Provides methods which will be substituted into Pygame in order to
+Provides methods which will be substituted into Pygame in order to
provide the synthetic events that we will feed into the Pygame queue.
These methods are registered by the "install" method.
-This event queue does not support getting events only of a certain type.
-You need to get all pending events at a time, or filter them yourself. You
-can, however, block and unblock events of certain types, so that may be
-useful to you.
+This event queue does not support getting events only of a certain type.
+You need to get all pending events at a time, or filter them yourself. You
+can, however, block and unblock events of certain types, so that may be
+useful to you.
Set_grab doesn't do anything (you are not allowed to grab events). Sorry.
Extensions:
- wait( timeout=None ) -- allows you to wait for only a specified period
- before you return to the application. Can be used to e.g. wait for a
+ wait( timeout=None ) -- allows you to wait for only a specified period
+ before you return to the application. Can be used to e.g. wait for a
short period, then release some resources, then wait a bit more, then
release a few more resources, then a bit more...
"""
import pygame
-import gtk
+from gi.repository import GObject
import Queue
import thread, threading
import logging
@@ -29,10 +29,11 @@ log = logging.getLogger( 'olpcgames.eventwrap' )
from pygame.event import Event, event_name, pump as pygame_pump, get as pygame_get
-class Event(object):
+class Event(GObject.Object):
"""Mock pygame events"""
def __init__(self, type, dict=None,**named):
"""Initialise the new event variables from dictionary and named become attributes"""
+ GObject.Object.__init__(self)
self.type = type
if dict:
self.__dict__.update( dict )
@@ -54,8 +55,8 @@ class Event(object):
"""Block until this event is finished processing
Event process is only finalized on the *next* call to retrieve an event
- after the processing operation in which the event is processed. In some
- extremely rare cases we might actually see that happen, were the
+ after the processing operation in which the event is processed. In some
+ extremely rare cases we might actually see that happen, were the
file-saving event (for example) causes the Pygame event loop to exit.
In that case, the GTK event loop *could* hang.
"""
@@ -68,18 +69,19 @@ class Event(object):
self.__lock.set()
log.info( '''Released GTK thread on event: %s''', self )
except AttributeError, err:
- pass
+ pass
-class CallbackResult( object ):
+class CallbackResult( GObject.Object ):
def __init__( self, callable, args, named, callContext=None ):
"""Perform callback in Pygame loop with args and named
- callContext is used to provide more information when there is
+ callContext is used to provide more information when there is
a failure in the callback (for debugging purposes)
"""
+ GObject.Object.__init__(self)
self.callable = callable
- self.args = args
- self.named = named
+ self.args = args
+ self.named = named
if callContext is None:
callContext = util.get_traceback( None )
self.callContext = callContext
@@ -90,7 +92,7 @@ class CallbackResult( object ):
except Exception, err:
log.error(
"""Failure in callback %s( *%s, **%s ): %s\n%s""",
- getattr(self.callable, '__name__',self.callable),
+ getattr(self.callable, '__name__',self.callable),
self.args, self.named,
util.get_traceback( err ),
self.callContext
@@ -117,7 +119,7 @@ def _processCallbacks( events ):
else:
result.append( event )
if events and not result:
- result.append(
+ result.append(
Event( type=pygame.NOEVENT )
)
return result
@@ -126,18 +128,18 @@ def _recordEvents( events ):
"""Record the set of events to retire on the next iteration"""
global _EVENTS_TO_RETIRE
events = _processCallbacks( events )
- _EVENTS_TO_RETIRE = events
+ _EVENTS_TO_RETIRE = events
return events
def install():
"""Installs this module (eventwrap) as an in-place replacement for the pygame.event module.
Use install() when you need to interact with Pygame code written
- without reference to the olpcgames wrapper mechanisms to have the
+ without reference to the olpcgames wrapper mechanisms to have the
code use this module's event queue.
XXX Really, use it everywhere you want to use olpcgames, as olpcgames
- registers the handler itself, so you will always wind up with it registered when
+ registers the handler itself, so you will always wind up with it registered when
you use olpcgames (the gtkEvent.Translator.hook_pygame method calls it).
"""
log.info( 'Installing OLPCGames event wrapper' )
@@ -153,7 +155,7 @@ class _FilterQueue( Queue.Queue ):
def get_type( self, filterFunction, block=True, timeout=None ):
"""Get events of a given type
- Note: can raise Empty *even* when blocking if someone else
+ Note: can raise Empty *even* when blocking if someone else
pops the event off the queue before we get around to it.
"""
self.not_empty.acquire()
@@ -182,14 +184,14 @@ class _FilterQueue( Queue.Queue ):
"""Are we empty with respect to filterFunction?"""
for element in self.queue:
if filterFunction( element ):
- return False
+ return False
return True
def _get_type( self, filterFunction ):
"""Get the first instance which matches filterFunction"""
for element in self.queue:
if filterFunction( element ):
self.queue.remove( element )
- return element
+ return element
# someone popped the event off the queue before we got to it!
raise Queue.Empty
def peek_type( self, filterFunction= lambda x: True ):
@@ -236,14 +238,14 @@ def pump():
def get( types=None):
"""Get a list of all pending events
- types -- either an integer event-type or a sequence of integer event types
- which restrict the set of event-types returned from the queue. Keep in mind
+ types -- either an integer event-type or a sequence of integer event types
+ which restrict the set of event-types returned from the queue. Keep in mind
that if you do not remove events you may wind up with an eternally growing
- queue or a full queue. Normally you will want to remove all events in your
+ queue or a full queue. Normally you will want to remove all events in your
top-level event-loop and propagate them yourself.
Note: if you use types you lose all event ordering guarantees, events
- may show up after events which were originally produced before them due to
+ may show up after events which were originally produced before them due to
the re-ordering of the queue on filtering!
"""
pump()
@@ -278,13 +280,13 @@ def poll():
def wait( timeout = None):
"""Get the next pending event, wait up to timeout if none
- timeout -- if present, only wait up to timeout seconds, if we
- do not find an event before then, return None. timeout
+ timeout -- if present, only wait up to timeout seconds, if we
+ do not find an event before then, return None. timeout
is an OLPCGames-specific extension.
"""
pump()
try:
- result = None
+ result = None
result = g_events.get(block=True, timeout=timeout)
try:
return _recordEvents( [result] )[0]
@@ -296,8 +298,8 @@ def wait( timeout = None):
def peek(types=None):
"""True if there is any pending event
- types -- optional set of event-types used to check whether
- an event is of interest. If specified must be either a sequence
+ types -- optional set of event-types used to check whether
+ an event is of interest. If specified must be either a sequence
of integers/longs or an integer/long.
"""
if types:
@@ -306,7 +308,7 @@ def peek(types=None):
return not g_events.empty()
def clear():
- """Clears the entire pending queue of events
+ """Clears the entire pending queue of events
Rarely used
"""