Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/olpcgames/eventwrap.py
blob: 000511c54a121b5c4ad9652918151b381b3b395c (plain)
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
"""Converts from GTK to Pygame events

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.
"""
import pygame
import gtk
import Queue
import thread
import logging

log = logging.getLogger( 'eventwrap' )

# This module reuses Pygame's Event, but
# reimplements the event queue.
from pygame.event import Event, event_name, pump as pygame_pump, get as pygame_get

#print "Initializing own event.py"

# Install myself on top of pygame.event
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 
    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 
    you use olpcgames (the gtkEvent.Translator.hook_pygame method calls it).
    """
    import eventwrap,pygame
    pygame.event = eventwrap
    import sys
    sys.modules["pygame.event"] = eventwrap
    

# Event queue:
g_events = Queue.Queue()

# Set of blocked events as set by set
g_blocked = set()
g_blockedlock = thread.allocate_lock()
g_blockAll = False

def pump():
    """Handle any window manager and other external events that aren't passed to the user. Call this periodically (once a frame) if you don't call get(), poll() or wait()."""
    pygame_pump()

def get():
    """Get a list of all pending events. (Unlike pygame, there's no option to filter by event type; you should use set_blocked() if you don't want to see certain events.)"""
    pump()
    eventlist = []
    try:
        while True:
            eventlist.append(g_events.get(block=False))
    except Queue.Empty:
        pass
    
    pygameEvents = pygame_get()
    if pygameEvents:
        log.info( 'Raw Pygame events: %s', pygameEvents)
        eventlist.extend( pygameEvents )

    return eventlist

def poll():
    """Get the next pending event if exists. Otherwise, return pygame.NOEVENT."""
    pump()
    try:
        return g_events.get(block=False)
    except Queue.Empty:
        return Event(pygame.NOEVENT)


def wait():
    """Get the next pending event, waiting if none."""
    pump()
    return g_events.get(block=True)

def peek(types=None):
    """True if there is any pending event. (Unlike pygame, there's no option to
    filter by event type)"""
    return not g_events.empty()
    
def clear():
    """Dunno why you would do this, but throws every event out of the queue"""
    try:
        while True:
            g_events.get(block=False)
    except Queue.Empty:
        pass

def set_blocked(item):
    g_blockedlock.acquire()
    try:
        # FIXME: we do not currently know how to block all event types when
        # you set_blocked(none).
        [g_blocked.add(x) for x in makeseq(item)]
    finally:
        g_blockedlock.release()
    
def set_allowed(item):
    g_blockedlock.acquire()
    try:
        if item is None:
            # Allow all events when you set_allowed(none). Strange, eh?
            # Pygame is a wonderful API.
            g_blocked.clear()
        else:
            [g_blocked.remove(x) for x in makeseq(item)]
    finally:
        g_blockedlock.release()

def get_blocked(*args, **kwargs):
    g_blockedlock.acquire()
    try:
        blocked = frozenset(g_blocked)
        return blocked
    finally:
        g_blockedlock.release()

def set_grab(grabbing):
    # We don't do this.
    pass

def get_grab():
    # We don't do this.
    return False

def post(event):
    #print "posting on own"
    g_blockedlock.acquire()
    try:
        if event.type not in g_blocked:
            g_events.put(event, block=False)
    finally:
        g_blockedlock.release()

def makeseq(obj):
    """Accept either a scalar object or a sequence, and return a sequence
    over which we can iterate. If we were passed a sequence, return it
    unchanged. If we were passed a scalar, return a tuple containing only
    that scalar. This allows the caller to easily support one-or-many.
    """
    # Strings are the exception because you can iterate over their chars
    # -- yet, for all the purposes I've ever cared about, I want to treat
    # a string as a scalar.
    if isinstance(obj, basestring):
        return (obj,)
    try:
        # Except as noted above, if you can get an iter() from an object,
        # it's a collection.
        iter(obj)
        return obj
    except TypeError:
        # obj is a scalar. Wrap it in a tuple so we can iterate over the
        # one item.
        return (obj,)