def write_file( self, file_path ): """Handle request to write to the given file on the Pygame side This is rather complicated by the need to have the file complete by the time the function returns. Very poor API, after all, if I have to write a multi-hundred-megabyte file it might take many minutes to complete writing. """ log.info( 'write_file: %s %s', file_path, self.metadata ) if os.path.exists( file_path ): self.read_file( file_path ) import olpcgames, pygame from olpcgames import eventwrap event = eventwrap.Event( type = pygame.USEREVENT, code = olpcgames.FILE_WRITE_REQUEST, filename = file_path, metadata = self.metadata, ) eventwrap.post( event ) event.block() if not os.path.exists( file_path ): log.warn( '''No file created in %r''', file_path ) raise NotImplementedError( """Pygame Activity code did not produce a file for %s"""%( file_path, )) else: log.info( '''Stored file in %r''', file_path )
def _mousemove(self, widget, event): # From http://www.learningpython.com/2006/07/25/writing-a-custom-widget-using-pygtk/ # if this is a hint, then let's get all the necessary # information, if not it's all we need. if event.is_hint: x, y, state = event.window.get_pointer() else: x = event.x y = event.y state = event.state rel = (x - self.__mouse_pos[0], y - self.__mouse_pos[1]) self.__mouse_pos = (x, y) self.__button_state = [ state & gtk.gdk.BUTTON1_MASK and 1 or 0, state & gtk.gdk.BUTTON2_MASK and 1 or 0, state & gtk.gdk.BUTTON3_MASK and 1 or 0, ] evt = eventwrap.Event(pygame.MOUSEMOTION, pos=self.__mouse_pos, rel=rel, buttons=self.__button_state) self._post(evt) return True
def _mouseevent(self, widget, event, type): evt = eventwrap.Event(type, button=event.button, pos=(event.x, event.y)) self._post(evt) return True
def _keyevent(self, widget, event, type): key = gtk.gdk.keyval_name(event.keyval) if key is None: # No idea what this key is. return False keycode = None if key in self.key_trans: keycode = self.key_trans[key] elif hasattr(pygame, 'K_' + key.upper()): keycode = getattr(pygame, 'K_' + key.upper()) elif hasattr(pygame, 'K_' + key.lower()): keycode = getattr(pygame, 'K_' + key.lower()) elif key == 'XF86Start': # view source request, specially handled... self._mainwindow.view_source() else: print 'Key %s unrecognized' % key if keycode is not None: if type == pygame.KEYDOWN: mod = self._keymods() self.__keystate[keycode] = type == pygame.KEYDOWN if type == pygame.KEYUP: mod = self._keymods() ukey = unichr(gtk.gdk.keyval_to_unicode(event.keyval)) if ukey == '\000': ukey = '' evt = eventwrap.Event(type, key=keycode, unicode=ukey, mod=mod) assert evt.key, evt self._post(evt) return True
def addBuddy(self, handle, buddy): """Add a new buddy to internal structures Note: this is called in the GObject thread! """ try: current = self._buddies.get(handle) if current is False: self.removeBuddy(handle) return # we left before we got our metadata text = textsprite.TextSprite( text=buddy.props.nick, color=(255, 255, 255), size=11, ) self._buddies[handle] = (buddy, text) for group in self.groups: group.add(text) #log.info( '''Created text for buddy: %s (%s)''', buddy.props.nick, handle ) # send event to trigger a rendering/processing cycle... eventwrap.post(eventwrap.Event( pygame.USEREVENT, code=1, )) #log.debug( 'Sent user event' ) except Exception, err: log.error("""Failure setting up buddy %s: %s""", buddy, get_traceback(err))
def read_file(self, file_path): """Handle request to read the given file on the Pygame side This is complicated rather noticeably by the silly semantics of the Journal where it unlinks the file as soon as this method returns. We either have to handle the file-opening in PyGTK (not acceptable), block this thread until the Pygame thread handles the event (which it may never do) or we have to make the silly thing use a non-standard file-opening interface. """ log.info( 'read_file: %s %s', file_path, self.metadata ) import olpcgames, pygame from olpcgames import eventwrap event = eventwrap.Event( type = pygame.USEREVENT, code = olpcgames.FILE_READ_REQUEST, filename = file_path, metadata = self.metadata, ) eventwrap.post( event ) event.block()
def _quit(self, data=None): self.__stopped = True eventwrap.post(eventwrap.Event(pygame.QUIT))
def do_expose_event(self, event, widget): """Handle exposure event (trigger redraw by gst)""" log.info('Expose event: %s', event) from olpcgames import eventwrap eventwrap.post(eventwrap.Event(eventwrap.pygame.VIDEOEXPOSE)) return True