From 8f36842d4f3e74516229738e01e27577af16bde1 Mon Sep 17 00:00:00 2001 From: Alan Aguiar Date: Wed, 23 May 2012 07:40:51 +0000 Subject: add all files of version 2 --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ac43ebc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +*.py~ diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..55faddb --- /dev/null +++ b/MANIFEST @@ -0,0 +1,29 @@ +setup.py +activity.py +run.py +buildmanifest.py +NEWS +README +activity/activity.svg +activity/activity.info +olpcgames/COPYING +olpcgames/__init__.py +olpcgames/pangofont.py +olpcgames/mesh.py +olpcgames/pausescreen.py +olpcgames/_cairoimage.py +olpcgames/video.py +olpcgames/util.py +olpcgames/activity.py +olpcgames/canvas.py +olpcgames/svgsprite.py +olpcgames/gtkEvent.py +olpcgames/eventwrap.py +olpcgames/camera.py +olpcgames/data/__init__.py +olpcgames/data/sleeping_svg.py +horse/game.py +horse/__init__.py +horse/graphics.py +horse/horse.png +horse/grass.png \ No newline at end of file diff --git a/NEWS b/NEWS new file mode 100644 index 0000000..06afeb9 --- /dev/null +++ b/NEWS @@ -0,0 +1,5 @@ +Still in development + +TODO: + Add sound ? + Add better images for food diff --git a/README b/README new file mode 100644 index 0000000..fb77aab --- /dev/null +++ b/README @@ -0,0 +1 @@ +A game about a horse diff --git a/activity.py b/activity.py new file mode 100644 index 0000000..65f7177 --- /dev/null +++ b/activity.py @@ -0,0 +1,9 @@ +import olpcgames +from gettext import gettext as _ + +class Activity(olpcgames.PyGameActivity): + """Your Sugar activity""" + + game_name = 'run' + game_title = _('HorseGame') + game_size = None diff --git a/activity/activity.info b/activity/activity.info new file mode 100644 index 0000000..96e96fd --- /dev/null +++ b/activity/activity.info @@ -0,0 +1,7 @@ +[Activity] +name = HorseGame +activity_version = 2 +host_version = 1 +service_name = org.laptop.community.HorseGame +icon = activity +exec = sugar-activity activity.Activity diff --git a/activity/activity.svg b/activity/activity.svg new file mode 100644 index 0000000..40e804b --- /dev/null +++ b/activity/activity.svg @@ -0,0 +1,65 @@ + + + + + +]> + + + + + image/svg+xml + + + + + + + + + diff --git a/buildmanifest.py b/buildmanifest.py new file mode 100755 index 0000000..899433b --- /dev/null +++ b/buildmanifest.py @@ -0,0 +1,33 @@ +#! /usr/bin/env python +"""Stupid little script to automate generation of MANIFEST and po/POTFILES.in + +Really this should have been handled by using distutils, but oh well, +distutils is a hoary beast and I can't fault people for not wanting to +spend days spelunking around inside it to find the solutions... +""" +from distutils.filelist import FileList +import os + +def fileList( template ): + """Produce a formatted file-list for storing in a file""" + files = FileList() + for line in filter(None,template.splitlines()): + files.process_template_line( line ) + content = '\n'.join( files.files ) + return content + + +def main( ): + """Do the quicky finding of files for our manifests""" + content = fileList( open('MANIFEST.in').read() ) + open( 'MANIFEST','w').write( content ) + + content = fileList( open('POTFILES.in').read() ) + try: + os.makedirs( 'po' ) + except OSError, err: + pass + open( os.path.join('po','POTFILES.in'), 'w').write( content ) + +if __name__ == "__main__": + main() diff --git a/horse/__init__.py b/horse/__init__.py new file mode 100644 index 0000000..4ce73d7 --- /dev/null +++ b/horse/__init__.py @@ -0,0 +1,5 @@ +"""The Horse Game + +Designed for OLPC, but should run on any system with PyGame +""" +from horse import game diff --git a/horse/game.py b/horse/game.py new file mode 100644 index 0000000..6e2d5f6 --- /dev/null +++ b/horse/game.py @@ -0,0 +1,182 @@ +import pygame, logging +import math, random + +log = logging.getLogger( 'horse.game' ) +log.setLevel( logging.DEBUG ) + +class Game(): + game_running = True + # tuple for horse location + horse_loc = (0,0) + # array of (image,location) for apple/carrot/etc locations + objects = [] + # keep track of the mouse pointer + mouse_pos = (200,200) + # tuple size + screen_size = (0,0) + grass_size = (0,0) + horse_size = (0,0) + apple_size = (0,0) + # images / (type pygame Surface) + grass_image = None + horse_image = None + horse_image_l = None + moving_left = False + apple_image = None + carrot_image = None + hay_image = None + # other parameters + horse_speed = 8 # pixels per tick; at 25 ticks/second, this is approx 200 pixels per second + horse_reach = 20 # pixels from cener of horse where he can reach + target_loc = None + + def setup(self,screen): + self.screen_size = screen.get_size() # tuple + # put the horse in the center of the screen + self.horse_loc = (100,100) + # load the images and convert to screen format + self.grass_image = pygame.image.load('horse/grass.png','grass') + self.grass_image.convert(screen) + self.grass_size = self.grass_image.get_size() + self.horse_image = pygame.image.load('horse/horse.png','horse') + self.horse_image.convert(screen) + self.horse_size = self.horse_image.get_size() + # Make a copy for the left-facing image + self.horse_image_l=pygame.transform.flip(self.horse_image,True,False) + # Make the edibles + self.apple_size = (10,10) + self.apple_image = pygame.Surface(self.apple_size,0,screen) + self.apple_image.fill((0xff,0,0)) + self.carrot_size = (7,20) + self.carrot_image = pygame.Surface(self.carrot_size,0,screen) + self.carrot_image.fill((0xff,0x99,0)) + self.hay_size = (20,20) + self.hay_image = pygame.Surface(self.hay_size,0,screen) + self.hay_image.fill((0x99,0x66,0x33)) + self.update(screen) + + def update(self,screen): + """updates the screen image""" + self.screen_size = screen.get_size() # tuple + # paint background image + # TODO: there is probably a better way to tile the background image + # (and perhaps to tile it only once, and save the base image) + tilex=int(math.ceil(self.screen_size[0]/self.grass_size[0])) + tiley=int(math.ceil(self.screen_size[1]/self.grass_size[1])) + for x in range(0,tilex): + for y in range(0,tiley): + screen.blit(self.grass_image,(x*self.grass_size[0],y*self.grass_size[1])) + # paint horse image on screen + # TODO: flip horse image left or right, depending on the direction she is moving + #screen.blit(self.horse_image,self.horse_loc) + if self.moving_left: + self.drawObject(screen,(self.horse_image_l, self.horse_loc)) + else: + self.drawObject(screen,(self.horse_image, self.horse_loc)) + # draw apples and other objects + for o in self.objects: + self.drawObject(screen,o) + # flip display buffer + pygame.display.flip() + + def drawObject(self,screen,object): + # unpack the object + (image, loc) = object + object_size = image.get_size() + # adjust the upper left corner so that the center of object is at the recorded location + adj_loc = (loc[0]-object_size[0]/2,loc[1]-object_size[1]/2) + screen.blit(image, adj_loc) + + def placeObject(self,image,location): + #adj_loc = self.adjust_loc(location, image.get_size()) + adj_loc = location + self.objects.append((image,adj_loc)) + + def adjust_loc(self,loc,object_size): + """adjust the given location by half the object size. Thus the center of the object will be at loc""" + adj_loc = (loc[0]-object_size[0]/2,loc[1]-object_size[1]/2) + return adj_loc + + def handleEvent(self,event): + if event.type == pygame.QUIT: + self.game_running = False + elif event.type == pygame.KEYDOWN: + #log.debug("event keydown: %s", event) + # TODO: keys are not a localized + if event.key in (27,113): # esc or q=quit + log.debug('quit key pressed') + self.game_running = False + elif event.key == 97: # a=apple + self.placeObject(self.apple_image, self.mouse_pos) + elif event.key == 99: # c=carrot + self.placeObject(self.carrot_image, self.mouse_pos) + elif event.key == 104: # h=hay + self.placeObject(self.hay_image, self.mouse_pos) + elif event.type == pygame.KEYUP: + pass + elif event.type == pygame.MOUSEBUTTONDOWN: + #log.debug("event mousedown: %s", event) + # place apples + self.placeObject(self.apple_image, self.mouse_pos) + elif event.type == pygame.MOUSEMOTION: + #log.debug("event mousemove: %s", event) + # Remember mouse location, because we need it in KEYDOWN events + self.mouse_pos = event.pos + else: + #log.debug("event other: %s", event) + pass + + def tick(self,millis): + """updates the game state for a tick""" + # millis is ignored + if len(self.objects)>0: + # move the horse toward the first object in the queue, at full speed + (target_image,target_loc) = self.objects[0] + horse_speed = self.horse_speed + else: + # the horse might feel inclined to wander slowly toward a random target + #if self.target_loc is None: + # self.target_loc = (self.screen_size[0]*random.random(), self.screen_size[1]*random.random()) + #target_loc = self.target_loc + + # wander toward mouse + target_loc = self.mouse_pos + horse_speed = 2 + + (distx, disty) = (target_loc[0] - self.horse_loc[0], target_loc[1] - self.horse_loc[1]) + # TODO: there is probably a library function to scale this for me + # move the horse approx horse_speed pixels in the indicated direction + dist = math.sqrt(distx*distx+disty*disty) + (movex, movey) = (horse_speed*distx/dist, horse_speed*disty/dist) + + # "eat" the object if we are close enough + # (so that we will get a new target next tick) + # TODO: perhaps colision detection would be better here + if dist < self.horse_speed*2: + if len(self.objects)>0: + self.objects.pop(0) + else: + self.target_loc = None + # dont move the horse (causes bounce) + return + + # move the horse, but check that the horse has not wandered off the screen + (horsex, horsey) = (self.horse_loc[0] + movex, self.horse_loc[1] + movey) + # TODO: check for a library function to determine out of bounds + if (horsex < 0): + horsex = 0 + if (horsey < 0): + horsey = 0 + if (horsex > self.screen_size[0]): + horsex = self.screen_size[0] + if (horsey > self.screen_size[1]): + horsey = self.screen_size[1] + self.horse_loc = (horsex,horsey) + + if movex<0 and abs(distx)>horse_speed: + self.moving_left = True + else: + self.moving_left = False + + def isRunning(self): + return self.game_running \ No newline at end of file diff --git a/horse/graphics.py b/horse/graphics.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/horse/graphics.py diff --git a/horse/grass.png b/horse/grass.png new file mode 100755 index 0000000..cf7534a --- /dev/null +++ b/horse/grass.png Binary files differ diff --git a/horse/horse.png b/horse/horse.png new file mode 100644 index 0000000..79ad434 --- /dev/null +++ b/horse/horse.png Binary files differ diff --git a/olpcgames/COPYING b/olpcgames/COPYING new file mode 100644 index 0000000..b8adee0 --- /dev/null +++ b/olpcgames/COPYING @@ -0,0 +1,24 @@ +* Copyright (c) 2007, One Laptop Per Child. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * Neither the name of One Laptop Per Child nor the +* names of its contributors may be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY ONE LAPTOP PER CHILD ``AS IS'' AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL ONE LAPTOP PER CHILD BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/olpcgames/__init__.py b/olpcgames/__init__.py new file mode 100644 index 0000000..5fd28b7 --- /dev/null +++ b/olpcgames/__init__.py @@ -0,0 +1,42 @@ +"""Wrapper/adaptation system for writing/porting PyGame games to OLPC/Sugar + +The wrapper system attempts to substitute various pieces of the PyGame +implementation in order to make code written without knowledge of the +OLPC/Sugar environment run "naturally" under the GTK environment of +Sugar. It also provides some convenience mechanisms for dealing with +e.g. the Camera and Mesh Network system. + +Considerations for Developers: + +PyGame programs running under OLPCGames will generally not have +"hardware" surfaces, and will not be able to have a reduced-resolution +full-screen view to optimise rendering. The PyGame code will run in +a secondary thread, with the main GTK UI running in the primary thread. +A third "mainloop" thread will occasionally be created to handle the +GStreamer interface to the camera. + +Attributes of Note: + + ACTIVITY -- if not None, then the activity instance which represents + this activity at the Sugar shell level. + WIDGET -- PygameCanvas instance, a GTK widget with an embedded + socket object which is a proxy for the SDL window Pygame to which + pygame renders. +""" +# XXX handle configurations that are not running under Sugar and +# report proper errors to describe the problem, rather than letting the +# particular errors propagate outward. +# XXX allow use of a particular feature within the library without needing +# to be running under sugar. e.g. allow importing mesh or camera without +# needing to import the activity machinery. +from olpcgames.canvas import * +try: + from olpcgames.activity import * +except ImportError, err: + PyGameActivity = None +from olpcgames import camera +from olpcgames import pangofont +from olpcgames import mesh + +ACTIVITY = None +widget = WIDGET = None diff --git a/olpcgames/_cairoimage.py b/olpcgames/_cairoimage.py new file mode 100644 index 0000000..30e53f0 --- /dev/null +++ b/olpcgames/_cairoimage.py @@ -0,0 +1,69 @@ +"""Utility functions for cairo-specific operations""" +import cairo, pygame, struct +big_endian = struct.pack( '=i', 1 ) == struct.pack( '>i', 1 ) + +def newContext( width, height ): + """Create a new render-to-image context + + width, height -- pixel dimensions to be rendered + + returns surface, context for rendering + """ + csrf = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) + return csrf, cairo.Context (csrf) + +def mangle_color(color): + """Mange a colour depending on endian-ness, and swap-necessity + + This implementation has only been tested on an AMD64 + machine with a get_data implementation (rather than + a get_data_as_rgba implementation). + """ + r,g,b = color[:3] + if len(color) > 3: + a = color[3] + else: + a = 255.0 + return map(_fixColorBase, (r,g,b,a) ) + +def _fixColorBase( v ): + """Return a properly clamped colour in floating-point space""" + return max((0,min((v,255.0))))/255.0 + +def asImage( csrf ): + """Get the pixels in csrf as a Pygame image + + Note that Pygame 1.7.1 on Gentoo AMD64 is incorrectly calculating + the required size of the arrays, so this code will *not* work on that + platform with that version of the library. Pygame-ctypes does work + correctly. + """ + # Create and return a new Pygame Image derived from the Cairo Surface + format = 'ARGB' + if hasattr(csrf,'get_data'): + # more recent API, native-format, but have to (potentially) convert the format... + data = csrf.get_data() + if not big_endian: + # we use array here because it's considerably lighter-weight + # to import than the numpy module + import array + a = array.array( 'I' ) + a.fromstring( data ) + a.byteswap() + data = a.tostring() + else: + data = str(data) # there's one copy + else: + # older api, not native, but we know what it is... + data = csrf.get_data_as_rgba() + data = str(data) # there's one copy + width, height = csrf.get_width(),csrf.get_height() + try: + return pygame.image.fromstring( + data, + (width,height), + format + ) # there's the next + except ValueError, err: + err.args += (len(data), (width,height), width*height*4,format ) + raise diff --git a/olpcgames/activity.py b/olpcgames/activity.py new file mode 100644 index 0000000..45a6a69 --- /dev/null +++ b/olpcgames/activity.py @@ -0,0 +1,162 @@ +"""Embeds the Canvas widget into a Sugar-specific Activity environment""" +import logging +logging.root.setLevel( logging.WARN ) +log = logging.getLogger( 'olpcgames.activity' ) +#log.setLevel( logging.INFO ) + +import pygtk +pygtk.require('2.0') +import gtk +import gtk.gdk + +from sugar.activity import activity +from sugar.graphics import style +from olpcgames.canvas import PyGameCanvas +from olpcgames import mesh, util + +__all__ = ['PyGameActivity'] + +class PyGameActivity(activity.Activity): + """PyGame-specific activity type, provides boilerplate toolbar, creates canvas + + Subclass Overrides: + + game_name -- specifies a fully-qualified name for the game's main-loop + format like so: + 'package.module:main' + if not function name is provided, "main" is assumed. + game_handler -- alternate specification via direct reference to a main-loop + function + + game_size -- two-value tuple specifying the size of the display in pixels, + this is currently static, so once the window is created it cannot be + changed. + + If None, use the bulk of the screen for the PyGame surface based on + the values reported by the gtk.gdk functions. Note that None is + *not* the default value. + + game_title -- title to be displayed in the Sugar Shell UI + + pygame_mode -- chooses the rendering engine used for handling the + PyGame drawing mode, 'SDL' chooses the standard PyGame renderer, + 'Cairo' chooses the experimental pygamecairo renderer. + + PYGAME_CANVAS_CLASS -- normally PyGameCanvas, but can be overridden + if you want to provide a different canvas class, e.g. to provide a different + internal layout. Note: only used where pygame_mode == 'SDL' + + The Activity, once created, will be made available as olpcgames.ACTIVITY, + and that access mechanism should allow code to test for the presence of the + activity before accessing Sugar-specific functionality. + + XXX Note that currently the toolbar and window layout are hard-coded into + this super-class, with no easy way of overriding without completely rewriting + the __init__ method. We should allow for customising both the UI layout and + the toolbar contents/layout/connection. + + XXX Note that if you change the title of your activity in the toolbar you may + see the same focus issues as we have patched around in the build_toolbar + method. If so, please report them to Mike Fletcher. + """ + game_name = None + game_title = 'PyGame Game' + game_handler = None + game_size = (16 * style.GRID_CELL_SIZE, + 11 * style.GRID_CELL_SIZE) + pygame_mode = 'SDL' + + def __init__(self, handle): + """Initialise the Activity with the activity-description handle""" + super(PyGameActivity, self).__init__(handle) + self.make_global() + if self.game_size is None: + width,height = gtk.gdk.screen_width(), gtk.gdk.screen_height() + log.info( 'Total screen size: %s %s', width,height) + # for now just fudge the toolbar size... + self.game_size = width, height - (1*style.GRID_CELL_SIZE) + self.set_title(self.game_title) + toolbar = self.build_toolbar() + log.debug( 'Toolbar size: %s', toolbar.get_size_request()) + canvas = self.build_canvas() + + def make_global( self ): + """Hack to make olpcgames.ACTIVITY point to us + """ + import weakref, olpcgames + assert not olpcgames.ACTIVITY, """Activity.make_global called twice, have you created two Activity instances in a single process?""" + olpcgames.ACTIVITY = weakref.proxy( self ) + + def build_toolbar( self ): + """Build our Activity toolbar for the Sugar system + + This is a customisation point for those games which want to + provide custom toolbars when running under Sugar. + """ + toolbar = activity.ActivityToolbar(self) + toolbar.show() + self.set_toolbox(toolbar) + def shared_cb(*args, **kwargs): + log.info( 'shared: %s, %s', args, kwargs ) + try: + mesh.activity_shared(self) + except Exception, err: + log.error( """Failure signaling activity sharing to mesh module: %s""", util.get_traceback(err) ) + else: + log.info( 'mesh activity shared message sent, trying to grab focus' ) + try: + self._pgc.grab_focus() + except Exception, err: + log.warn( 'Focus failed: %s', err ) + else: + log.info( 'asserting focus' ) + assert self._pgc.is_focus(), """Did not successfully set pygame canvas focus""" + log.info( 'callback finished' ) + + def joined_cb(*args, **kwargs): + log.info( 'joined: %s, %s', args, kwargs ) + mesh.activity_joined(self) + self._pgc.grab_focus() + self.connect("shared", shared_cb) + self.connect("joined", joined_cb) + + if self.get_shared(): + # if set at this point, it means we've already joined (i.e., + # launched from Neighborhood) + joined_cb() + + toolbar.title.unset_flags(gtk.CAN_FOCUS) + return toolbar + + PYGAME_CANVAS_CLASS = PyGameCanvas + def build_canvas( self ): + """Construct the PyGame or PyGameCairo canvas for drawing""" + assert self.game_handler or self.game_name, 'You must specify a game_handler or game_name on your Activity (%r)'%( + self.game_handler or self.game_name + ) + if self.pygame_mode != 'Cairo': + self._pgc = self.PYGAME_CANVAS_CLASS(*self.game_size) + self.set_canvas(self._pgc) + self._pgc.grab_focus() + self._pgc.connect_game(self.game_handler or self.game_name) + gtk.gdk.threads_init() + return self._pgc + else: + import hippo + self._drawarea = gtk.DrawingArea() + canvas = hippo.Canvas() + canvas.grab_focus() + self.set_canvas(canvas) + self.show_all() + + import pygamecairo + pygamecairo.install() + + pygamecairo.display.init(canvas) + app = self.game_handler or self.game_name + if ':' not in app: + app += ':main' + mod_name, fn_name = app.split(':') + mod = __import__(mod_name, globals(), locals(), []) + fn = getattr(mod, fn_name) + fn() diff --git a/olpcgames/camera.py b/olpcgames/camera.py new file mode 100644 index 0000000..b51a394 --- /dev/null +++ b/olpcgames/camera.py @@ -0,0 +1,235 @@ +"""Accesses OLPC Camera functionality via gstreamer + +Depends upon: + pygame + python-gstreamer +""" +import threading +import logging +import time +import os +import pygame +import gst +from olpcgames.util import get_activity_root + +log = logging.getLogger( 'olpcgames.camera' ) +#log.setLevel( logging.DEBUG ) + +CAMERA_LOAD = 9917 +CAMERA_LOAD_FAIL = 9918 + +class CameraSprite(object): + """Create gstreamer surface for the camera.""" + def __init__(self, x, y): + import olpcgames + if olpcgames.WIDGET: + self._init_video(olpcgames.WIDGET, x, y) + + def _init_video(self, widget, x, y): + from olpcgames import video + self._vid = video.VideoWidget() + widget._fixed.put(self._vid, x, y) + self._vid.show() + + self.player = video.Player(self._vid) + self.player.play() + +class Camera(object): + """A class representing a still-picture camera + + Produces a simple gstreamer bus that terminates in a filesink, that is, + it stores the results in a file. When a picture is "snapped" the gstreamer + stream is iterated until it finishes processing and then the file can be + read. + + There are two APIs available, a synchronous API which can potentially + stall your activity's GUI (and is NOT recommended) and an + asynchronous API which returns immediately and delivers the captured + camera image via a Pygame event. To be clear, it is recommended + that you use the snap_async method, *not* the snap method. + + Note: + + The Camera class is simply a convenience wrapper around a fairly + straightforward gstreamer bus. If you have more involved + requirements for your camera manipulations you will probably + find it easier to write your own camera implementation than to + use this one. Basically we provide here the "normal" use case of + snapping a picture into a pygame image. + """ + _aliases = { + 'camera': 'v4l2src', + 'test': 'videotestsrc', + 'testing': 'videotestsrc', + 'png': 'pngenc', + 'jpeg': 'jpegenc', + 'jpg': 'jpegenc', + } + def __init__(self, source='camera', format='png', filename='snap.png', directory = None): + """Initialises the Camera's internal description + + source -- the gstreamer source for the video to capture, useful values: + 'v4l2src','camera' -- the camera + 'videotestsrc','test' -- test pattern generator source + format -- the gstreamer encoder to use for the capture, useful values: + 'pngenc','png' -- PNG format graphic + 'jpegenc','jpg','jpeg' -- JPEG format graphic + filename -- the filename to use for the capture + directory -- the directory in which to create the temporary file, defaults + to get_activity_root() + 'tmp' + """ + self.source = self._aliases.get( source, source ) + self.format = self._aliases.get( format, format ) + self.filename = filename + self.directory = directory + SNAP_PIPELINE = '%(source)s ! ffmpegcolorspace ! %(format)s ! filesink location="%(filename)s"' + def _create_pipe( self ): + """Method to create the cstreamer pipe from our settings""" + if not self.directory: + path = os.path.join( get_activity_root(), 'tmp' ) + try: + os.makedirs( path ) + log.info( 'Created temporary directory: %s', path ) + except (OSError,IOError), err: + pass + else: + path = self.directory + filename = os.path.join( path, self.filename ) + format = self.format + source = self.source + pipeline = self.SNAP_PIPELINE % locals() + log.debug( 'Background thread processing: %s', pipeline ) + return filename, gst.parse_launch(pipeline) + + def snap(self): + """Snap a picture via the camera by iterating gstreamer until finished + + Note: this is an unsafe implementation, it will cause the whole + activity to hang if the operation happens to fail! It is strongly + recommended that you use snap_async instead of snap! + """ + log.debug( 'Starting snap' ) + filename, pipe = self._create_pipe() + pipe.set_state(gst.STATE_PLAYING) + bus = pipe.get_bus() + tmp = False + while True: + event = self.bus.poll(gst.MESSAGE_STATE_CHANGED, 5) + if event: + old, new, pending = event.parse_state_changed() + if pending == gst.STATE_VOID_PENDING: + if tmp: + break + else: + tmp = True + else: + break + log.log( 'Ending snap, loading: %s', filename ) + return self._load_and_clean( filename ) + def _load_and_clean( self, filename ): + """Use pygame to load given filename, delete after loading/attempt""" + try: + log.info( 'Loading snapshot file: %s', filename ) + return pygame.image.load(filename) + finally: + try: + os.remove( filename ) + except (IOError,OSError), err: + pass + def snap_async( self, token=None ): + """Snap a picture asynchronously generating event on success/failure + + token -- passed back as attribute of the event which signals that capture + is finished + + We return two types of events CAMERA_LOAD and CAMERA_LOAD_FAIL, + depending on whether we succeed or not. Attributes of the events which + are returned: + + token -- as passed to this method + filename -- the filename in our temporary directory we used to store + the file temporarily + image -- pygame image.load result if successful, None otherwise + err -- Exception instance if failed, None otherwise + + Basically identical to the snap method, save that it posts a message + to the event bus in eventwrap instead of blocking and returning... + """ + log.debug( 'beginning async snap') + t = threading.Thread(target=self._background_snap, args=(token,)) + t.start() + log.debug( 'background thread started for gstreamer' ) + return token + + def _background_snap( + self, + token = None, + ): + """Process gst messages until pipe is finished + + pipe -- gstreamer pipe definition for parse_launch, normally it will + produce a file into which the camera should store an image + + We consider pipe to be finished when we have had two "state changed" + gstreamer events where the pending state is VOID, the first for when + we begin playing, the second for when we finish. + """ + log.debug( 'Background thread kicking off gstreamer capture begun' ) + from olpcgames import eventwrap + from pygame.event import Event + filename, pipe = self._create_pipe() + bus = pipe.get_bus() + bus.add_signal_watch() + def _background_snap_onmessage( bus, message ): + """Handle messages from the picture-snapping bus""" + log.debug( 'Message handler for gst messages: %s', message ) + t = message.type + if t == gst.MESSAGE_EOS: + pipe.set_state(gst.STATE_NULL) + try: + image = self._load_and_clean( filename ) + success = True + except Exception, err: + success = False + image = None + else: + err = None + log.debug( 'Success loading file %r', token ) + eventwrap.post(Event( + CAMERA_LOAD, + filename=filename, + success = success, + token = token, + image=image, + err=err + )) + + elif t == gst.MESSAGE_ERROR: + log.warn( 'Failure loading file %r: %s', token, message ) + pipe.set_state(gst.STATE_NULL) + err, debug = message.parse_error() + eventwrap.post(Event( + CAMERA_LOAD_FAIL, + filename=filename, + success = False, + token = token, + image=None, + err=err + )) + return False + bus.connect('message', _background_snap_onmessage) + pipe.set_state(gst.STATE_PLAYING) + +def snap(): + """Dump a snapshot from the camera to a pygame surface in background thread + + See Camera.snap + """ + return Camera().snap() + +def snap_async( token=None, **named ): + """Dump snapshot from camera return asynchronously as event in Pygame + + See Camera.snap_async + """ + return Camera(**named).snap_async( token ) diff --git a/olpcgames/canvas.py b/olpcgames/canvas.py new file mode 100644 index 0000000..0874d2d --- /dev/null +++ b/olpcgames/canvas.py @@ -0,0 +1,124 @@ +"""Implements bridge connection between Sugar/GTK and PyGame""" +import os +import sys +import logging +log = logging.getLogger( 'olpcgames.canvas' ) +#log.setLevel( logging.DEBUG ) +import threading +from pprint import pprint + +import pygtk +pygtk.require('2.0') +import gtk +import gobject +import pygame + +from olpcgames import gtkEvent, util + +__all__ = ['PyGameCanvas'] + +class PyGameCanvas(gtk.Layout): + """Canvas providing bridge methods to run PyGame in GTK + + The PyGameCanvas creates a secondary thread in which the Pygame instance will + live, providing synthetic PyGame events to that thread via a Queue. The GUI + connection is done by having the PyGame canvas use a GTK Port object as it's + window pointer, it draws to that X-level window in order to produce output. + """ + def __init__(self, width, height): + """Initializes the Canvas Object + + width,height -- passed to the inner EventBox in order to request a given size, + the Socket is the only child of this EventBox, and the PyGame commands + will be writing to the Window ID of the socket. The internal EventBox is + centered via an Alignment instance within the PyGameCanvas instance. + + XXX Should refactor so that the internal setup can be controlled by the + sub-class, e.g. to get size from the host window, or something similar. + """ + # Build the main widget + super(PyGameCanvas,self).__init__() + self.set_flags(gtk.CAN_FOCUS) + + # Build the sub-widgets + self._align = gtk.Alignment(0.5, 0.5) + self._inner_evb = gtk.EventBox() + self._socket = gtk.Socket() + + + # Add internal widgets + self._inner_evb.set_size_request(width, height) + self._inner_evb.add(self._socket) + + self._socket.show() + + self._align.add(self._inner_evb) + self._inner_evb.show() + + self._align.show() + + self.put(self._align, 0,0) + + # Construct a gtkEvent.Translator + self._translator = gtkEvent.Translator(self, self._inner_evb) + # + self.show() + def connect_game(self, app): + """Imports the given main-loop and starts processing in secondary thread + + app -- fully-qualified Python path-name for the game's main-loop, with + name within module as :functionname, if no : character is present then + :main will be assumed. + + Side effects: + + Sets the SDL_WINDOWID variable to our socket's window ID + Calls PyGame init + Causes the gtkEvent.Translator to "hook" PyGame + Creates and starts secondary thread for Game/PyGame event processing. + """ + # Setup the embedding + os.environ['SDL_WINDOWID'] = str(self._socket.get_id()) + #print 'Socket ID=%s'%os.environ['SDL_WINDOWID'] + pygame.init() + + self._translator.hook_pygame() + + # Load the modules + # NOTE: This is delayed because pygame.init() must come after the embedding is up + if ':' not in app: + app += ':main' + mod_name, fn_name = app.split(':') + mod = __import__(mod_name, globals(), locals(), []) + fn = getattr(mod, fn_name) + + # Start Pygame + self.__thread = threading.Thread(target=self._start, args=[fn]) + self.__thread.start() + + def _start(self, fn): + """The method that actually runs in the background thread""" + import olpcgames + olpcgames.widget = olpcgames.WIDGET = self + try: + import sugar.activity.activity,os + except ImportError, err: + log.info( """Running outside Sugar""" ) + else: + try: + os.chdir(sugar.activity.activity.get_bundle_path()) + except KeyError, err: + pass + + try: + try: + log.info( '''Running mainloop: %s''', fn ) + fn() + except Exception, err: + log.error( + """Uncaught top-level exception: %s""", + util.get_traceback( err ), + ) + raise + finally: + gtk.main_quit() diff --git a/olpcgames/data/__init__.py b/olpcgames/data/__init__.py new file mode 100644 index 0000000..8510186 --- /dev/null +++ b/olpcgames/data/__init__.py @@ -0,0 +1,36 @@ +"""Design-time __init__.py for resourcepackage + +This is the scanning version of __init__.py for your +resource modules. You replace it with a blank or doc-only +init when ready to release. +""" +try: + __file__ +except NameError: + pass +else: + import os + if os.path.splitext(os.path.basename( __file__ ))[0] == "__init__": + try: + from resourcepackage import package, defaultgenerators + generators = defaultgenerators.generators.copy() + + ### CUSTOMISATION POINT + ## import specialised generators here, such as for wxPython + #from resourcepackage import wxgenerators + #generators.update( wxgenerators.generators ) + except ImportError: + pass + else: + package = package.Package( + packageName = __name__, + directory = os.path.dirname( os.path.abspath(__file__) ), + generators = generators, + ) + package.scan( + ### CUSTOMISATION POINT + ## force true -> always re-loads from external files, otherwise + ## only reloads if the file is newer than the generated .py file. + # force = 1, + ) + diff --git a/olpcgames/data/sleeping_svg.py b/olpcgames/data/sleeping_svg.py new file mode 100644 index 0000000..fa67eee --- /dev/null +++ b/olpcgames/data/sleeping_svg.py @@ -0,0 +1,501 @@ +# -*- coding: ISO-8859-1 -*- +"""Resource sleeping_svg (from file sleeping.svg)""" +# written by resourcepackage: (1, 0, 1) +source = 'sleeping.svg' +package = 'olpcgames.data' +data = "\012\012\012 \012 \012 \012 \012 \ +\012 \012 \ +\012 \012 \012 \012 \012 \ + \012 image/s\ +vg+xml\012 \012 \ +\012 \012 \012 \012 \012 \012 \012 \012 \012 \012\012" +### end diff --git a/olpcgames/eventwrap.py b/olpcgames/eventwrap.py new file mode 100644 index 0000000..8fca462 --- /dev/null +++ b/olpcgames/eventwrap.py @@ -0,0 +1,306 @@ +"""Provides substitute for Pygame's "event" module using gtkEvent + +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. + +Extensions: + + last_event_time() -- returns period since the last event was produced + in seconds. This can be used to create "pausing" effects for games. + + 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 +import Queue +import thread +import logging + +log = logging.getLogger( 'olpcgames.eventwrap' ) + +from pygame.event import Event, event_name, pump as pygame_pump, get as pygame_get + +class Event(object): + """Mock pygame events""" + def __init__(self, type, dict=None,**named): + """Initialise the new event variables from dictionary and named become attributes""" + self.type = type + if dict: + self.__dict__.update( dict ) + self.__dict__.update( named ) + def _get_dict( self ): + return self.__dict__ + dict = property( _get_dict ) + +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). + """ + log.info( 'Installing OLPCGames event wrapper' ) + import eventwrap,pygame + pygame.event = eventwrap + import sys + sys.modules["pygame.event"] = eventwrap + + +# Event queue: +class _FilterQueue( Queue.Queue ): + """Simple Queue sub-class with a put_left method""" + 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 + pops the event off the queue before we get around to it. + """ + self.not_empty.acquire() + try: + if not block: + if self._empty_type( filterFunction ): + raise Queue.Empty + elif timeout is None: + while self._empty_type( filterFunction ): + self.not_empty.wait() + else: + if timeout < 0: + raise ValueError("'timeout' must be a positive number") + endtime = _time() + timeout + while self._empty_type( filterFunction ): + remaining = endtime - _time() + if remaining <= 0.0: + raise Queue.Empty + self.not_empty.wait(remaining) + item = self._get_type( filterFunction ) + self.not_full.notify() + return item + finally: + self.not_empty.release() + def _empty_type( self, filterFunction ): + """Are we empty with respect to filterFunction?""" + for element in self.queue: + if filterFunction( element ): + 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 + # someone popped the event off the queue before we got to it! + raise Queue.Empty + def peek_type( self, filterFunction= lambda x: True ): + """Peek to see if we have filterFunction-matching element + + Note: obviously this is *not* thread safe, it's just informative... + """ + try: + for element in self.queue: + if filterFunction( element ): + return element + return None + except RuntimeError, err: + return None # none yet, at least + +g_events = _FilterQueue() + +# Set of blocked events as set by set +g_blocked = set() +g_blockedlock = thread.allocate_lock() +g_blockAll = False + +def _typeChecker( types ): + """Create check whether an event is in types""" + try: + if 1 in types: + pass + def check( element ): + return element.type in types + return check + except TypeError, err: + def check( element ): + return element.type == types + return check + +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( 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 + 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 + 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 + the re-ordering of the queue on filtering! + """ + pump() + eventlist = [] + try: + if types: + check = _typeChecker( types ) + while True: + eventlist.append(g_events.get_type( check, block=False)) + else: + 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 ) + if eventlist: + _set_last_event_time() + return eventlist + +_LAST_EVENT_TIME = 0 + +def _set_last_event_time( time=None ): + """Set this as the last event time""" + global _LAST_EVENT_TIME + if time is None: + time = pygame.time.get_ticks() + _LAST_EVENT_TIME = time + return time + +def last_event_time( ): + """Return the last event type for pausing operations in seconds""" + global _LAST_EVENT_TIME + return (pygame.time.get_ticks() - _LAST_EVENT_TIME)/1000. + +def poll(): + """Get the next pending event if exists. Otherwise, return pygame.NOEVENT.""" + pump() + try: + result = g_events.get(block=False) + _set_last_event_time() + return result + except Queue.Empty: + return Event(pygame.NOEVENT) + + +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 + is an OLPCGames-specific extension. + """ + pump() + try: + result = g_events.get(block=True, timeout=timeout) + _set_last_event_time() + return result + except Queue.Empty, err: + return 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 + of integers/longs or an integer/long. + """ + if types: + check = _typeChecker( types ) + return g_events.peek_type( check ) is not None + return not g_events.empty() + +def clear(): + """Clears the entire pending queue of events + + Rarely used + """ + try: + while True: + g_events.get(block=False) + except Queue.Empty: + pass + +def set_blocked(item): + """Block item/items from being added to the event queue""" + 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): + """Allow item/items to be added to the event queue""" + 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): + """This method will not be implemented""" + +def get_grab(): + """This method will not be implemented""" + +def post(event): + """Post a new event to the Queue of events""" + 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,) diff --git a/olpcgames/gtkEvent.py b/olpcgames/gtkEvent.py new file mode 100644 index 0000000..ce4f9eb --- /dev/null +++ b/olpcgames/gtkEvent.py @@ -0,0 +1,270 @@ +"""gtkEvent.py: translate GTK events into Pygame events.""" +import pygtk +pygtk.require('2.0') +import gtk +import gobject +import pygame +from olpcgames import eventwrap +import logging +log = logging.getLogger( 'olpcgames.gtkevent' ) +#log.setLevel( logging.DEBUG ) + +class _MockEvent(object): + """Used to inject key-repeat events on the gtk side.""" + def __init__(self, keyval): + self.keyval = keyval + +class Translator(object): + """Utility class to translate GTK events into Pygame events + + The Translator object interprets incoming GTK events and generates + Pygame events in the eventwrap module's queue as a result. + It also handles generating Pygame style key-repeat events + by synthesizing them via a GTK timer. + """ + key_trans = { + 'Alt_L': pygame.K_LALT, + 'Alt_R': pygame.K_RALT, + 'Control_L': pygame.K_LCTRL, + 'Control_R': pygame.K_RCTRL, + 'Shift_L': pygame.K_LSHIFT, + 'Shift_R': pygame.K_RSHIFT, + 'Super_L': pygame.K_LSUPER, + 'Super_R': pygame.K_RSUPER, + 'KP_Page_Up' : pygame.K_KP9, + 'KP_Page_Down' : pygame.K_KP3, + 'KP_End' : pygame.K_KP1, + 'KP_Home' : pygame.K_KP7, + 'KP_Up' : pygame.K_KP8, + 'KP_Down' : pygame.K_KP2, + 'KP_Left' : pygame.K_KP4, + 'KP_Right' : pygame.K_KP6, + + } + + mod_map = { + pygame.K_LALT: pygame.KMOD_LALT, + pygame.K_RALT: pygame.KMOD_RALT, + pygame.K_LCTRL: pygame.KMOD_LCTRL, + pygame.K_RCTRL: pygame.KMOD_RCTRL, + pygame.K_LSHIFT: pygame.KMOD_LSHIFT, + pygame.K_RSHIFT: pygame.KMOD_RSHIFT, + } + + def __init__(self, mainwindow, mouselistener=None): + """Initialise the Translator with the windows to which to listen""" + # _inner_evb is Mouselistener + self._mainwindow = mainwindow + if mouselistener is None: + mouselistener = mainwindow + + self._inner_evb = mouselistener + + # Need to set our X event masks so we see mouse motion and stuff -- + mainwindow.set_events( + gtk.gdk.KEY_PRESS_MASK | \ + gtk.gdk.KEY_RELEASE_MASK \ + ) + + self._inner_evb.set_events( + gtk.gdk.POINTER_MOTION_MASK | \ + gtk.gdk.POINTER_MOTION_HINT_MASK | \ + gtk.gdk.BUTTON_MOTION_MASK | \ + gtk.gdk.BUTTON_PRESS_MASK | \ + gtk.gdk.BUTTON_RELEASE_MASK + ) + + # Callback functions to link the event systems + mainwindow.connect('unrealize', self._quit) + mainwindow.connect('key_press_event', self._keydown) + mainwindow.connect('key_release_event', self._keyup) + self._inner_evb.connect('button_press_event', self._mousedown) + self._inner_evb.connect('button_release_event', self._mouseup) + self._inner_evb.connect('motion-notify-event', self._mousemove) + + # You might need to do this + mainwindow.set_flags(gtk.CAN_FOCUS) + self._inner_evb.set_flags(gtk.CAN_FOCUS) + + # Internal data + self.__stopped = False + self.__keystate = [0] * 323 + self.__button_state = [0,0,0] + self.__mouse_pos = (0,0) + self.__repeat = (None, None) + self.__held = set() + self.__held_time_left = {} + self.__held_last_time = {} + self.__tick_id = None + + #print "translator initialized" + mainwindow.connect( 'expose-event', self.do_expose_event ) + 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 + def hook_pygame(self): + """Hook the various Pygame features so that we implement the event APIs""" + # Pygame should be initialized. Hijack their key and mouse methods + pygame.key.get_pressed = self._get_pressed + pygame.key.set_repeat = self._set_repeat + pygame.mouse.get_pressed = self._get_mouse_pressed + pygame.mouse.get_pos = self._get_mouse_pos + import eventwrap + eventwrap.install() + + def _quit(self, data=None): + self.__stopped = True + eventwrap.post(eventwrap.Event(pygame.QUIT)) + + def _keydown(self, widget, event): + key = event.keyval + log.debug( 'key down: %s', key ) + if key in self.__held: + return True + else: + if self.__repeat[0] is not None: + self.__held_last_time[key] = pygame.time.get_ticks() + self.__held_time_left[key] = self.__repeat[0] + self.__held.add(key) + + return self._keyevent(widget, event, pygame.KEYDOWN) + + def _keyup(self, widget, event): + key = event.keyval + if self.__repeat[0] is not None: + if key in self.__held: + # This is possibly false if set_repeat() is called with a key held + del self.__held_time_left[key] + del self.__held_last_time[key] + self.__held.discard(key) + + return self._keyevent(widget, event, pygame.KEYUP) + + def _keymods(self): + """Extract the keymods as they stand currently.""" + mod = 0 + for key_val, mod_val in self.mod_map.iteritems(): + mod |= self.__keystate[key_val] and mod_val + return mod + + + 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()) + 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 _get_pressed(self): + """Retrieve map/array of which keys are currently depressed (held down)""" + return self.__keystate + + def _get_mouse_pressed(self): + """Return three-element array of which mouse-buttons are currently depressed (held down)""" + return self.__button_state + + def _mousedown(self, widget, event): + self.__button_state[event.button-1] = 1 + return self._mouseevent(widget, event, pygame.MOUSEBUTTONDOWN) + + def _mouseup(self, widget, event): + self.__button_state[event.button-1] = 0 + return self._mouseevent(widget, event, pygame.MOUSEBUTTONUP) + + def _mouseevent(self, widget, event, type): + + evt = eventwrap.Event(type, + button=event.button, + pos=(event.x, event.y)) + self._post(evt) + return True + + 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 _tick(self): + """Generate synthetic events for held-down keys""" + cur_time = pygame.time.get_ticks() + for key in self.__held: + delta = cur_time - self.__held_last_time[key] + self.__held_last_time[key] = cur_time + + self.__held_time_left[key] -= delta + if self.__held_time_left[key] <= 0: + self.__held_time_left[key] = self.__repeat[1] + self._keyevent(None, _MockEvent(key), pygame.KEYDOWN) + + return True + + def _set_repeat(self, delay=None, interval=None): + """Set the key-repetition frequency for held-down keys""" + if delay is not None and self.__repeat[0] is None: + self.__tick_id = gobject.timeout_add(10, self._tick) + elif delay is None and self.__repeat[0] is not None: + gobject.source_remove(self.__tick_id) + self.__repeat = (delay, interval) + + def _get_mouse_pos(self): + """Retrieve the current mouse position as a two-tuple of integers""" + return self.__mouse_pos + + def _post(self, evt): + try: + eventwrap.post(evt) + except pygame.error, e: + if str(e) == 'Event queue full': + print "Event queue full!" + pass + else: + raise e diff --git a/olpcgames/mesh.py b/olpcgames/mesh.py new file mode 100644 index 0000000..254089f --- /dev/null +++ b/olpcgames/mesh.py @@ -0,0 +1,398 @@ +'''mesh.py: utilities for wrapping the mesh and making it accessible to Pygame''' +import logging +log = logging.getLogger( 'olpcgames.mesh' ) +#log.setLevel( logging.DEBUG ) +try: + from sugar.presence.tubeconn import TubeConnection +except ImportError, err: + TubeConnection = object +try: + from dbus.gobject_service import ExportedGObject +except ImportError, err: + ExportedGObject = object +from dbus.service import method, signal + +try: + import telepathy +except ImportError, err: + telepathy = None + +class OfflineError( Exception ): + """Raised when we cannot complete an operation due to being offline""" + +DBUS_IFACE="org.laptop.games.pygame" +DBUS_PATH="/org/laptop/games/pygame" +DBUS_SERVICE = None + + +### NEW PYGAME EVENTS ### + +'''The tube connection was started. (i.e., the user clicked Share or started +the activity from the Neighborhood screen). +Event properties: + id: a unique identifier for this connection. (shouldn't be needed for anything)''' +CONNECT = 9912 + +'''A participant joined the activity. This will trigger for the local user +as well as any arriving remote users. +Event properties: + handle: the arriving user's handle.''' +PARTICIPANT_ADD = 9913 + +'''A participant quit the activity. +Event properties: + handle: the departing user's handle.''' +PARTICIPANT_REMOVE = 9914 + +'''A message was sent to you. +Event properties: + content: the content of the message (a string) + handle: the handle of the sending user.''' +MESSAGE_UNI = 9915 + +'''A message was sent to everyone. +Event properties: + content: the content of the message (a string) + handle: the handle of the sending user.''' +MESSAGE_MULTI = 9916 + + +# Private objects for useful purposes! +pygametubes = [] +text_chan, tubes_chan = (None, None) +conn = None +initiating = False +joining = False + +connect_callback = None + +def is_initiating(): + '''A version of is_initiator that's a bit less goofy, and can be used + before the Tube comes up.''' + global initiating + return initiating + +def is_joining(): + '''Returns True if the activity was started up by means of the + Neighbourhood mesh view.''' + global joining + return joining + +def set_connect_callback(cb): + '''Just the same as the Pygame event loop can listen for CONNECT, + this is just an ugly callback that the glib side can use to be aware + of when the Tube is ready.''' + global connect_callback + connect_callback = cb + +def activity_shared(activity): + '''Called when the user clicks Share.''' + + global initiating + initiating = True + + _setup(activity) + + + log.debug('This is my activity: making a tube...') + channel = tubes_chan[telepathy.CHANNEL_TYPE_TUBES] + if hasattr( channel, 'OfferDBusTube' ): + id = channel.OfferDBusTube( + DBUS_SERVICE, {}) + else: + id = channel.OfferTube( + telepathy.TUBE_TYPE_DBUS, DBUS_SERVICE, {}) + + global connect_callback + if connect_callback is not None: + connect_callback() + +def activity_joined(activity): + '''Called at the startup of our Activity, when the user started it via Neighborhood intending to join an existing activity.''' + + # Find out who's already in the shared activity: + log.debug('Joined an existing shared activity') + + for buddy in activity._shared_activity.get_joined_buddies(): + log.debug('Buddy %s is already in the activity' % buddy.props.nick) + + + global initiating + global joining + initiating = False + joining = True + + + _setup(activity) + + tubes_chan[telepathy.CHANNEL_TYPE_TUBES].ListTubes( + reply_handler=_list_tubes_reply_cb, + error_handler=_list_tubes_error_cb) + + global connect_callback + if connect_callback is not None: + connect_callback() + +def _getConn(): + log.info( '_getConn' ) + pservice = _get_presence_service() + name, path = pservice.get_preferred_connection() + global conn + conn = telepathy.client.Connection(name, path) + return conn + + + +def _setup(activity): + '''Determines text and tube channels for the current Activity. If no tube +channel present, creates one. Updates text_chan and tubes_chan. + +setup(sugar.activity.Activity, telepathy.client.Connection)''' + global text_chan, tubes_chan, DBUS_SERVICE + if not DBUS_SERVICE: + DBUS_SERVICE = activity.get_bundle_id() + if not activity.get_shared(): + log.error('Failed to share or join activity') + raise "Failure" + + bus_name, conn_path, channel_paths = activity._shared_activity.get_channels() + _getConn() + + # Work out what our room is called and whether we have Tubes already + room = None + tubes_chan = None + text_chan = None + for channel_path in channel_paths: + channel = telepathy.client.Channel(bus_name, channel_path) + htype, handle = channel.GetHandle() + if htype == telepathy.HANDLE_TYPE_ROOM: + log.debug('Found our room: it has handle#%d "%s"', + handle, conn.InspectHandles(htype, [handle])[0]) + room = handle + ctype = channel.GetChannelType() + if ctype == telepathy.CHANNEL_TYPE_TUBES: + log.debug('Found our Tubes channel at %s', channel_path) + tubes_chan = channel + elif ctype == telepathy.CHANNEL_TYPE_TEXT: + log.debug('Found our Text channel at %s', channel_path) + text_chan = channel + + if room is None: + log.error("Presence service didn't create a room") + raise "Failure" + if text_chan is None: + log.error("Presence service didn't create a text channel") + raise "Failure" + + # Make sure we have a Tubes channel - PS doesn't yet provide one + if tubes_chan is None: + log.debug("Didn't find our Tubes channel, requesting one...") + tubes_chan = conn.request_channel(telepathy.CHANNEL_TYPE_TUBES, + telepathy.HANDLE_TYPE_ROOM, room, True) + + tubes_chan[telepathy.CHANNEL_TYPE_TUBES].connect_to_signal('NewTube', + new_tube_cb) + + return (text_chan, tubes_chan) + +def new_tube_cb(id, initiator, type, service, params, state): + log.debug("New_tube_cb called: %s %s %s" % (id, initiator, type)) + if (type == telepathy.TUBE_TYPE_DBUS and service == DBUS_SERVICE): + if state == telepathy.TUBE_STATE_LOCAL_PENDING: + channel = tubes_chan[telepathy.CHANNEL_TYPE_TUBES] + if hasattr( channel, 'AcceptDBusTube' ): + channel.AcceptDBusTube( id ) + else: + channel.AcceptTube(id) + + tube_conn = TubeConnection(conn, + tubes_chan[telepathy.CHANNEL_TYPE_TUBES], + id, group_iface=text_chan[telepathy.CHANNEL_INTERFACE_GROUP]) + + global pygametubes, initiating + pygametubes.append(PygameTube(tube_conn, initiating, len(pygametubes))) + + +def _list_tubes_reply_cb(tubes): + for tube_info in tubes: + new_tube_cb(*tube_info) + +def _list_tubes_error_cb(e): + log.error('ListTubes() failed: %s', e) + + + +def get_buddy(dbus_handle): + """Get a Buddy from a handle.""" + log.debug('Trying to find owner of handle %s...', dbus_handle) + cs_handle = instance().tube.bus_name_to_handle[dbus_handle] + log.debug('Trying to find my handle in %s...', cs_handle) + group = text_chan[telepathy.CHANNEL_INTERFACE_GROUP] + log.debug( 'Calling GetSelfHandle' ) + my_csh = group.GetSelfHandle() + log.debug('My handle in that group is %s', my_csh) + if my_csh == cs_handle: + handle = conn.GetSelfHandle() + log.debug('CS handle %s belongs to me, %s', cs_handle, handle) + elif group.GetGroupFlags() & telepathy.CHANNEL_GROUP_FLAG_CHANNEL_SPECIFIC_HANDLES: + handle = group.GetHandleOwners([cs_handle])[0] + log.debug('CS handle %s belongs to %s', cs_handle, handle) + else: + handle = cs_handle + log.debug('non-CS handle %s belongs to itself', handle) + + # XXX: we're assuming that we have Buddy objects for all contacts - + # this might break when the server becomes scalable. + pservice = _get_presence_service() + name, path = pservice.get_preferred_connection() + return pservice.get_buddy_by_telepathy_handle(name, path, handle) + +def _get_presence_service( ): + """Attempt to retrieve the presence service (check for offline condition) + + The presence service, when offline, has no preferred connection type, + so we check that before returning the object... + """ + import sugar.presence.presenceservice + pservice = sugar.presence.presenceservice.get_instance() + try: + name, path = pservice.get_preferred_connection() + except (TypeError,ValueError), err: + log.warn('Working in offline mode, cannot retrieve buddy information for %s: %s', handle, err ) + raise OfflineError( """Unable to retrieve buddy information, currently offline""" ) + else: + return pservice + +def instance(idx=0): + return pygametubes[idx] + +import eventwrap,pygame.event as PEvent + +class PygameTube(ExportedGObject): + '''The object whose instance is shared across D-bus + + Call instance() to get the instance of this object for your activity service. + Its 'tube' property contains the underlying D-bus Connection. + ''' + def __init__(self, tube, is_initiator, tube_id): + super(PygameTube, self).__init__(tube, DBUS_PATH) + log.info( 'PygameTube init' ) + self.tube = tube + self.is_initiator = is_initiator + self.entered = False + self.ordered_bus_names = [] + eventwrap.post(PEvent.Event(CONNECT, id=tube_id)) + + if not self.is_initiator: + self.tube.add_signal_receiver(self.new_participant_cb, 'NewParticipants', DBUS_IFACE, path=DBUS_PATH) + self.tube.watch_participants(self.participant_change_cb) + self.tube.add_signal_receiver(self.broadcast_cb, 'Broadcast', DBUS_IFACE, path=DBUS_PATH, sender_keyword='sender') + + + def participant_change_cb(self, added, removed): + log.debug( 'participant_change_cb: %s %s', added, removed ) + def nick(buddy): + if buddy is not None: + return buddy.props.nick + else: + return 'Unknown' + + for handle, bus_name in added: + dbus_handle = self.tube.participants[handle] + self.ordered_bus_names.append(dbus_handle) + eventwrap.post(PEvent.Event(PARTICIPANT_ADD, handle=dbus_handle)) + + for handle in removed: + dbus_handle = self.tube.participants[handle] + self.ordered_bus_names.remove(dbus_handle) + eventwrap.post(PEvent.Event(PARTICIPANT_REMOVE, handle=dbus_handle)) + + if self.is_initiator: + if not self.entered: + # Initiator will broadcast a new ordered_bus_names each time + # a participant joins. + self.ordered_bus_names = [self.tube.get_unique_name()] + self.NewParticipants(self.ordered_bus_names) + + self.entered = True + + @signal(dbus_interface=DBUS_IFACE, signature='as') + def NewParticipants(self, ordered_bus_names): + '''This is the NewParticipants signal, sent when the authoritative list of ordered_bus_names changes.''' + log.debug("sending NewParticipants: %s" % ordered_bus_names) + pass + + @signal(dbus_interface=DBUS_IFACE, signature='s') + def Broadcast(self, content): + '''This is the Broadcast signal; it sends a message to all other activity participants.''' + pass + + @method(dbus_interface=DBUS_IFACE, in_signature='s', out_signature='', sender_keyword='sender') + def Tell(self, content, sender=None): + '''This is the targeted-message interface; called when a message is received that was sent directly to me.''' + eventwrap.post(PEvent.Event(MESSAGE_UNI, handle=sender, content=content)) + + def broadcast_cb(self, content, sender=None): + '''This is the Broadcast callback, fired when someone sends a Broadcast signal along the bus.''' + eventwrap.post(PEvent.Event(MESSAGE_MULTI, handle=sender, content=content)) + + def new_participant_cb(self, new_bus_names): + '''This is the NewParticipants callback, fired when someone joins or leaves.''' + log.debug("new participant. new bus names %s, old %s" % (new_bus_names, self.ordered_bus_names)) + if self.ordered_bus_names != new_bus_names: + log.warn("ordered bus names out of sync with server, resyncing") + self.ordered_bus_names = new_bus_names + +def send_to(handle, content=""): + '''Sends the given message to the given buddy identified by handle.''' + log.debug( 'send_to: %s %s', handle, content ) + remote_proxy = dbus_get_object(handle, DBUS_PATH) + remote_proxy.Tell(content, reply_handler=dbus_msg, error_handler=dbus_err) + +def dbus_msg(): + log.debug("async reply to send_to") +def dbus_err(e): + log.error("async error: %s" % e) + +def broadcast(content=""): + '''Sends the given message to all participants.''' + log.debug( 'Broadcast: %s', content ) + instance().Broadcast(content) + +def my_handle(): + '''Returns the handle of this user + + Note, you can get a DBusException from this if you have + not yet got a unique ID assigned by the bus. You may need + to delay calling until you are sure you are connected. + ''' + log.debug( 'my handle' ) + return instance().tube.get_unique_name() + +def is_initiator(): + '''Returns the handle of this user.''' + log.debug( 'is initiator' ) + return instance().is_initiator + +def get_participants(): + '''Returns the list of active participants, in order of arrival. + List is maintained by the activity creator; if that person leaves it may not stay in sync.''' + log.debug( 'get_participants' ) + try: + return instance().ordered_bus_names[:] + except IndexError, err: + return [] # no participants yet, as we don't yet have a connection + +def dbus_get_object(handle, path): + '''Get a D-bus object from another participant. + + This is how you can communicate with other participants using + arbitrary D-bus objects without having to manage the participants + yourself. + + Simply define a D-bus class with an interface and path that you + choose; when you want a reference to the corresponding remote + object on a participant, call this method. + ''' + log.debug( 'dbus_get_object: %s %s', handle, path ) + return instance().tube.get_object(handle, path) diff --git a/olpcgames/pangofont.py b/olpcgames/pangofont.py new file mode 100644 index 0000000..81a2d7c --- /dev/null +++ b/olpcgames/pangofont.py @@ -0,0 +1,279 @@ +"""Implement Pygame's font interface using Pango for international support + +Depends on: + + pygtk (to get the pango context) + pycairo (for the pango rendering context) + python-pango (obviously) + pygame (obviously) +""" +import pango +import logging +import cairo +import pangocairo +import pygame.rect, pygame.image +import gtk +import struct +from pygame import surface +from olpcgames import _cairoimage + +log = logging.getLogger( 'olpcgames.pangofont' ) +#log.setLevel( logging.DEBUG ) + +# Install myself on top of pygame.font +def install(): + """Replace Pygame's font module with this module""" + log.info( 'installing' ) + from olpcgames import pangofont + import pygame + pygame.font = pangofont + import sys + sys.modules["pygame.font"] = pangofont + +class PangoFont(object): + """Base class for a pygame.font.Font-like object drawn by Pango + + Attributes of note: + + fd -- instances Pango FontDescription object + WEIGHT_* -- parameters for use with set_weight + STYLE_* -- parameters for use with set_style + + """ + WEIGHT_BOLD = pango.WEIGHT_BOLD + WEIGHT_HEAVY = pango.WEIGHT_HEAVY + WEIGHT_LIGHT = pango.WEIGHT_LIGHT + WEIGHT_NORMAL = pango.WEIGHT_NORMAL + WEIGHT_SEMIBOLD = pango.WEIGHT_SEMIBOLD + WEIGHT_ULTRABOLD = pango.WEIGHT_ULTRABOLD + WEIGHT_ULTRALIGHT = pango.WEIGHT_ULTRALIGHT + STYLE_NORMAL = pango.STYLE_NORMAL + STYLE_ITALIC = pango.STYLE_ITALIC + STYLE_OBLIQUE = pango.STYLE_OBLIQUE + def __init__(self, family=None, size=None, bold=False, italic=False, underline=False, fd=None): + """If you know what pango.FontDescription (fd) you want, pass it in as + 'fd'. Otherwise, specify any number of family, size, bold, or italic, + and we will try to match something up for you.""" + + # Always set the FontDescription (FIXME - only set it if the user wants + # to change something?) + if fd is None: + fd = pango.FontDescription() + if family is not None: + fd.set_family(family) + if size is not None: + fd.set_size(size*1000) + self.fd = fd + self.set_bold( bold ) + self.set_italic( italic ) + self.set_underline( underline ) + + def render(self, text, antialias=True, color=(255,255,255), background=None ): + """Render the font onto a new Surface and return it. + We ignore 'antialias' and use system settings. + + text -- (unicode) string with the text to render + antialias -- attempt to antialias the text or not + color -- three or four-tuple of 0-255 values specifying rendering + colour for the text + background -- three or four-tuple of 0-255 values specifying rendering + colour for the background, or None for trasparent background + + returns a pygame image instance + """ + log.info( 'render: %r, antialias = %s, color=%s, background=%s', text, antialias, color, background ) + + # create layout + layout = pango.Layout(gtk.gdk.pango_context_get()) + layout.set_font_description(self.fd) + if self.underline: + attrs = layout.get_attributes() + if not attrs: + attrs = pango.AttrList() + attrs.insert(pango.AttrUnderline(pango.UNDERLINE_SINGLE, 0, 32767)) + layout.set_attributes( attrs ) + layout.set_text(text) + + # determine pixel size + (logical, ink) = layout.get_pixel_extents() + ink = pygame.rect.Rect(ink) + + # Create a new Cairo ImageSurface + csrf,cctx = _cairoimage.newContext( ink.w, ink.h ) + cctx = pangocairo.CairoContext(cctx) + + # Mangle the colors on little-endian machines. The reason for this + # is that Cairo writes native-endian 32-bit ARGB values whereas + # Pygame expects endian-independent values in whatever format. So we + # tell our users not to expect transparency here (avoiding the A issue) + # and we swizzle all the colors around. + + # render onto it + if background is not None: + background = _cairoimage.mangle_color( background ) + cctx.set_source_rgba(*background) + cctx.paint() + + log.debug( 'incoming color: %s', color ) + color = _cairoimage.mangle_color( color ) + log.debug( ' translated color: %s', color ) + + cctx.new_path() + cctx.layout_path(layout) + cctx.set_source_rgba(*color) + cctx.fill() + + # Create and return a new Pygame Image derived from the Cairo Surface + return _cairoimage.asImage( csrf ) + + def set_bold( self, bold=True): + """Set our font description's weight to "bold" or "normal" + + bold -- boolean, whether to set the value to "bold" weight or not + """ + if bold: + self.set_weight( self.WEIGHT_BOLD ) + else: + self.set_weight( self.WEIGHT_NORMAL ) + def set_weight( self, weight ): + """Explicitly set our pango-style weight value""" + self.fd.set_weight( weight ) + return self.get_weight() + def get_weight( self ): + """Explicitly get our pango-style weight value""" + return self.fd.get_weight() + def get_bold( self ): + """Return whether our font's weight is bold (or above)""" + return self.fd.get_weight() >= pango.WEIGHT_BOLD + + def set_italic( self, italic=True ): + """Set our "italic" value (style)""" + if italic: + self.set_style( self.STYLE_ITALIC ) + else: + self.set_style( self.STYLE_NORMAL ) + def set_style( self, style ): + """Set our font description's pango-style""" + self.fd.set_style( style ) + return self.fd.get_style() + def get_style( self ): + """Get our font description's pango-style""" + return self.fd.get_style() + def get_italic( self ): + """Return whether we are currently italicised""" + return self.fd.get_style() == self.STYLE_ITALIC # what about oblique? + + def set_underline( self, underline=True ): + """Set our current underlining properly""" + self.underline = underline + def get_underline( self ): + return self.underline + +class SysFont(PangoFont): + """Construct a PangoFont from a font description (name), size in pixels, + bold, and italic designation. Similar to SysFont from Pygame.""" + def __init__(self, name, size, bold=False, italic=False): + fd = pango.FontDescription(name) + fd.set_absolute_size(size*pango.SCALE) + if bold: + fd.set_weight(pango.WEIGHT_BOLD) + if italic: + fd.set_style(pango.STYLE_OBLIQUE) + super(SysFont, self).__init__(fd=fd) + +# originally defined a new class, no reason for that... +NotImplemented = NotImplementedError + +class Font(PangoFont): + """Abstract class, do not use""" + def __init__(self, *args, **kwargs): + raise NotImplementedError("PangoFont doesn't support Font directly, use SysFont or .fontByDesc") + +def match_font(name,bold=False,italic=False): + """Stub, does not work, use fontByDesc instead""" + raise NotImplementedError("PangoFont doesn't support match_font directly, use SysFont or .fontByDesc") + +def fontByDesc(desc="",bold=False,italic=False): + """Constructs a FontDescription from the given string representation. +The format of the string representation is: + + "[FAMILY-LIST] [STYLE-OPTIONS] [SIZE]" + +where FAMILY-LIST is a comma separated list of families optionally terminated by a comma, STYLE_OPTIONS is a whitespace separated list of words where each WORD describes one of style, variant, weight, or stretch, and SIZE is an decimal number (size in points). For example the following are all valid string representations: + + "sans bold 12" + "serif,monospace bold italic condensed 16" + "normal 10" + +The commonly available font families are: Normal, Sans, Serif and Monospace. The available styles are: +Normal the font is upright. +Oblique the font is slanted, but in a roman style. +Italic the font is slanted in an italic style. + +The available weights are: +Ultra-Light the ultralight weight (= 200) +Light the light weight (=300) +Normal the default weight (= 400) +Bold the bold weight (= 700) +Ultra-Bold the ultra-bold weight (= 800) +Heavy the heavy weight (= 900) + +The available variants are: +Normal +Small-Caps + +The available stretch styles are: +Ultra-Condensed the smallest width +Extra-Condensed +Condensed +Semi-Condensed +Normal the normal width +Semi-Expanded +Expanded +Extra-Expanded +Ultra-Expanded the widest width + """ + fd = pango.FontDescription(name) + if bold: + fd.set_weight(pango.WEIGHT_BOLD) + if italic: + fd.set_style(pango.STYLE_OBLIQUE) + return PangoFont(fd=fd) + +def get_init(): + """Return boolean indicating whether we are initialised + + Always returns True + """ + return True + +def init(): + """Initialise the module (null operation)""" + pass + +def quit(): + """De-initialise the module (null operation)""" + pass + +def get_default_font(): + """Return default-font specification to be passed to e.g. fontByDesc""" + return "sans" + +def get_fonts(): + """Return the set of all fonts available (currently just 3 generic types)""" + return ["sans","serif","monospace"] + + +def stdcolor(color): + """Produce a 4-element 0.0-1.0 color value from input""" + def fixlen(color): + if len(color) == 3: + return tuple(color) + (255,) + elif len(color) == 4: + return color + else: + raise TypeError("What sort of color is this: %s" % (color,)) + return [_fixColorBase(x) for x in fixlen(color)] +def _fixColorBase( v ): + """Return a properly clamped colour in floating-point space""" + return max((0,min((v,255.0))))/255.0 diff --git a/olpcgames/pausescreen.py b/olpcgames/pausescreen.py new file mode 100644 index 0000000..513e9d7 --- /dev/null +++ b/olpcgames/pausescreen.py @@ -0,0 +1,90 @@ +"""Display a "paused" version of the currently-displayed screen + +This code is largely cribbed from the Pippy activity's display code, +but we try to be a little more generally usable than they are, as +we have more involved activities using the code. + +We use svgsprite to render a graphic which is stored in the +olpcgames data directory over a dimmed version of the current +screen contents. +""" +import logging +log = logging.getLogger( 'olpcgames.pausescreen' ) +import pygame +from pygame import sprite + +def get_events( sleep_timeout = 10, pause=None, **args ): + """Retrieve the set of pending events or sleep + + sleep_timeout -- dormant period before we invoke pause_screen + pause -- callable to produce visual notification of pausing, normally + by taking the current screen and modifying it in some way. Defaults + to pauseScreen in this module. If you return nothing from this + function then no restoration or display-flipping will occur + *args -- if present, passed to pause to configuration operation (e.g. + to specify a different overlaySVG file) + + returns set of pending events (potentially empty) + """ + log.debug( 'called get events' ) + if not pause: + pause = pauseScreen + events = pygame.event.get( ) + if not events: + log.info( 'No events in queue' ) + old_screen = None + if hasattr(pygame.event, 'last_event_time') and pygame.event.last_event_time() > sleep_timeout: + # we've been waiting long enough, go to sleep visually + log.warn( 'Pausing activity after %s with function %s', sleep_timeout, pause ) + old_screen = pause( ) + if old_screen: + pygame.display.flip() + # now we wait until there *are* some events (efficiently) + # and retrieve any extra events that are waiting... + events = [ pygame.event.wait() ] + pygame.event.get() + log.warn( 'Activity restarted') + if old_screen: + restoreScreen( old_screen ) + else: + log.info( 'Not running under OLPCGames' ) + return events + +def pauseScreen( overlaySVG=None ): + """Display a "Paused" screen and suspend + + This default implementation will not do anything to shut down your + simulation or other code running in other threads. It will merely block + this thread (the pygame thread) until an event shows up in the + eventwrap queue. + + Returns a surface to pass to restoreScreen to continue... + """ + from olpcgames import svgsprite + if not overlaySVG: + from olpcgames.data import sleeping_svg + overlaySVG = sleeping_svg.data + screen = pygame.display.get_surface() + old_screen = screen.copy() # save this for later. + pause_sprite = svgsprite.SVGSprite( + overlaySVG, + ) + pause_sprite.rect.center = screen.get_rect().center + group = sprite.RenderUpdates( ) + group.add( pause_sprite ) + + # dim the screen and display the 'paused' message in the center. + BLACK = (0,0,0) + WHITE = (255,255,255) + dimmed = screen.copy() + dimmed.set_alpha(128) + screen.fill(BLACK) + screen.blit(dimmed, (0,0)) + + group.draw( screen ) + return old_screen + +def restoreScreen( old_screen ): + """Restore the original screen and return""" + screen = pygame.display.get_surface() + screen.blit(old_screen, (0,0)) + return old_screen diff --git a/olpcgames/svgsprite.py b/olpcgames/svgsprite.py new file mode 100644 index 0000000..2c53178 --- /dev/null +++ b/olpcgames/svgsprite.py @@ -0,0 +1,69 @@ +"""RSVG/Cairo-based rendering of SVG into Pygame Images""" +from pygame import sprite +from olpcgames import _cairoimage +import cairo, rsvg + +class SVGSprite( sprite.Sprite ): + """Sprite class which renders SVG source-code as a Pygame image""" + rect = image = None + resolution = None + def __init__( + self, svg=None, size=None, *args + ): + """Initialise the svg sprite + + svg -- svg source text (i.e. content of an svg file) + size -- optional, to constrain size, (width,height), leaving one + as None or 0 causes proportional scaling, leaving both + as None or 0 causes natural scaling (screen resolution) + args -- if present, groups to which to automatically add + """ + self.size = size + super( SVGSprite, self ).__init__( *args ) + if svg: + self.setSVG( svg ) + def setSVG( self, svg ): + """Set our SVG source""" + self.svg = svg + # XXX could delay this until actually asked to display... + if self.size: + width,height = self.size + else: + width,height = None,None + self.image = self._render( width,height ) + rect = self.image.get_rect() + if self.rect: + rect.move( self.rect ) # should let something higher-level do that... + self.rect = rect + + def _render( self, width, height ): + """Render our SVG to a Pygame image""" + handle = rsvg.Handle( data = self.svg ) + originalSize = (width,height) + scale = 1.0 + hw,hh = handle.get_dimension_data()[:2] + if hw and hh: + if not width: + if not height: + width,height = hw,hh + else: + scale = float(height)/hh + width = hh/float(hw) * height + elif not height: + scale = float(width)/hw + height = hw/float(hh) * width + else: + # scale only, only rendering as large as it is... + if width/height > hw/hh: + # want it taller than it is... + width = hh/float(hw) * height + else: + height = hw/float(hh) * width + scale = float(height)/hh + + csrf, ctx = _cairoimage.newContext( int(width), int(height) ) + ctx.scale( scale, scale ) + handle.render_cairo( ctx ) + return _cairoimage.asImage( csrf ) + return None + diff --git a/olpcgames/util.py b/olpcgames/util.py new file mode 100644 index 0000000..f4ecbf0 --- /dev/null +++ b/olpcgames/util.py @@ -0,0 +1,68 @@ +"""Abstraction layer for working outside the Sugar environment""" +import traceback, cStringIO +import logging +log = logging.getLogger( 'olpcgames.util' ) +import os +import os.path + +NON_SUGAR_ROOT = '~/.sugar/default/olpcgames' + +try: + from sugar.activity.activity import get_bundle_path as _get_bundle_path + def get_bundle_path( ): + """Retrieve bundle path from activity with fix for silly registration bug""" + path = _get_bundle_path() + if path.endswith( '.activity.activity' ): + log.warn( '''Found double .activity suffix in bundle path, truncating: %s''', path ) + path = path[:-9] + return path +except ImportError: + log.warn( '''Do not appear to be running under Sugar, stubbing-in get_bundle_path''' ) + def get_bundle_path(): + """Retrieve a substitute data-path for non OLPC systems""" + return os.getcwd() + + +def get_activity_root( ): + """Return the activity root for data storage operations + + If the activity is present, returns the activity's root, + otherwise returns NON_SUGAR_ROOT as the directory. + """ + import olpcgames + if olpcgames.ACTIVITY: + return olpcgames.ACTIVITY.get_activity_root() + else: + return os.path.expanduser( NON_SUGAR_ROOT ) + +def data_path(file_name): + """Return the full path to a file in the data sub-directory of the bundle""" + return os.path.join(get_bundle_path(), 'data', file_name) +def tmp_path(file_name): + """Return the full path to a file in the temporary directory""" + return os.path.join(get_activity_root(), 'tmp', file_name) + +def get_traceback(error): + """Get formatted traceback from current exception + + error -- Exception instance raised + + Attempts to produce a 10-level traceback as a string + that you can log off. Use like so: + + try: + doSomething() + except Exception, err: + log.error( + '''Failure during doSomething with X,Y,Z parameters: %s''', + util.get_traceback( err ), + ) + """ + exception = str(error) + file = cStringIO.StringIO() + try: + traceback.print_exc( limit=10, file = file ) + exception = file.getvalue() + finally: + file.close() + return exception diff --git a/olpcgames/video.py b/olpcgames/video.py new file mode 100644 index 0000000..0cf9ac9 --- /dev/null +++ b/olpcgames/video.py @@ -0,0 +1,170 @@ +"""Video widget for displaying a gstreamer pipe""" +import logging +log = logging.getLogger( 'olpcgames.video' ) +#log.setLevel( logging.INFO ) +import os +import signal +import pygame +import olpcgames + +import pygtk +pygtk.require('2.0') +import gtk +import gst + +class VideoWidget(gtk.DrawingArea): + """Widget to render GStreamer video over our Pygame Canvas + + The VideoWidget is a simple GTK window which is + held by the PygameCanvas, just as is the Pygame + window we normally use. As such this approach + *cannot* work without the GTK wrapper. + + It *should* be possible to use raw X11 operations + to create a child window of the Pygame/SDL window + and use that for the same purpose, but that would + require some pretty low-level ctypes hacking. + + Attributes of Note: + + rect -- Pygame rectangle which tells us where to + display ourselves, setting the rect changes the + position and size of the window. + """ + _imagesink = None + _renderedRect = None + def __init__(self, rect=None, force_aspect_ratio=True): + super(VideoWidget, self).__init__() + self.unset_flags(gtk.DOUBLE_BUFFERED) + if rect is None: + rect = pygame.Rect( (0,0), (160,120)) + self.rect = rect + self.force_aspect_ratio = force_aspect_ratio + self.set_size_request(rect.width,rect.height) + olpcgames.WIDGET.put( self, rect.left,rect.top) + self._renderedRect = rect + self.show() + + def set_rect( self, rect ): + """Set our rectangle (area of the screen)""" + log.debug( 'Set rectangle: %s', rect ) + self.set_size_request(rect.width,rect.height) + olpcgames.WIDGET.move( self, rect.left,rect.top) + self.rect = rect + + def do_expose_event(self, event): + """Handle exposure event (trigger redraw by gst)""" + if self._imagesink: + self._imagesink.expose() + return False + else: + return True + + def set_sink(self, sink): + """Set our window-sink for output""" + assert self.window.xid + self._imagesink = sink + self._imagesink.set_xwindow_id(self.window.xid) + self._imagesink.set_property('force-aspect-ratio', self.force_aspect_ratio) + +class PygameWidget( object ): + """Render "full-screen" video to the entire Pygame screen + + Not particularly useful unless this happens to be exactly what you need. + """ + def __init__( self ): + try: + window_id = pygame.display.get_wm_info()['window'] + except KeyError, err: # pygame-ctypes... + window_id = int(os.environ['SDL_WINDOWID']) + self.window_id = window_id + self._imagesink = None + def set_sink( self, sink ): + """Set up our gst sink""" + log.info( 'Setting sink: %s', sink ) + self._imagesink = sink + sink.set_xwindow_id( self.window_id ) + +#pipe_desc = 'v4l2src ! video/x-raw-yuv,width=160,height=120 ! ffmpegcolorspace ! xvimagesink' +class Player(object): + pipe_desc = 'v4l2src ! ffmpegcolorspace ! video/x-raw-yuv ! xvimagesink' + test_pipe_desc = 'videotestsrc ! ffmpegcolorspace ! video/x-raw-yuv ! xvimagesink' + _synchronized = False + def __init__(self, videowidget, pipe_desc=pipe_desc): + self._playing = False + self._videowidget = videowidget + + self._pipeline = gst.parse_launch(pipe_desc) + + bus = self._pipeline.get_bus() + bus.enable_sync_message_emission() + bus.add_signal_watch() + bus.connect('sync-message::element', self.on_sync_message) + bus.connect('message', self.on_message) + + def play(self): + log.info( 'Play' ) + if self._playing == False: + self._pipeline.set_state(gst.STATE_PLAYING) + self._playing = True + + def pause(self): + log.info( 'Pause' ) + if self._playing == True: + if self._synchronized: + log.debug( ' pause already sync\'d' ) + self._pipeline.set_state(gst.STATE_PAUSED) + self._playing = False + def stop( self ): + """Stop all playback""" + self._pipeline.set_state( gst.STATE_NULL ) + + def on_sync_message(self, bus, message): + log.info( 'Sync: %s', message ) + if message.structure is None: + return + if message.structure.get_name() == 'prepare-xwindow-id': + self._synchronized = True + self._videowidget.set_sink(message.src) + + def on_message(self, bus, message): + log.info( 'Message: %s', message ) + t = message.type + if t == gst.MESSAGE_ERROR: + err, debug = message.parse_error() + log.warn("Video error: (%s) %s" ,err, debug) + self._playing = False + +if __name__ == "__main__": + # Simple testing code... + logging.basicConfig() + log.setLevel( logging.DEBUG ) + from pygame import image,display, event + import pygame + def main(): + display.init() + maxX,maxY = display.list_modes()[0] + screen = display.set_mode( (maxX/3, maxY/3 ) ) + + display.flip() + + pgw = PygameWidget( ) + p = Player( pgw ) + p.play() + + clock = pygame.time.Clock() + + running = True + while running: + clock.tick( 60 ) + for evt in [pygame.event.wait()] + pygame.event.get(): + if evt.type == pygame.KEYDOWN: + if p._playing: + p.pause() + else: + p.play() + elif evt.type == pygame.QUIT: + p.stop() + running = False + #display.flip() + main() diff --git a/run.py b/run.py new file mode 100644 index 0000000..3329a3a --- /dev/null +++ b/run.py @@ -0,0 +1,41 @@ +#! /usr/bin/env python +"""Skeleton project file mainloop for new OLPCGames users""" +import olpcgames, pygame, logging +from olpcgames import pausescreen +import horse + +log = logging.getLogger( 'run' ) +log.setLevel( logging.DEBUG ) + +def main(): + """The mainloop which is specified in the activity.py file + + "main" is the assumed function name + """ + size = (800,600) + #size = (16*75,11*75) + if olpcgames.ACTIVITY: + size = olpcgames.ACTIVITY.game_size + screen = pygame.display.set_mode(size) + clock = pygame.time.Clock() + game = horse.game.Game() + game.setup(screen) + + running = True + while game.isRunning(): + # tick with wait 1/25th of a second + milliseconds = clock.tick(25) # maximum number of frames per second + game.tick(milliseconds) + game.update(screen) + + # Event processing loop + # not sure i want the pausescreen behavior + #events = pausescreen.get_events() + events = pygame.event.get() + if events: + for event in events: + game.handleEvent(event) + +if __name__ == "__main__": + logging.basicConfig() + main() diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..b7135d2 --- /dev/null +++ b/setup.py @@ -0,0 +1,4 @@ +#!/usr/bin/env python +from sugar.activity import bundlebuilder +if __name__ == "__main__": + bundlebuilder.start("HorseGame") -- cgit v0.9.1