[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] overlay menu on mplayer



I want to know if this is even possible using pygame. I'm building an
in-car app and want to support touch screen's for users. Pretty much
this is just a frontend for mplayer. I'd like to show a menu over a move
then have it hide away. I can't seem to get it working. The childapp and
threading class's are fine. They are pretty much taken from freevo to
get it working for now till I can write my own. This is the test code
i'm trying to get to work. When I hit b the box pops up and I'd like
that box to go away when i hit v. This is just trying to move it. I
dunno if this is even possible in pygame.


import pygame
from pygame.locals import *

import commands

import popen2
import threading
import sys
import time
import os
import re

DEBUG = 1


class ChildApp:
	def __init__(self, app):
		# Start the child app through 'runapp' which will unblock signals and
		# sets the priority.

		start_str = app
        
		self.child = popen2.Popen3(start_str, 1, 100) 
		self.outfile = self.child.fromchild 
		self.errfile = self.child.childerr
		self.infile = self.child.tochild
        
		self.t1 = Read_Thread('stdout', self.outfile, self.stdout_cb)
		self.t1.setDaemon(1)
		self.t1.start()
        
		self.t2 = Read_Thread('stderr', self.errfile, self.stderr_cb)
		self.t2.setDaemon(1)
		self.t2.start()
		
		self.line = 0


	# Write a string to the app. 
	def write(self, line):
		try:
			self.infile.write(line)
			self.infile.flush()
		except IOError:
			print "I could not write to the app!"
        

	# Override this method to receive stdout from the child app
	# The function receives complete lines
	def stdout_cb(self, line):
		self.line = line

	def get_stdout_cb(self):
		return self.line


	# Override this method to receive stderr from the child app
	# The function receives complete lines
	def stderr_cb(self, line):
		pass


	def isAlive(self):
		return self.t1.isAlive() or self.t2.isAlive()

        
class Read_Thread(threading.Thread):

    def __init__(self, name, fp, callback):
        threading.Thread.__init__(self)
        self.name = name
        self.fp = fp
        self.callback = callback

        
    def run(self):
        try:
            self._handle_input()
        except IOError:
            pass
        except ValueError:
            pass


    def _handle_input(self):
        
        saved = ''
        while 1:

            # XXX There should be a C helper app that converts CR to LF.
            data = self.fp.readline(50)
            if not data:
                if DEBUG:
                    print '%s: No data, stopping (pid %s)!' %
(self.name, os.getpid())
                break
            else:
                data = data.replace('\r', '\n')
                lines = data.split('\n')

                # Only one partial line?
                if len(lines) == 1:
                    saved += data
                else:
                    # Combine saved data and first line, send to app
                    self.callback(saved + lines[0])
                    saved = ''

                    # There's one or more lines + possibly a partial
line
                    if lines[-1] != '':
                        # The last line is partial, save it for the next
time
                        saved = lines[-1]

                        # Send all lines except the last partial line to
the app
                        for line in lines[1:-1]:
                            self.callback(line)
                    else:
                        # Send all lines to the app
                        for line in lines[1:]:
                            self.callback(line)




def main():

	print "initing"
	pygame.init()
	screen = pygame.display.set_mode((640, 480))
	pygame.display.set_caption('Test App')


	# getting the window ID
	windId = commands.getoutput("xwininfo -tree -name \"Test App\" | grep
640x480 | awk {\' print $1 \'}")
	print windId
	
	
	cmd = '/usr/local/bin/mplayer -really-quiet -wid %s -slave
/dump/mp3s/11/1short.mpeg'  % (windId)

	app = ChildApp(cmd)        

	c = 0
	i = 0

	while 1:
		#c = (c+1) % 250 
		#clr = c, 50, c             
		#r = screen.fill(clr, (0, 0, 640, 20))
		#pygame.display.update(r)
		
		s = raw_input('Cmd>')
		if s.strip() == '':
			print 'Alive = %s' % app.isAlive()
		elif s.strip() == 'q':
			app.write("quit \n")
			sys.exit(0)
			print "quiting"
		elif s.strip() == 'm':
			app.write("mute \n")
			print 'Wrote to app'
		elif s.strip() == 'p':
			app.write("pause \n")
			print 'Wrote to app'             
		elif s.strip() == 's':
			app.write("seek 10 1 \n")
			print 'Wrote to app' 
		elif s.strip() == 't':
			app.write("get_time_length \n")
			print 'Wrote to app' 
		elif s.strip() == 'i':
			line = app.get_stdout_cb()
			print type(line)
			strip = re.compile("^A: *([0-9]+)").match
			line = strip(line)
			if hasattr(line,'group'):
				print line.group(1)
		elif s.strip() == 'v':
			image = pygame.Surface((660,30))
			image.fill((255, 0, 0))
			rect = image.get_rect()
			screen.blit(image, (0,0))
			pygame.display.update(rect)
		elif s.strip() == 'b':	
			image.set_colorkey((255, 255, 0))
			image = image.convert_alpha()
			rect[1] += 10
			#del(image)
			#screen.blit(image, (0,0))
			pygame.display.update(rect)
			
		#print "not a lopp"
		#c = (c+1) % 250
		#clr = c, 50, c
		#r = screen.fill(clr, (0, 460, 640, 20))
		#r = screen.fill(clr, (0, 0, 640, 20))
		#pygame.display.update(r)
		#pygame.time.wait(10)
		print "i need to flip"
		pygame.display.flip()