def __init__(self,show_id,root,canvas,show_params,track_params,pp_dir,pp_home,pp_profile): self.mon=Monitor() self.mon.on() self.root=root self.canvas=canvas self.show_id=show_id self.track_params=track_params self.show_params=show_params self.pp_dir=pp_dir self.pp_home=pp_home self.pp_profile=pp_profile # get config from medialist if there. if 'duration' in self.track_params and self.track_params['duration']<>"": self.duration= int(self.track_params['duration']) else: self.duration= int(self.show_params['duration']) # get background image from profile. self.background_file='' if self.track_params['background-image']<>"": self.background_file= self.track_params['background-image'] else: if self.track_params['display-show-background']=='yes': self.background_file= self.show_params['background-image'] # get background colour from profile. if self.track_params['background-colour']<>"": self.background_colour= self.track_params['background-colour'] else: self.background_colour= self.show_params['background-colour'] self.centre_x = int(self.canvas['width'])/2 self.centre_y = int(self.canvas['height'])/2 # keep tick as an integer sub-multiple of 1 second self.tick = 100 # tick time for image display (milliseconds) self.dwell = 1000*self.duration #get animation instructions from profile self.animate_begin_text=self.track_params['animate-begin'] self.animate_end_text=self.track_params['animate-end'] # open the plugin Manager self.pim=PluginManager(self.show_id,self.root,self.canvas,self.show_params,self.track_params,self.pp_dir,self.pp_home,self.pp_profile) #create an instance of PPIO so we can create gpio events self.ppio = PPIO()
def __init__(self, show_id, showlist, show_params, root, canvas, pp_dir, pp_profile, pp_home): self.show_id = show_id self.showlist = showlist self.show_params = show_params self.root = root self.show_canvas = canvas self.pp_dir = pp_dir self.pp_profile = pp_profile self.pp_home = pp_home if not 'plugin' in self.show_params: self.show_params['plugin'] = '' self.pim = PluginManager(self.show_id, self.root, self.show_canvas, self.show_params, None, self.pp_dir, self.pp_home, self.pp_profile) self.mon = Monitor()
def __init__(self, show_id, showlist, show_params, root, canvas, pp_dir, pp_profile, pp_home): self.show_id = show_id self.showlist = showlist self.show_params = show_params self.root = root self.show_canvas = canvas self.pp_dir = pp_dir self.pp_profile = pp_profile self.pp_home = pp_home if not "plugin" in self.show_params: self.show_params["plugin"] = "" self.pim = PluginManager( self.show_id, self.root, self.show_canvas, self.show_params, None, self.pp_dir, self.pp_home, self.pp_profile, ) self.mon = Monitor()
def __init__(self, show_id, showlist, root, canvas, show_params, track_params, pp_dir, pp_home, pp_profile, end_callback, command_callback): # create debugging log object self.mon = Monitor() self.mon.trace(self, '') # instantiate arguments self.show_id = show_id self.showlist = showlist self.root = root self.canvas = canvas['canvas-obj'] self.show_canvas_x1 = canvas['show-canvas-x1'] self.show_canvas_y1 = canvas['show-canvas-y1'] self.show_canvas_x2 = canvas['show-canvas-x2'] self.show_canvas_y2 = canvas['show-canvas-y2'] self.show_canvas_width = canvas['show-canvas-width'] self.show_canvas_height = canvas['show-canvas-height'] self.show_canvas_centre_x = canvas['show-canvas-centre-x'] self.show_canvas_centre_y = canvas['show-canvas-centre-y'] self.show_params = show_params self.track_params = track_params self.pp_dir = pp_dir self.pp_home = pp_home self.pp_profile = pp_profile self.end_callback = end_callback self.command_callback = command_callback # get background image from profile. self.background_file = '' if self.track_params['background-image'] != '': self.background_file = self.track_params['background-image'] # get background colour from profile. if self.track_params['background-colour'] != '': self.background_colour = self.track_params['background-colour'] else: self.background_colour = self.show_params['background-colour'] # get animation instructions from profile self.animate_begin_text = self.track_params['animate-begin'] self.animate_end_text = self.track_params['animate-end'] # create an instance of showmanager so we can control concurrent shows # self.show_manager=ShowManager(self.show_id,self.showlist,self.show_params,self.root,self.canvas,self.pp_dir,self.pp_profile,self.pp_home) # open the plugin Manager self.pim = PluginManager(self.show_id, self.root, self.canvas, self.show_params, self.track_params, self.pp_dir, self.pp_home, self.pp_profile) # create an instance of Animate so we can send animation commands self.animate = Animate() # initialise state and signals self.background_obj = None self.show_text_obj = None self.track_text_obj = None self.hint_obj = None self.background = None self.freeze_at_end_required = 'no' # overriden by videoplayer self.tick_timer = None self.terminate_signal = False self.play_state = ''
class Player(object): # common bits of __init__(...) def __init__(self, show_id, showlist, root, canvas, show_params, track_params, pp_dir, pp_home, pp_profile, end_callback, command_callback): # create debugging log object self.mon = Monitor() self.mon.trace(self, '') # instantiate arguments self.show_id = show_id self.showlist = showlist self.root = root self.canvas = canvas['canvas-obj'] self.show_canvas_x1 = canvas['show-canvas-x1'] self.show_canvas_y1 = canvas['show-canvas-y1'] self.show_canvas_x2 = canvas['show-canvas-x2'] self.show_canvas_y2 = canvas['show-canvas-y2'] self.show_canvas_width = canvas['show-canvas-width'] self.show_canvas_height = canvas['show-canvas-height'] self.show_canvas_centre_x = canvas['show-canvas-centre-x'] self.show_canvas_centre_y = canvas['show-canvas-centre-y'] self.show_params = show_params self.track_params = track_params self.pp_dir = pp_dir self.pp_home = pp_home self.pp_profile = pp_profile self.end_callback = end_callback self.command_callback = command_callback # get background image from profile. self.background_file = '' if self.track_params['background-image'] != '': self.background_file = self.track_params['background-image'] # get background colour from profile. if self.track_params['background-colour'] != '': self.background_colour = self.track_params['background-colour'] else: self.background_colour = self.show_params['background-colour'] # get animation instructions from profile self.animate_begin_text = self.track_params['animate-begin'] self.animate_end_text = self.track_params['animate-end'] # create an instance of showmanager so we can control concurrent shows # self.show_manager=ShowManager(self.show_id,self.showlist,self.show_params,self.root,self.canvas,self.pp_dir,self.pp_profile,self.pp_home) # open the plugin Manager self.pim = PluginManager(self.show_id, self.root, self.canvas, self.show_params, self.track_params, self.pp_dir, self.pp_home, self.pp_profile) # create an instance of Animate so we can send animation commands self.animate = Animate() # initialise state and signals self.background_obj = None self.show_text_obj = None self.track_text_obj = None self.hint_obj = None self.background = None self.freeze_at_end_required = 'no' # overriden by videoplayer self.tick_timer = None self.terminate_signal = False self.play_state = '' def pre_load(self): # Control other shows at beginning self.show_control(self.track_params['show-control-begin']) pass # common bits of show(....) def pre_show(self): self.mon.trace(self, '') # show_x_content moved to just before ready_callback to improve flicker. self.show_x_content() # and whatecer the plugin has created self.pim.show_plugin() #ready callback hides and closes players from previous track, also displays show background if self.ready_callback is not None: self.ready_callback(self.enable_show_background) # create animation events reason, message = self.animate.animate(self.animate_begin_text, id(self)) if reason == 'error': self.mon.err(self, message) self.play_state = 'show-failed' if self.finished_callback is not None: self.finished_callback('error', message) else: # return to start playing the track. self.mon.log( self, ">show track received from show Id: " + str(self.show_id)) return # to keep landscape happy def ready_callback(self, enable_show_background): self.mon.fatal(self, 'ready callback not overridden') self.end('error', 'ready callback not overridden') def finished_callback(self, reason, message): self.mon.fatal(self, 'finished callback not overridden') self.end('error', 'finished callback not overridden') def closed_callback(self, reason, message): self.mon.fatal(self, 'closed callback not overridden') self.end('error', 'closed callback not overridden') # Control shows so pass the show control commands back to PiPresents via the command callback def show_control(self, show_control_text): lines = show_control_text.split('\n') for line in lines: if line.strip() == "": continue # print 'show control command: ',line self.command_callback(line, source='track', show=self.show_params['show-ref']) # ***************** # hide content and end animation, show control etc. # called by ready calback and end # ***************** def hide(self): self.mon.trace(self, '') # abort the timer if self.tick_timer is not None: self.canvas.after_cancel(self.tick_timer) self.tick_timer = None self.hide_x_content() # stop the plugin if self.track_params['plugin'] != '': self.pim.stop_plugin() # Control concurrent shows at end self.show_control(self.track_params['show-control-end']) # clear events list for this track if self.track_params['animate-clear'] == 'yes': self.animate.clear_events_list(id(self)) # create animation events for ending reason, message = self.animate.animate(self.animate_end_text, id(self)) if reason == 'error': self.play_state = 'show-failed' if self.finished_callback is not None: self.finished_callback('error', message) else: return def terminate(self): self.mon.trace(self, '') self.terminate_signal = True if self.play_state == 'showing': # call the derived class's stop method self.stop() else: self.end('killed', 'terminate with no track or show open') # must be overriden by derived class def stop(self): self.mon.fatal(self, 'stop not overidden by derived class') self.play_state = 'show-failed' if self.finished_callback is not None: self.finished_callback('error', 'stop not overidden by derived class') def get_play_state(self): return self.play_state # ***************** # ending the player # ***************** def end(self, reason, message): self.mon.trace(self, '') # stop the plugin if self.terminate_signal is True: reason = 'killed' self.terminate_signal = False self.hide() self.end_callback(reason, message) self = None # ***************** # displaying common things # ***************** def load_plugin(self): # load the plugin if required if self.track_params['plugin'] != '': reason, message, self.track = self.pim.load_plugin( self.track, self.track_params['plugin']) return reason, message def draw_plugin(self): # load the plugin if required if self.track_params['plugin'] != '': self.pim.draw_plugin() return def load_x_content(self, enable_menu): self.mon.trace(self, '') self.background_obj = None self.background = None self.track_text_obj = None self.show_text_obj = None self.hint_obj = None self.track_obj = None # background image if self.background_file != '': background_img_file = self.complete_path(self.background_file) if not os.path.exists(background_img_file): return 'error', "Track background file not found " + background_img_file else: pil_background_img = Image.open(background_img_file) # print 'pil_background_img ',pil_background_img image_width, image_height = pil_background_img.size window_width = self.show_canvas_width window_height = self.show_canvas_height if image_width != window_width or image_height != window_height: pil_background_img = pil_background_img.resize( (window_width, window_height)) self.background = ImageTk.PhotoImage(pil_background_img) del pil_background_img self.background_obj = self.canvas.create_image( self.show_canvas_x1, self.show_canvas_y1, image=self.background, anchor=NW) # print '\nloaded background_obj: ',self.background_obj # load the track content. Dummy function below is overridden in players status, message = self.load_track_content() if status == 'error': return 'error', message # load show text if enabled if self.show_params['show-text'] != '' and self.track_params[ 'display-show-text'] == 'yes': x, y, anchor, justify = calculate_text_position( self.show_params['show-text-x'], self.show_params['show-text-y'], self.show_canvas_x1, self.show_canvas_y1, self.show_canvas_centre_x, self.show_canvas_centre_y, self.show_canvas_x2, self.show_canvas_y2, self.show_params['show-text-justify']) self.show_text_obj = self.canvas.create_text( x, y, anchor=anchor, justify=justify, text=self.show_params['show-text'], fill=self.show_params['show-text-colour'], font=self.show_params['show-text-font']) # load track text if enabled if self.track_params['track-text'] != '': x, y, anchor, justify = calculate_text_position( self.track_params['track-text-x'], self.track_params['track-text-y'], self.show_canvas_x1, self.show_canvas_y1, self.show_canvas_centre_x, self.show_canvas_centre_y, self.show_canvas_x2, self.show_canvas_y2, self.track_params['track-text-justify']) self.track_text_obj = self.canvas.create_text( x, y, anchor=anchor, justify=justify, text=self.track_params['track-text'], fill=self.track_params['track-text-colour'], font=self.track_params['track-text-font']) # load instructions if enabled if enable_menu is True: x, y, anchor, justify = calculate_text_position( self.show_params['hint-x'], self.show_params['hint-y'], self.show_canvas_x1, self.show_canvas_y1, self.show_canvas_centre_x, self.show_canvas_centre_y, self.show_canvas_x2, self.show_canvas_y2, self.show_params['hint-justify']) self.hint_obj = self.canvas.create_text( x, y, justify=justify, text=self.show_params['hint-text'], fill=self.show_params['hint-colour'], font=self.show_params['hint-font'], anchor=anchor) self.display_show_canvas_rectangle() self.pim.draw_plugin() self.canvas.tag_raise('pp-click-area') self.canvas.itemconfig(self.background_obj, state='hidden') self.canvas.itemconfig(self.show_text_obj, state='hidden') self.canvas.itemconfig(self.track_text_obj, state='hidden') self.canvas.itemconfig(self.hint_obj, state='hidden') self.canvas.update_idletasks() return 'normal', 'x-content loaded' # display the rectangle that is the show canvas def display_show_canvas_rectangle(self): # coords=[self.show_canvas_x1,self.show_canvas_y1,self.show_canvas_x2-1,self.show_canvas_y2-1] # self.canvas.create_rectangle(coords, # outline='yellow', # fill='') pass # dummy functions to manipulate the track content, overidden in some players, # message text in messageplayer # image in imageplayer # menu stuff in menuplayer def load_track_content(self): return 'normal', 'player has no track content to load' def show_track_content(self): pass def hide_track_content(self): pass def show_x_content(self): self.mon.trace(self, '') # background colour if self.background_colour != '': self.canvas.config(bg=self.background_colour) # print 'showing background_obj: ', self.background_obj # reveal background image and text self.canvas.itemconfig(self.background_obj, state='normal') self.show_track_content() self.canvas.itemconfig(self.show_text_obj, state='normal') self.canvas.itemconfig(self.track_text_obj, state='normal') self.canvas.itemconfig(self.hint_obj, state='normal') # self.canvas.update_idletasks( ) # decide whether the show background should be enabled. # print 'DISPLAY SHOW BG',self.track_params['display-show-background'],self.background_obj if self.background_obj is None and self.track_params[ 'display-show-background'] == 'yes': self.enable_show_background = True else: self.enable_show_background = False # print 'ENABLE SB',self.enable_show_background def hide_x_content(self): self.mon.trace(self, '') self.hide_track_content() self.canvas.itemconfig(self.background_obj, state='hidden') self.canvas.itemconfig(self.show_text_obj, state='hidden') self.canvas.itemconfig(self.track_text_obj, state='hidden') self.canvas.itemconfig(self.hint_obj, state='hidden') # self.canvas.update_idletasks( ) self.canvas.delete(self.background_obj) self.canvas.delete(self.show_text_obj) self.canvas.delete(self.track_text_obj) self.canvas.delete(self.hint_obj) self.background = None # self.canvas.update_idletasks( ) # **************** # utilities # ***************** def get_links(self): return self.track_params['links'] # produce an absolute path from the relative one in track paramters def complete_path(self, track_file): # complete path of the filename of the selected entry if track_file[0] == "+": track_file = self.pp_home + track_file[1:] elif track_file[0] == "@": track_file = self.pp_profile + track_file[1:] return track_file # get a text string from resources.cfg def resource(self, section, item): value = self.rr.get(section, item) return value # False if not found
def __init__(self, show_id, root, canvas, show_params, track_params, pp_dir, pp_home, pp_profile): self.mon = Monitor() self.mon.off() # instantiate arguments self.show_id = show_id self.root = root self.canvas = canvas self.show_params = show_params self.track_params = track_params self.pp_dir = pp_dir self.pp_home = pp_home self.pp_profile = pp_profile # get duration limit (secs ) from profile if self.track_params["duration"] <> "": self.duration = int(self.track_params["duration"]) self.duration_limit = 20 * self.duration else: self.duration_limit = -1 # get background image from profile. self.background_file = "" if self.track_params["background-image"] <> "": self.background_file = self.track_params["background-image"] else: if self.track_params["display-show-background"] == "yes": self.background_file = self.show_params["background-image"] # get background colour from profile. if self.track_params["background-colour"] <> "": self.background_colour = self.track_params["background-colour"] else: self.background_colour = self.show_params["background-colour"] # get audio device from profile. if self.track_params["mplayer-audio"] <> "": self.mplayer_audio = self.track_params["mplayer-audio"] else: self.mplayer_audio = self.show_params["mplayer-audio"] # get audio volume from profile. if self.track_params["mplayer-volume"] <> "": self.mplayer_volume = self.track_params["mplayer-volume"].strip() else: self.mplayer_volume = self.show_params["mplayer-volume"].strip() self.volume_option = "volume=" + self.mplayer_volume # get speaker from profile if self.track_params["audio-speaker"] <> "": self.audio_speaker = self.track_params["audio-speaker"] else: self.audio_speaker = self.show_params["audio-speaker"] if self.audio_speaker == "left": self.speaker_option = AudioPlayer._LEFT elif self.audio_speaker == "right": self.speaker_option = AudioPlayer._RIGHT else: self.speaker_option = AudioPlayer._STEREO # get animation instructions from profile self.animate_begin_text = self.track_params["animate-begin"] self.animate_end_text = self.track_params["animate-end"] # open the plugin Manager self.pim = PluginManager( self.show_id, self.root, self.canvas, self.show_params, self.track_params, self.pp_dir, self.pp_home, self.pp_profile, ) # create an instance of PPIO so we can create gpio events self.ppio = PPIO() # could put instance generation in play, not sure which is better. self.mplayer = mplayerDriver(self.canvas) self.tick_timer = None self.init_play_state_machine()
def __init__(self,show_id,root,canvas,show_params,track_params,pp_dir,pp_home,pp_profile): """ show_id - show instance that player is run from (for monitoring only) canvas - the canvas onto which the image is to be drawn show_params - dictionary of show parameters track_params - disctionary of track paramters pp_home - data home directory pp_profile - profile name """ self.mon=Monitor() self.mon.off() self.show_id=show_id self.root=root self.canvas=canvas self.show_params=show_params self.track_params=track_params self.pp_dir=pp_dir self.pp_home=pp_home self.pp_profile=pp_profile # open resources self.rr=ResourceReader() # get parameters self.animate_begin_text=self.track_params['animate-begin'] self.animate_end_text=self.track_params['animate-end'] if self.track_params['duration']<>"": self.duration= int(self.track_params['duration']) else: self.duration= int(self.show_params['duration']) #create an instance of PPIO so we can create gpio events self.ppio = PPIO() # get background image from profile. self.background_file='' if self.track_params['background-image']<>'': self.background_file= self.track_params['background-image'] else: if self.track_params['display-show-background']=='yes': self.background_file= self.show_params['background-image'] # get background colour from profile. if self.track_params['background-colour']<>"": self.background_colour= self.track_params['background-colour'] else: self.background_colour= self.show_params['background-colour'] # get image window from profile if self.track_params['image-window'].strip()<>"": self.image_window= self.track_params['image-window'].strip() else: self.image_window= self.show_params['image-window'].strip() # open the plugin Manager self.pim=PluginManager(self.show_id,self.root,self.canvas,self.show_params,self.track_params,self.pp_dir,self.pp_home,self.pp_profile)
class Player(object): # common bits of __init__(...) def __init__( self, show_id, showlist, root, canvas, show_params, track_params, pp_dir, pp_home, pp_profile, end_callback, command_callback, ): # create debugging log object self.mon = Monitor() self.mon.trace(self, "") # instantiate arguments self.show_id = show_id self.showlist = showlist self.root = root self.canvas = canvas["canvas-obj"] self.show_canvas_x1 = canvas["show-canvas-x1"] self.show_canvas_y1 = canvas["show-canvas-y1"] self.show_canvas_x2 = canvas["show-canvas-x2"] self.show_canvas_y2 = canvas["show-canvas-y2"] self.show_canvas_width = canvas["show-canvas-width"] self.show_canvas_height = canvas["show-canvas-height"] self.show_canvas_centre_x = canvas["show-canvas-centre-x"] self.show_canvas_centre_y = canvas["show-canvas-centre-y"] self.show_params = show_params self.track_params = track_params self.pp_dir = pp_dir self.pp_home = pp_home self.pp_profile = pp_profile self.end_callback = end_callback self.command_callback = command_callback # get background image from profile. self.background_file = "" if self.track_params["background-image"] != "": self.background_file = self.track_params["background-image"] # get background colour from profile. if self.track_params["background-colour"] != "": self.background_colour = self.track_params["background-colour"] else: self.background_colour = self.show_params["background-colour"] # get animation instructions from profile self.animate_begin_text = self.track_params["animate-begin"] self.animate_end_text = self.track_params["animate-end"] # create an instance of showmanager so we can control concurrent shows # self.show_manager=ShowManager(self.show_id,self.showlist,self.show_params,self.root,self.canvas,self.pp_dir,self.pp_profile,self.pp_home) # open the plugin Manager self.pim = PluginManager( self.show_id, self.root, self.canvas, self.show_params, self.track_params, self.pp_dir, self.pp_home, self.pp_profile, ) # create an instance of Animate so we can send animation commands self.animate = Animate() # initialise state and signals self.background_obj = None self.show_text_obj = None self.track_text_obj = None self.hint_obj = None self.background = None self.freeze_at_end_required = "no" # overriden by videoplayer self.tick_timer = None self.terminate_signal = False self.play_state = "" def pre_load(self): # Control other shows at beginning self.show_control(self.track_params["show-control-begin"]) pass # common bits of show(....) def pre_show(self): self.mon.trace(self, "") # show_x_content moved to just before ready_callback to improve flicker. self.show_x_content() # and whatecer the plugin has created self.pim.show_plugin() # ready callback hides and closes players from previous track, also displays show background if self.ready_callback is not None: self.ready_callback(self.enable_show_background) # create animation events reason, message = self.animate.animate(self.animate_begin_text, id(self)) if reason == "error": self.mon.err(self, message) self.play_state = "show-failed" if self.finished_callback is not None: self.finished_callback("error", message) else: # return to start playing the track. self.mon.log(self, ">show track received from show Id: " + str(self.show_id)) return # to keep landscape happy def ready_callback(self, enable_show_background): self.mon.fatal(self, "ready callback not overridden") self.end("error", "ready callback not overridden") def finished_callback(self, reason, message): self.mon.fatal(self, "finished callback not overridden") self.end("error", "finished callback not overridden") def closed_callback(self, reason, message): self.mon.fatal(self, "closed callback not overridden") self.end("error", "closed callback not overridden") # Control shows so pass the show control commands back to PiPresents via the command callback def show_control(self, show_control_text): lines = show_control_text.split("\n") for line in lines: if line.strip() == "": continue # print 'show control command: ',line self.command_callback(line, self.show_params["show-ref"]) # ***************** # hide content and end animation, show control etc. # called by ready calback and end # ***************** def hide(self): self.mon.trace(self, "") # abort the timer if self.tick_timer is not None: self.canvas.after_cancel(self.tick_timer) self.tick_timer = None self.hide_x_content() # stop the plugin if self.track_params["plugin"] != "": self.pim.stop_plugin() # Control concurrent shows at end self.show_control(self.track_params["show-control-end"]) # clear events list for this track if self.track_params["animate-clear"] == "yes": self.animate.clear_events_list(id(self)) # create animation events for ending reason, message = self.animate.animate(self.animate_end_text, id(self)) if reason == "error": self.play_state = "show-failed" if self.finished_callback is not None: self.finished_callback("error", message) else: return def terminate(self): self.mon.trace(self, "") self.terminate_signal = True if self.play_state == "showing": # call the derived class's stop method self.stop() else: self.end("killed", "terminate with no track or show open") # must be overriden by derived class def stop(self): self.mon.fatal(self, "stop not overidden by derived class") self.play_state = "show-failed" if self.finished_callback is not None: self.finished_callback("error", "stop not overidden by derived class") def get_play_state(self): return self.play_state # ***************** # ending the player # ***************** def end(self, reason, message): self.mon.trace(self, "") # stop the plugin if self.terminate_signal is True: reason = "killed" self.terminate_signal = False self.hide() self.end_callback(reason, message) self = None # ***************** # displaying common things # ***************** def load_plugin(self): # load the plugin if required if self.track_params["plugin"] != "": reason, message, self.track = self.pim.load_plugin(self.track, self.track_params["plugin"]) return reason, message def draw_plugin(self): # load the plugin if required if self.track_params["plugin"] != "": self.pim.draw_plugin() return def load_x_content(self, enable_menu): self.mon.trace(self, "") self.background_obj = None self.background = None self.track_text_obj = None self.show_text_obj = None self.hint_obj = None self.track_obj = None # background image if self.background_file != "": background_img_file = self.complete_path(self.background_file) if not os.path.exists(background_img_file): return "error", "Track background file not found " + background_img_file else: pil_background_img = Image.open(background_img_file) # print 'pil_background_img ',pil_background_img image_width, image_height = pil_background_img.size window_width = self.show_canvas_width window_height = self.show_canvas_height if image_width != window_width or image_height != window_height: pil_background_img = pil_background_img.resize((window_width, window_height)) self.background = ImageTk.PhotoImage(pil_background_img) del pil_background_img self.background_obj = self.canvas.create_image( self.show_canvas_x1, self.show_canvas_y1, image=self.background, anchor=NW ) # print '\nloaded background_obj: ',self.background_obj # load the track content. Dummy function below is overridden in players status, message = self.load_track_content() if status == "error": return "error", message # load show text if enabled if self.show_params["show-text"] != "" and self.track_params["display-show-text"] == "yes": self.show_text_obj = self.canvas.create_text( int(self.show_params["show-text-x"]) + self.show_canvas_x1, int(self.show_params["show-text-y"]) + self.show_canvas_y1, anchor=NW, text=self.show_params["show-text"], fill=self.show_params["show-text-colour"], font=self.show_params["show-text-font"], ) # load track text if enabled if self.track_params["track-text"] != "": self.track_text_obj = self.canvas.create_text( int(self.track_params["track-text-x"]) + self.show_canvas_x1, int(self.track_params["track-text-y"]) + self.show_canvas_y1, anchor=NW, text=self.track_params["track-text"], fill=self.track_params["track-text-colour"], font=self.track_params["track-text-font"], ) # load instructions if enabled if enable_menu is True: self.hint_obj = self.canvas.create_text( int(self.show_params["hint-x"]) + self.show_canvas_x1, int(self.show_params["hint-y"]) + self.show_canvas_y1, text=self.show_params["hint-text"], fill=self.show_params["hint-colour"], font=self.show_params["hint-font"], anchor=NW, ) self.display_show_canvas_rectangle() self.pim.draw_plugin() self.canvas.tag_raise("pp-click-area") self.canvas.itemconfig(self.background_obj, state="hidden") self.canvas.itemconfig(self.show_text_obj, state="hidden") self.canvas.itemconfig(self.track_text_obj, state="hidden") self.canvas.itemconfig(self.hint_obj, state="hidden") self.canvas.update_idletasks() return "normal", "x-content loaded" # display the rectangle that is the show canvas def display_show_canvas_rectangle(self): # coords=[self.show_canvas_x1,self.show_canvas_y1,self.show_canvas_x2-1,self.show_canvas_y2-1] # self.canvas.create_rectangle(coords, # outline='yellow', # fill='') pass # dummy functions to manipulate the track content, overidden in some players, # message text in messageplayer # image in imageplayer # menu stuff in menuplayer def load_track_content(self): return "normal", "player has no track content to load" def show_track_content(self): pass def hide_track_content(self): pass def show_x_content(self): self.mon.trace(self, "") # background colour if self.background_colour != "": self.canvas.config(bg=self.background_colour) # print 'showing background_obj: ', self.background_obj # reveal background image and text self.canvas.itemconfig(self.background_obj, state="normal") self.show_track_content() self.canvas.itemconfig(self.show_text_obj, state="normal") self.canvas.itemconfig(self.track_text_obj, state="normal") self.canvas.itemconfig(self.hint_obj, state="normal") # self.canvas.update_idletasks( ) # decide whether the show background should be enabled. # print 'DISPLAY SHOW BG',self.track_params['display-show-background'],self.background_obj if self.background_obj is None and self.track_params["display-show-background"] == "yes": self.enable_show_background = True else: self.enable_show_background = False # print 'ENABLE SB',self.enable_show_background def hide_x_content(self): self.mon.trace(self, "") self.hide_track_content() self.canvas.itemconfig(self.background_obj, state="hidden") self.canvas.itemconfig(self.show_text_obj, state="hidden") self.canvas.itemconfig(self.track_text_obj, state="hidden") self.canvas.itemconfig(self.hint_obj, state="hidden") # self.canvas.update_idletasks( ) self.canvas.delete(self.background_obj) self.canvas.delete(self.show_text_obj) self.canvas.delete(self.track_text_obj) self.canvas.delete(self.hint_obj) self.background = None # self.canvas.update_idletasks( ) # **************** # utilities # ***************** def get_links(self): return self.track_params["links"] # produce an absolute path from the relative one in track paramters def complete_path(self, track_file): # complete path of the filename of the selected entry if track_file[0] == "+": track_file = self.pp_home + track_file[1:] # self.mon.log(self,"Background image is "+ track_file) return track_file # get a text string from resources.cfg def resource(self, section, item): value = self.rr.get(section, item) return value # False if not found
class VideoPlayer: """ plays a track using omxplayer See pp_imageplayer for common software design description """ _CLOSED = "omx_closed" #probably will not exist _STARTING = "omx_starting" #track is being prepared _PLAYING = "omx_playing" #track is playing to the screen, may be paused _ENDING = "omx_ending" #track is in the process of ending due to quit or end of track # *************************************** # EXTERNAL COMMANDS # *************************************** def __init__(self, show_id, root, canvas, show_params, track_params , pp_dir, pp_home, pp_profile): self.mon=Monitor() self.mon.on() #instantiate arguments self.show_id=show_id self.root=root self.canvas = canvas self.show_params=show_params self.track_params=track_params self.pp_dir=pp_dir self.pp_home=pp_home self.pp_profile=pp_profile # get config from medialist if there. if self.track_params['omx-audio']<>"": self.omx_audio= self.track_params['omx-audio'] else: self.omx_audio= self.show_params['omx-audio'] if self.omx_audio<>"": self.omx_audio= "-o "+ self.omx_audio if self.track_params['omx-volume']<>"": self.omx_volume= self.track_params['omx-volume'] else: self.omx_volume= self.show_params['omx-volume'] if self.omx_volume<>"": self.omx_volume= "--vol "+ str(int(self.omx_volume)*100) + ' ' if self.track_params['omx-window']<>'': self.omx_window= self.track_params['omx-window'] else: self.omx_window= self.show_params['omx-window'] # get background image from profile. self.background_file='' if self.track_params['background-image']<>"": self.background_file= self.track_params['background-image'] else: if self.track_params['display-show-background']=='yes': self.background_file= self.show_params['background-image'] # get background colour from profile. if self.track_params['background-colour']<>"": self.background_colour= self.track_params['background-colour'] else: self.background_colour= self.show_params['background-colour'] self.centre_x = int(self.canvas['width'])/2 self.centre_y = int(self.canvas['height'])/2 #get animation instructions from profile self.animate_begin_text=self.track_params['animate-begin'] self.animate_end_text=self.track_params['animate-end'] # open the plugin Manager self.pim=PluginManager(self.show_id,self.root,self.canvas,self.show_params,self.track_params,self.pp_dir,self.pp_home,self.pp_profile) #create an instance of PPIO so we can create gpio events self.ppio = PPIO() # could put instance generation in play, not sure which is better. self.omx=OMXDriver(self.canvas) self.tick_timer=None self.init_play_state_machine() def play(self, track, showlist, end_callback, ready_callback, enable_menu=False): #instantiate arguments self.track=track self.showlist=showlist self.ready_callback=ready_callback #callback when ready to play self.end_callback=end_callback # callback when finished self.enable_menu = enable_menu # callback to the calling object to e.g remove egg timer and enable click areas. if self.ready_callback<>None: self.ready_callback() # create an instance of showmanager so we can control concurrent shows self.show_manager=ShowManager(self.show_id,self.showlist,self.show_params,self.root,self.canvas,self.pp_dir,self.pp_profile,self.pp_home) #set up video window reason,message,comand,has_window,x1,y1,x2,y2= self.parse_window(self.omx_window) if reason =='error': self.mon.err(self,'omx window error: ' + message + ' in ' + self.omx_window) self.end_callback(reason,message) else: if has_window==True: self.omx_window= '--win " '+ str(x1) + ' ' + str(y1) + ' ' + str(x2) + ' ' + str(y2) + ' " ' else: self.omx_window='' # Control other shows at beginning reason,message=self.show_manager.show_control(self.track_params['show-control-begin']) if reason in ('error','killed'): self.end_callback(reason,message) self=None else: #display content reason,message=self.display_content() if reason == 'error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: # create animation events reason,message=self.ppio.animate(self.animate_begin_text,id(self)) if reason=='error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: # start playing the video. if self.play_state == VideoPlayer._CLOSED: self.mon.log(self,">play track received") self.start_play_state_machine(self.track) else: self.mon.err(self,'play track rejected') self.end_callback('error','play track rejected') self=None def terminate(self,reason): # circumvents state machine and does not wait for omxplayer to close if self.omx<>None: self.mon.log(self,"sent terminate to omxdriver") self.omx.terminate(reason) self.end('killed',' end without waiting for omxplayer to finish') # end without waiting else: self.mon.log(self,"terminate, omxdriver not running") self.end('killed','terminate, mplayerdriver not running') def input_pressed(self,symbol): if symbol[0:4]=='omx-': self.control(symbol[4]) elif symbol =='pause': self.pause() elif symbol=='stop': self.stop() else: pass def get_links(self): return self.track_params['links'] # *************************************** # INTERNAL FUNCTIONS # *************************************** # respond to normal stop def stop(self): # send signal to stop the track to the state machine self.mon.log(self,">stop received") self.quit_signal=True #toggle pause def pause(self): if self.play_state in (VideoPlayer._PLAYING,VideoPlayer._ENDING): self.omx.pause() return True else: self.mon.log(self,"!<pause rejected") return False # other control when playing def control(self,char): if self.play_state==VideoPlayer._PLAYING and char not in ('q'): self.mon.log(self,"> send control to omx: "+ char) self.omx.control(char) return True else: self.mon.log(self,"!<control rejected") return False # *********************** # sequencing # ********************** """self. play_state controls the playing sequence, it has the following values. I am not entirely sure the starting and ending states are required. - _closed - the omx process is not running, omx process can be initiated - _starting - omx process is running but is not yet able to receive controls - _playing - playing a track, controls can be sent - _ending - omx is doing its termination, controls cannot be sent """ def init_play_state_machine(self): self.quit_signal=False self.play_state=VideoPlayer._CLOSED def start_play_state_machine(self,track): #initialise all the state machine variables #self.iteration = 0 # for debugging self.quit_signal=False # signal that user has pressed stop self.play_state=VideoPlayer._STARTING #play the selected track options=self.omx_audio+ " " + self.omx_volume + ' ' + self.omx_window + ' ' + self.show_params['omx-other-options']+" " self.omx.play(track,options) self.mon.log (self,'Playing track from show Id: '+ str(self.show_id)) # and start polling for state changes self.tick_timer=self.canvas.after(50, self.play_state_machine) def play_state_machine(self): if self.play_state == VideoPlayer._CLOSED: self.mon.log(self," State machine: " + self.play_state) return elif self.play_state == VideoPlayer._STARTING: self.mon.log(self," State machine: " + self.play_state) # if omxplayer is playing the track change to play state if self.omx.start_play_signal==True: self.mon.log(self," <start play signal received from omx") self.omx.start_play_signal=False self.play_state=VideoPlayer._PLAYING self.mon.log(self," State machine: omx_playing started") self.tick_timer=self.canvas.after(50, self.play_state_machine) elif self.play_state == VideoPlayer._PLAYING: # self.mon.log(self," State machine: " + self.play_state) # service any queued stop signals if self.quit_signal==True: self.mon.log(self," Service stop required signal") self.stop_omx() self.quit_signal=False # self.play_state = VideoPlayer._ENDING # omxplayer reports it is terminating so change to ending state if self.omx.end_play_signal: self.mon.log(self," <end play signal received") self.mon.log(self," <end detected at: " + str(self.omx.video_position)) if self.omx.end_play_reason<>'nice_day': # deal with omxplayer not sending 'have a nice day' self.mon.warn(self," <end detected at: " + str(self.omx.video_position)) self.mon.warn(self," <pexpect reports: "+self.omx.end_play_reason) self.mon.warn(self,'pexpect.before is'+self.omx.xbefore) self.play_state = VideoPlayer._ENDING self.ending_count=0 self.tick_timer=self.canvas.after(200, self.play_state_machine) elif self.play_state == VideoPlayer._ENDING: self.mon.log(self," State machine: " + self.play_state) # if spawned process has closed can change to closed state self.mon.log (self," State machine : is omx process running? - " + str(self.omx.is_running())) if self.omx.is_running() ==False: self.mon.log(self," <omx process is dead") self.play_state = VideoPlayer._CLOSED self.end('normal','quit by user or system') else: self.ending_count+=1 if self.ending_count>10: # deal with omxplayer not terminating at the end of a track self.mon.warn(self," <omxplayer failed to close at: " + str(self.omx.video_position)) self.mon.warn(self,'pexpect.before is'+self.omx.xbefore) self.omx.kill() self.mon.warn(self,'omxplayer now terminated ') self.play_state = VideoPlayer._CLOSED self.end('normal','end from omxplayer failed to terminate') else: self.tick_timer=self.canvas.after(200, self.play_state_machine) def stop_omx(self): # send signal to stop the track to the state machine self.mon.log(self," >stop omx received from state machine") if self.play_state==VideoPlayer._PLAYING: self.omx.stop() return True else: self.mon.log(self,"!<stop rejected") return False # ***************** # ending the player # ***************** def end(self,reason,message): # stop the plugin if self.track_params['plugin']<>'': self.pim.stop_plugin() # os.system("xrefresh -display :0") # abort the timer if self.tick_timer<>None: self.canvas.after_cancel(self.tick_timer) self.tick_timer=None if reason in ('error','killed'): self.end_callback(reason,message) self=None else: # normal end so do show control and animation # Control concurrent shows at end reason,message=self.show_manager.show_control(self.track_params['show-control-end']) if reason =='error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: # clear events list for this track if self.track_params['animate-clear']=='yes': self.ppio.clear_events_list(id(self)) # create animation events for ending reason,message=self.ppio.animate(self.animate_begin_text,id(self)) if reason=='error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: self.end_callback('normal',"track has terminated or quit") self=None # ***************** # displaying things # ***************** def display_content(self): #background colour if self.background_colour<>'': self.canvas.config(bg=self.background_colour) # delete previous content self.canvas.delete('pp-content') # background image if self.background_file<>'': self.background_img_file = self.complete_path(self.background_file) if not os.path.exists(self.background_img_file): self.mon.err(self,"Video background file not found: "+ self.background_img_file) self.end('error',"Video background file not found") else: pil_background_img=PIL.Image.open(self.background_img_file) self.background = PIL.ImageTk.PhotoImage(pil_background_img) self.drawn = self.canvas.create_image(int(self.canvas['width'])/2, int(self.canvas['height'])/2, image=self.background, anchor=CENTER, tag='pp-content') # execute the plugin if required if self.track_params['plugin']<>'': reason,message,self.track = self.pim.do_plugin(self.track,self.track_params['plugin'],) if reason <> 'normal': return reason,message # display show text if enabled if self.show_params['show-text']<> '' and self.track_params['display-show-text']=='yes': self.canvas.create_text(int(self.show_params['show-text-x']),int(self.show_params['show-text-y']), anchor=NW, text=self.show_params['show-text'], fill=self.show_params['show-text-colour'], font=self.show_params['show-text-font'], tag='pp-content') # display track text if enabled if self.track_params['track-text']<> '': self.canvas.create_text(int(self.track_params['track-text-x']),int(self.track_params['track-text-y']), anchor=NW, text=self.track_params['track-text'], fill=self.track_params['track-text-colour'], font=self.track_params['track-text-font'], tag='pp-content') # display instructions if enabled if self.enable_menu== True: self.canvas.create_text(int(self.show_params['hint-x']), int(self.show_params['hint-y']), text=self.show_params['hint-text'], fill=self.show_params['hint-colour'], font=self.show_params['hint-font'], anchor=NW, tag='pp-content') self.canvas.tag_raise('pp-click-area') self.canvas.update_idletasks( ) return 'normal','' # **************** # utilities # ***************** def complete_path(self,track_file): # complete path of the filename of the selected entry if track_file[0]=="+": track_file=self.pp_home+track_file[1:] self.mon.log(self,"Background image is "+ track_file) return track_file # original _ # warp _ or xy2 def parse_window(self,line): fields = line.split() # check there is a command field if len(fields) < 1: return 'error','no type field','',False,0,0,0,0 # deal with original which has 1 if fields[0]=='original': if len(fields) <> 1: return 'error','number of fields for original','',False,0,0,0,0 return 'normal','',fields[0],False,0,0,0,0 #deal with warp which has 1 or 5 arguments # check basic syntax if fields[0] <>'warp': return 'error','not a valid type','',False,0,0,0,0 if len(fields) not in (1,5): return 'error','wrong number of coordinates for warp','',False,0,0,0,0 # deal with window coordinates if len(fields) == 5: #window is specified if not (fields[1].isdigit() and fields[2].isdigit() and fields[3].isdigit() and fields[4].isdigit()): return 'error','coordinates are not positive integers','',False,0,0,0,0 has_window=True return 'normal','',fields[0],has_window,int(fields[1]),int(fields[2]),int(fields[3]),int(fields[4]) else: # fullscreen has_window=True return 'normal','',fields[0],has_window,0,0,self.canvas['width'],self.canvas['height']
class BrowserPlayer: #state constants _CLOSED = "player_closed" #probably will not exist _STARTING = "player_starting" #uzbl beinf loaded and fifo created _WAITING = "wait for timeout" # waiting for browser to appear on the screen _PLAYING = "player_playing" #track is playing to the screen _ENDING = "player_ending" #track is in the process of ending due to quit or duration exceeded # *************************************** # EXTERNAL COMMANDS # *************************************** def __init__(self, show_id, root, canvas, show_params, track_params, pp_dir, pp_home, pp_profile): self.mon=Monitor() self.mon.on() #instantiate arguments self.show_id=show_id self.root=root, self.canvas = canvas self.show_params=show_params self.track_params=track_params self.pp_dir=pp_dir self.pp_home=pp_home self.pp_profile=pp_profile # get duration limit (secs ) from profile if self.track_params['duration']<>"": self.duration= int(self.track_params['duration']) else: self.duration= int(self.show_params['duration']) self.duration_limit=20*self.duration # get background image from profile. self.background_file='' if self.track_params['background-image']<>"": self.background_file= self.track_params['background-image'] else: if self.track_params['display-show-background']=='yes': self.background_file= self.show_params['background-image'] # get background colour from profile. if self.track_params['background-colour']<>"": self.background_colour= self.track_params['background-colour'] else: self.background_colour= self.show_params['background-colour'] #get animation instructions from profile self.animate_begin_text=self.track_params['animate-begin'] self.animate_end_text=self.track_params['animate-end'] # open the plugin Manager self.pim=PluginManager(self.show_id,self.root,self.canvas,self.show_params,self.track_params,self.pp_dir,self.pp_home,self.pp_profile) #create an instance of PPIO so we can create gpio events self.ppio = PPIO() # could put instance generation in play, not sure which is better. self.bplayer=uzblDriver(self.canvas) self.command_timer=None self.tick_timer=None self.init_play_state_machine() def play(self, track, showlist, end_callback, ready_callback, enable_menu=False): #instantiate arguments self.track=track self.showlist=showlist self.end_callback=end_callback # callback when finished self.ready_callback=ready_callback #callback when ready to play self.enable_menu=enable_menu # callback to the calling object to e.g remove egg timer. if self.ready_callback<>None: self.ready_callback() # create an instance of showmanager so we can control concurrent shows self.show_manager=ShowManager(self.show_id,self.showlist,self.show_params,self.root,self.canvas,self.pp_dir,self.pp_profile,self.pp_home) #web window if self.track_params['web-window']<>'': self.web_window= self.track_params['web-window'] else: self.web_window= self.show_params['web-window'] reason,message,command,has_window,x1,y1,x2,y2=self.parse_window(self.web_window) if reason =='error': self.mon.err(self,'web window error: '+' ' + message + ' in ' + self.web_window) self.end_callback(reason,message) self=None else: #deal with web_window if has_window==False: self.geometry = ' --geometry=maximized ' else: width=x2-x1 height=y2-y1 self.geometry = "--geometry=%dx%d%+d%+d " % (width,height,x1,y1) # get browser commands reason,message=self.parse_commands(self.track_params['browser-commands']) if reason != 'normal': self.mon.err(self,message) self.end_callback(reason,message) self=None else: # Control other shows at beginning reason,message=self.show_manager.show_control(self.track_params['show-control-begin']) if reason == 'error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: #display content reason,message=self.display_content() if reason == 'error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: # create animation events reason,message=self.ppio.animate(self.animate_begin_text,id(self)) if reason=='error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: # start playing the track. self.start_play_state_machine() def terminate(self,reason): """ terminate the player in special circumstances normal user termination if by key_pressed 'stop' reason will be killed or error """ # circumvents state machine to terminate lower level and then itself. if self.bplayer<>None: self.mon.log(self,"sent terminate to uzbldriver") self.bplayer.terminate(reason) self.end('killed',' end without waiting for uzbl to finish') # end without waiting else: self.mon.log(self,"terminate, uzbldriver not running") self.end('killed','terminate, uzbldriver not running') def get_links(self): return self.track_params['links'] def input_pressed(self,symbol): # print symbol, symbol[0:5] if symbol[0:5]=='uzbl-': self.control(symbol[5:]) elif symbol == 'pause': self.pause() elif symbol=='stop': self.stop() # *************************************** # INTERNAL FUNCTIONS # *************************************** #browser do not do pause def pause(self): self.mon.log(self,"!<pause rejected") return False # other control when playing, not currently used def control(self,char): if self.play_state==BrowserPlayer._PLAYING and char not in ('exit'): self.mon.log(self,"> send control to uzbl:"+ char) self.bplayer.control(char) return True else: self.mon.log(self,"!<control rejected") return False # respond to normal stop def stop(self): # send signal to stop the track to the state machine self.mon.log(self,">stop received") self.quit_signal=True # *************************************** # sequencing # *************************************** """self. play_state controls the playing sequence, it has the following values. I am not entirely sure the starting and ending states are required. - _closed - the mplayer process is not running, mplayer process can be initiated - _starting - mplayer process is running but is not yet able to receive controls - _playing - playing a track, controls can be sent - _ending - mplayer is doing its termination, controls cannot be sent """ def init_play_state_machine(self): self.quit_signal=False self.play_state=BrowserPlayer._CLOSED def start_play_state_machine(self): #initialise all the state machine variables self.duration_count = 0 self.quit_signal=False # signal that user has pressed stop #play the track self.bplayer.play(self.track,self.geometry) self.mon.log (self,'Playing track from show Id: '+ str(self.show_id)) self.play_state=BrowserPlayer._STARTING # and start polling for state changes and count duration self.tick_timer=self.canvas.after(50, self.play_state_machine) def play_state_machine(self): if self.play_state == BrowserPlayer._CLOSED: self.mon.log(self," State machine: " + self.play_state) return elif self.play_state == BrowserPlayer._STARTING: # self.mon.log(self," State machine: " + self.play_state) # if uzbl fifo is available can send comands to uzbl but change to wait state to wait for it to appear on screen if self.bplayer.start_play_signal==True: self.mon.log(self," <fifo available signal received from uzbl") self.bplayer.start_play_signal=False self.play_state=BrowserPlayer._WAITING # get rid of status bar self.bplayer.control('set show_status = 0') # and get ready to wait for browser to appear self.wait_count= 50 # 10 seconds at 200mS steps self.mon.log(self," State machine: uzbl process alive") self.tick_timer=self.canvas.after(200, self.play_state_machine) elif self.play_state == BrowserPlayer._WAITING: if self.wait_count==0: # set state to playing self.play_state = BrowserPlayer._PLAYING # and start executing the browser commands self.play_commands() self.mon.log(self," State machine: uzbl_playing started") self.wait_count -=1 self.tick_timer=self.canvas.after(200, self.play_state_machine) elif self.play_state == BrowserPlayer._PLAYING: self.duration_count+=1 # self.mon.log(self," State machine: " + self.play_state) # service any queued stop signals and test duration count if self.quit_signal==True or (self.duration_limit>0 and self.duration_count>self.duration_limit): self.mon.log(self," Service stop required signal or timeout") # self.quit_signal=False self.stop_bplayer() self.play_state = BrowserPlayer._ENDING # uzbl reports it is terminating so change to ending state if self.bplayer.end_play_signal: self.mon.log(self," <end play signal received") self.play_state = BrowserPlayer._ENDING self.tick_timer=self.canvas.after(50, self.play_state_machine) elif self.play_state == BrowserPlayer._ENDING: # self.mon.log(self," State machine: " + self.play_state) # if spawned process has closed can change to closed state # self.mon.log (self," State machine : is luakit process running? - " + str(self.bplayer.is_running())) if self.bplayer.is_running() ==False: self.mon.log(self," <uzbl process is dead") if self.quit_signal==True: self.quit_signal=False self.play_state = BrowserPlayer._CLOSED self.end('normal','quit required or timeout') else: self.tick_timer=self.canvas.after(50, self.play_state_machine) def stop_bplayer(self): # send signal to stop the track to the state machine self.mon.log(self," >send stop to uzbl driver") if self.play_state==BrowserPlayer._PLAYING: self.bplayer.stop() return True else: self.mon.log(self,"!<stop rejected") return False # ***************** # ending the player # ***************** def end(self,reason,message): # stop the plugin if self.pim<>None: self.pim.stop_plugin() # abort the timers if self.tick_timer<>None: self.canvas.after_cancel(self.tick_timer) self.tick_timer=None if self.command_timer<>None: self.canvas.after_cancel(self.command_timer) self.tick_timer=None # clean up and fifos and sockets left by uzbl os.system('rm -f /tmp/uzbl_*') if reason in ('error','killed'): self.end_callback(reason,message) self=None else: # normal end so do show control and animation # Control concurrent shows at end reason,message=self.show_manager.show_control(self.track_params['show-control-end']) if reason =='error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: # clear events list for this track if self.track_params['animate-clear']=='yes': self.ppio.clear_events_list(id(self)) # create animation events for ending reason,message=self.ppio.animate(self.animate_end_text,id(self)) if reason=='error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: self.end_callback('normal',"track has terminated or quit") self=None # ***************** # displaying things # ***************** def display_content(self): self.canvas.delete('pp-content') #background colour if self.background_colour<>'': self.canvas.config(bg=self.background_colour) if self.background_file<>'': self.background_img_file = self.complete_path(self.background_file) if not os.path.exists(self.background_img_file): self.mon.err(self,"Audio background file not found: "+ self.background_img_file) self.end('error',"Audio background file not found") else: pil_background_img=PIL.Image.open(self.background_img_file) self.background = PIL.ImageTk.PhotoImage(pil_background_img) self.drawn = self.canvas.create_image(int(self.canvas['width'])/2, int(self.canvas['height'])/2, image=self.background, anchor=CENTER, tag='pp-content') # execute the plugin if required if self.track_params['plugin']<>'': reason,message,self.track = self.pim.do_plugin(self.track,self.track_params['plugin'],) if reason <> 'normal': return reason,message # display hint text if enabled if self.enable_menu== True: self.canvas.create_text(int(self.show_params['hint-x']), int(self.show_params['hint-y']), text=self.show_params['hint-text'], fill=self.show_params['hint-colour'], font=self.show_params['hint-font'], anchor=NW, tag='pp-content') # display show text if enabled if self.show_params['show-text']<> ''and self.track_params['display-show-text']=='yes': self.canvas.create_text(int(self.show_params['show-text-x']),int(self.show_params['show-text-y']), anchor=NW, text=self.show_params['show-text'], fill=self.show_params['show-text-colour'], font=self.show_params['show-text-font'], tag='pp-content') # display track text if enabled if self.track_params['track-text']<> '': self.canvas.create_text(int(self.track_params['track-text-x']),int(self.track_params['track-text-y']), anchor=NW, text=self.track_params['track-text'], fill=self.track_params['track-text-colour'], font=self.track_params['track-text-font'], tag='pp-content') self.mon.log(self,"Displayed background and text ") self.canvas.tag_raise('pp-click-area') self.canvas.update_idletasks( ) return 'normal','' # ******************* # browser commands # *********************** def parse_commands(self,command_text): self.command_list=[] lines = command_text.split('\n') for line in lines: if line.strip()=="": continue reason,entry=self.parse_command(line) if reason != 'normal': return 'error',entry self.command_list.append(copy.deepcopy(entry)) # print self.command_list return 'normal','' def parse_command(self,line): fields = line.split() if fields[0]=='uzbl': # print fields[0], line[4:] return 'normal',[fields[0],line[4:]] if len(fields) not in (1,2): return 'error',"incorrect number of fields in command: " + line command=fields[0] arg='' if command not in ('load','refresh','wait','exit','loop'): return 'error','unknown command: '+ command if command in ('refresh','exit','loop') and len(fields)<>1: return 'error','incorrect number of fields for '+ command + 'in: ' + line if command == 'load': if len(fields)<>2: return 'error','incorrect number of fields for '+ command + 'in: ' + line else: arg = fields[1] if command == 'wait': if len(fields)<>2: return 'error','incorrect number of fields for '+ command + 'in: ' + line else: arg = fields[1] if not arg.isdigit():return 'error','Argument for Wait is not 0 or positive number in: ' + line return 'normal',[command,arg] def play_commands(self): if len(self.command_list)==0: return self.loop=0 self.command_index=0 self.canvas.after(100,self.execute_command) def execute_command(self): entry=self.command_list[self.command_index] command=entry[0] arg=entry[1] if self.command_index==len(self.command_list)-1: self.command_index=self.loop else: self.command_index+=1 # execute command if command == 'load': #self.canvas.focus_force() #self.root.lower() file=self.complete_path(arg) self.bplayer.control('uri '+ file) self.command_timer=self.canvas.after(10,self.execute_command) elif command == 'refresh': self.bplayer.control('reload_ign_cache') self.command_timer=self.canvas.after(10,self.execute_command) elif command == 'wait': self.command_timer=self.canvas.after(1000*int(arg),self.execute_command) elif command=='exit': self.quit_signal=True elif command=='loop': self.loop=self.command_index self.command_timer=self.canvas.after(10,self.execute_command) elif command=='uzbl': self.bplayer.control(arg) self.command_timer=self.canvas.after(10,self.execute_command) # ***************** # utilities # ***************** def complete_path(self,track_file): # complete path of the filename of the selected entry if track_file[0]=="+": track_file=self.pp_home+track_file[1:] self.mon.log(self,"Background image is "+ track_file) return track_file def parse_window(self,line): # parses warp _ or xy2 fields = line.split() # check there is a command field if len(fields) < 1: return 'error','no type field','',False,0,0,0,0 #deal with warp which has 1 or 5 arguments # check basic syntax if fields[0] <>'warp': return 'error','not a valid type','',False,0,0,0,0 if len(fields) not in (1,5): return 'error','wrong number of coordinates for warp','',False,0,0,0,0 # deal with window coordinates if len(fields) == 5: #window is specified if not (fields[1].isdigit() and fields[2].isdigit() and fields[3].isdigit() and fields[4].isdigit()): return 'error','coordinates are not positive integers','',False,0,0,0,0 has_window=True return 'normal','',fields[0],has_window,int(fields[1]),int(fields[2]),int(fields[3]),int(fields[4]) else: # fullscreen has_window=False return 'normal','',fields[0],has_window,0,0,0,0
class ShowManager(object): """ ShowManager manages PiPresents' concurrent shows. It does not manage sub-shows or child-shows but has a bit of common code to initilise them concurrent shows are always top level (level 0) shows: They can be opened/closed by the start show(open only) or by 'open/close myshow' in the Show Control field of players, by time of day sceduler or by OSC Two shows with the same show reference cannot be run concurrently as there is no way to reference an individual instance. However a workaround is to make the secong instance a subshow of a mediashow with a different reference. """ # Declare class variables shows = [] canvas = None #canvas for all shows shutdown_required = False SHOW_TEMPLATE = ['', None] SHOW_REF = 0 # show-reference - name of the show as in editor SHOW_OBJ = 1 # the python object showlist = [] # Initialise class variables, first time through only in pipresents.py def init(self, canvas, all_shows_ended_callback, command_callback, showlist): ShowManager.all_shows_ended_callback = all_shows_ended_callback ShowManager.shows = [] ShowManager.shutdown_required = False ShowManager.canvas = canvas ShowManager.command_callback = command_callback ShowManager.showlist = showlist # ************************************** # functions to manipulate show register # ************************************** def register_shows(self): for show in ShowManager.showlist.shows(): if show['show-ref'] != 'start': reason, message = self.register_show(show['show-ref']) if reason == 'error': return reason, message return 'normal', 'shows regiistered' def register_show(self, ref): registered = self.show_registered(ref) if registered == -1: ShowManager.shows.append(copy.deepcopy(ShowManager.SHOW_TEMPLATE)) index = len(ShowManager.shows) - 1 ShowManager.shows[index][ShowManager.SHOW_REF] = ref ShowManager.shows[index][ShowManager.SHOW_OBJ] = None self.mon.trace( self, ' - register show: show_ref = ' + ref + ' index = ' + str(index)) return 'normal', 'show registered' else: # self.mon.err(self, ' more than one show in showlist with show-ref: ' + ref ) return 'error', ' more than one show in showlist with show-ref: ' + ref # is the show registered? # can be used to return the index to the show def show_registered(self, show_ref): index = 0 for show in ShowManager.shows: if show[ShowManager.SHOW_REF] == show_ref: return index index += 1 return -1 # needs calling program to check that the show is not already running def set_running(self, index, show_obj): ShowManager.shows[index][ShowManager.SHOW_OBJ] = show_obj self.mon.trace( self, 'show_ref= ' + ShowManager.shows[index][ShowManager.SHOW_REF] + ' show_id= ' + str(index)) # is the show running? def show_running(self, index): if ShowManager.shows[index][ShowManager.SHOW_OBJ] is not None: return ShowManager.shows[index][ShowManager.SHOW_OBJ] else: return None def set_exited(self, index): ShowManager.shows[index][ShowManager.SHOW_OBJ] = None self.mon.trace( self, 'show_ref= ' + ShowManager.shows[index][ShowManager.SHOW_REF] + ' show_id= ' + str(index)) # are all shows exited? def all_shows_exited(self): all_exited = True for show in ShowManager.shows: if show[ShowManager.SHOW_OBJ] is not None: all_exited = False return all_exited # fromat for printing def pretty_shows(self): shows = '\n' for show in ShowManager.shows: shows += show[ShowManager.SHOW_REF] + '\n' return shows # ********************************* # show control # ********************************* # show manager can be initialised by a player, shower or by pipresents.py # if by pipresents.py then show_id=-1 def __init__(self, show_id, showlist, show_params, root, canvas, pp_dir, pp_profile, pp_home): self.show_id = show_id self.showlist = showlist self.show_params = show_params self.root = root self.show_canvas = canvas self.pp_dir = pp_dir self.pp_profile = pp_profile self.pp_home = pp_home if not 'plugin' in self.show_params: self.show_params['plugin'] = '' self.pim = PluginManager(self.show_id, self.root, self.show_canvas, self.show_params, None, self.pp_dir, self.pp_home, self.pp_profile) self.mon = Monitor() def control_a_show(self, show_ref, show_command): if show_command == 'open': return self.start_show(show_ref) elif show_command == 'close': return self.exit_show(show_ref) else: return 'error', 'command not recognised ' + show_command def exit_all_shows(self): for show in ShowManager.shows: self.exit_show(show[ShowManager.SHOW_REF]) self.pim.stop_plugin() return 'normal', 'exited all shows' # kick off the exit sequence of a show by calling the shows exit method. # it will result in all the shows in a stack being closed and end_play_show being called def exit_show(self, show_ref): index = self.show_registered(show_ref) self.mon.log(self, "Exiting show " + show_ref + ' show index:' + str(index)) show_obj = self.show_running(index) if show_obj is not None: show_obj.exit() return 'normal', 'exited a concurrent show' def start_show(self, show_ref): index = self.show_registered(show_ref) if index < 0: return 'error', "Show not found in showlist: " + show_ref show_index = self.showlist.index_of_show(show_ref) show = self.showlist.show(show_index) reason, message, show_canvas = self.compute_show_canvas(show) if reason == 'error': return reason, message #print 'STARTING TOP LEVEL SHOW',show_canvas self.mon.log( self, 'Starting Show: ' + show_ref + ' from: ' + self.show_params['show-ref']) if self.show_running(index): self.mon.warn( self, "show already running so ignoring command: " + show_ref) return 'normal', 'this concurrent show already running' show_obj = self.init_show(index, show, show_canvas) if show_obj is None: return 'error', "unknown show type in start concurrent show - " + show[ 'type'] else: self.set_running(index, show_obj) # params - end_callback, show_ready_callback, parent_kickback_signal, level show_obj.play(self._end_play_show, None, False, 0, []) if not self.pim.is_running: self.pim.load_plugin(None, self.show_params['plugin']) self.pim.draw_plugin() self.pim.show_plugin() return 'normal', 'concurrent show started' # used by shows to create subshows or child shows def init_subshow(self, show_id, show, show_canvas): return self.init_show(show_id, show, show_canvas) def _end_play_show(self, index, reason, message): show_ref_to_exit = ShowManager.shows[index][ShowManager.SHOW_REF] show_to_exit = ShowManager.shows[index][ShowManager.SHOW_OBJ] self.mon.log( self, 'Exited from show: ' + show_ref_to_exit + ' ' + str(index)) self.mon.log(self, 'Exited with Reason = ' + reason) self.mon.trace( self, ' Show is: ' + show_ref_to_exit + ' show index ' + str(index)) # closes the video/audio from last track then closes the track # print 'show to exit ',show_to_exit, show_to_exit.current_player,show_to_exit.previous_player self.set_exited(index) if self.all_shows_exited() is True: ShowManager.all_shows_ended_callback(reason, message) return reason, message # common function to initilaise the show by type def init_show( self, show_id, selected_show, show_canvas, ): if selected_show['type'] == "mediashow": return MediaShow(show_id, selected_show, self.root, show_canvas, self.showlist, self.pp_dir, self.pp_home, self.pp_profile, ShowManager.command_callback) elif selected_show['type'] == "liveshow": return LiveShow(show_id, selected_show, self.root, show_canvas, self.showlist, self.pp_dir, self.pp_home, self.pp_profile, ShowManager.command_callback) elif selected_show['type'] == "radiobuttonshow": return RadioButtonShow(show_id, selected_show, self.root, show_canvas, self.showlist, self.pp_dir, self.pp_home, self.pp_profile, ShowManager.command_callback) elif selected_show['type'] == "hyperlinkshow": return HyperlinkShow(show_id, selected_show, self.root, show_canvas, self.showlist, self.pp_dir, self.pp_home, self.pp_profile, ShowManager.command_callback) elif selected_show['type'] == "menu": return MenuShow(show_id, selected_show, self.root, show_canvas, self.showlist, self.pp_dir, self.pp_home, self.pp_profile, ShowManager.command_callback) elif selected_show['type'] == "artmediashow": return ArtMediaShow(show_id, selected_show, self.root, show_canvas, self.showlist, self.pp_dir, self.pp_home, self.pp_profile, ShowManager.command_callback) elif selected_show['type'] == "artliveshow": return ArtLiveShow(show_id, selected_show, self.root, show_canvas, self.showlist, self.pp_dir, self.pp_home, self.pp_profile, ShowManager.command_callback) else: return None def compute_show_canvas(self, show_params): canvas = {} canvas['canvas-obj'] = ShowManager.canvas status, message, self.show_canvas_x1, self.show_canvas_y1, self.show_canvas_x2, self.show_canvas_y2 = self.parse_show_canvas( show_params['show-canvas']) if status == 'error': # self.mon.err(self,'show canvas error: ' + message + ' in ' + show_params['show-canvas']) return 'error', 'show canvas error: ' + message + ' in ' + show_params[ 'show-canvas'], canvas else: self.show_canvas_width = self.show_canvas_x2 - self.show_canvas_x1 self.show_canvas_height = self.show_canvas_y2 - self.show_canvas_y1 self.show_canvas_centre_x = self.show_canvas_width / 2 self.show_canvas_centre_y = self.show_canvas_height / 2 canvas['show-canvas-x1'] = self.show_canvas_x1 canvas['show-canvas-y1'] = self.show_canvas_y1 canvas['show-canvas-x2'] = self.show_canvas_x2 canvas['show-canvas-y2'] = self.show_canvas_y2 canvas['show-canvas-width'] = self.show_canvas_width canvas['show-canvas-height'] = self.show_canvas_height canvas['show-canvas-centre-x'] = self.show_canvas_centre_x canvas['show-canvas-centre-y'] = self.show_canvas_centre_y return 'normal', '', canvas def parse_show_canvas(self, text): fields = text.split() # blank so show canvas is the whole screen if len(fields) < 1: return 'normal', '', 0, 0, int(self.canvas['width']), int( self.canvas['height']) elif len(fields) == 4: # window is specified if not (fields[0].isdigit() and fields[1].isdigit() and fields[2].isdigit() and fields[3].isdigit()): return 'error', 'coordinates are not positive integers', 0, 0, 0, 0 return 'normal', '', int(fields[0]), int(fields[1]), int( fields[2]), int(fields[3]) else: # error return 'error', 'illegal Show canvas dimensions ' + text, 0, 0, 0, 0
class ShowManager(object): """ ShowManager manages PiPresents' concurrent shows. It does not manage sub-shows or child-shows but has a bit of common code to initilise them concurrent shows are always top level (level 0) shows: They can be opened/closed by the start show(open only) or by 'open/close myshow' in the Show Control field of players, by time of day sceduler or by OSC Two shows with the same show reference cannot be run concurrently as there is no way to reference an individual instance. However a workaround is to make the secong instance a subshow of a mediashow with a different reference. """ # Declare class variables shows = [] canvas = None # canvas for all shows shutdown_required = False SHOW_TEMPLATE = ["", None] SHOW_REF = 0 # show-reference - name of the show as in editor SHOW_OBJ = 1 # the python object showlist = [] # Initialise class variables, first time through only in pipresents.py def init(self, canvas, all_shows_ended_callback, command_callback, showlist): ShowManager.all_shows_ended_callback = all_shows_ended_callback ShowManager.shows = [] ShowManager.shutdown_required = False ShowManager.canvas = canvas ShowManager.command_callback = command_callback ShowManager.showlist = showlist # ************************************** # functions to manipulate show register # ************************************** def register_shows(self): for show in ShowManager.showlist.shows(): if show["show-ref"] != "start": reason, message = self.register_show(show["show-ref"]) if reason == "error": return reason, message return "normal", "shows regiistered" def register_show(self, ref): registered = self.show_registered(ref) if registered == -1: ShowManager.shows.append(copy.deepcopy(ShowManager.SHOW_TEMPLATE)) index = len(ShowManager.shows) - 1 ShowManager.shows[index][ShowManager.SHOW_REF] = ref ShowManager.shows[index][ShowManager.SHOW_OBJ] = None self.mon.trace(self, " - register show: show_ref = " + ref + " index = " + str(index)) return "normal", "show registered" else: # self.mon.err(self, ' more than one show in showlist with show-ref: ' + ref ) return "error", " more than one show in showlist with show-ref: " + ref # is the show registered? # can be used to return the index to the show def show_registered(self, show_ref): index = 0 for show in ShowManager.shows: if show[ShowManager.SHOW_REF] == show_ref: return index index += 1 return -1 # needs calling program to check that the show is not already running def set_running(self, index, show_obj): ShowManager.shows[index][ShowManager.SHOW_OBJ] = show_obj self.mon.trace(self, "show_ref= " + ShowManager.shows[index][ShowManager.SHOW_REF] + " show_id= " + str(index)) # is the show running? def show_running(self, index): if ShowManager.shows[index][ShowManager.SHOW_OBJ] is not None: return ShowManager.shows[index][ShowManager.SHOW_OBJ] else: return None def set_exited(self, index): ShowManager.shows[index][ShowManager.SHOW_OBJ] = None self.mon.trace(self, "show_ref= " + ShowManager.shows[index][ShowManager.SHOW_REF] + " show_id= " + str(index)) # are all shows exited? def all_shows_exited(self): all_exited = True for show in ShowManager.shows: if show[ShowManager.SHOW_OBJ] is not None: all_exited = False return all_exited # fromat for printing def pretty_shows(self): shows = "\n" for show in ShowManager.shows: shows += show[ShowManager.SHOW_REF] + "\n" return shows # ********************************* # show control # ********************************* # show manager can be initialised by a player, shower or by pipresents.py # if by pipresents.py then show_id=-1 def __init__(self, show_id, showlist, show_params, root, canvas, pp_dir, pp_profile, pp_home): self.show_id = show_id self.showlist = showlist self.show_params = show_params self.root = root self.show_canvas = canvas self.pp_dir = pp_dir self.pp_profile = pp_profile self.pp_home = pp_home if not "plugin" in self.show_params: self.show_params["plugin"] = "" self.pim = PluginManager( self.show_id, self.root, self.show_canvas, self.show_params, None, self.pp_dir, self.pp_home, self.pp_profile, ) self.mon = Monitor() def control_a_show(self, show_ref, show_command): if show_command == "open": return self.start_show(show_ref) elif show_command == "close": return self.exit_show(show_ref) else: return "error", "command not recognised " + show_command def exit_all_shows(self): for show in ShowManager.shows: self.exit_show(show[ShowManager.SHOW_REF]) self.pim.stop_plugin() return "normal", "exited all shows" # kick off the exit sequence of a show by calling the shows exit method. # it will result in all the shows in a stack being closed and end_play_show being called def exit_show(self, show_ref): index = self.show_registered(show_ref) self.mon.log(self, "Exiting show " + show_ref + " show index:" + str(index)) show_obj = self.show_running(index) if show_obj is not None: show_obj.exit() return "normal", "exited a concurrent show" def start_show(self, show_ref): index = self.show_registered(show_ref) if index < 0: return "error", "Show not found in showlist: " + show_ref show_index = self.showlist.index_of_show(show_ref) show = self.showlist.show(show_index) reason, message, show_canvas = self.compute_show_canvas(show) if reason == "error": return reason, message # print 'STARTING TOP LEVEL SHOW',show_canvas self.mon.log(self, "Starting Show: " + show_ref + " from: " + self.show_params["show-ref"]) if self.show_running(index): self.mon.warn(self, "show already running so ignoring command: " + show_ref) return "normal", "this concurrent show already running" show_obj = self.init_show(index, show, show_canvas) if show_obj is None: return "error", "unknown show type in start concurrent show - " + show["type"] else: self.set_running(index, show_obj) # params - end_callback, show_ready_callback, parent_kickback_signal, level show_obj.play(self._end_play_show, None, False, 0, []) if not self.pim.is_running: self.pim.load_plugin(None, self.show_params["plugin"]) self.pim.draw_plugin() self.pim.show_plugin() return "normal", "concurrent show started" # used by shows to create subshows or child shows def init_subshow(self, show_id, show, show_canvas): return self.init_show(show_id, show, show_canvas) def _end_play_show(self, index, reason, message): show_ref_to_exit = ShowManager.shows[index][ShowManager.SHOW_REF] show_to_exit = ShowManager.shows[index][ShowManager.SHOW_OBJ] self.mon.log(self, "Exited from show: " + show_ref_to_exit + " " + str(index)) self.mon.log(self, "Exited with Reason = " + reason) self.mon.trace(self, " Show is: " + show_ref_to_exit + " show index " + str(index)) # closes the video/audio from last track then closes the track # print 'show to exit ',show_to_exit, show_to_exit.current_player,show_to_exit.previous_player self.set_exited(index) if self.all_shows_exited() is True: ShowManager.all_shows_ended_callback(reason, message) return reason, message # common function to initilaise the show by type def init_show(self, show_id, selected_show, show_canvas): if selected_show["type"] == "mediashow": return MediaShow( show_id, selected_show, self.root, show_canvas, self.showlist, self.pp_dir, self.pp_home, self.pp_profile, ShowManager.command_callback, ) elif selected_show["type"] == "liveshow": return LiveShow( show_id, selected_show, self.root, show_canvas, self.showlist, self.pp_dir, self.pp_home, self.pp_profile, ShowManager.command_callback, ) elif selected_show["type"] == "radiobuttonshow": return RadioButtonShow( show_id, selected_show, self.root, show_canvas, self.showlist, self.pp_dir, self.pp_home, self.pp_profile, ShowManager.command_callback, ) elif selected_show["type"] == "hyperlinkshow": return HyperlinkShow( show_id, selected_show, self.root, show_canvas, self.showlist, self.pp_dir, self.pp_home, self.pp_profile, ShowManager.command_callback, ) elif selected_show["type"] == "menu": return MenuShow( show_id, selected_show, self.root, show_canvas, self.showlist, self.pp_dir, self.pp_home, self.pp_profile, ShowManager.command_callback, ) elif selected_show["type"] == "artmediashow": return ArtMediaShow( show_id, selected_show, self.root, show_canvas, self.showlist, self.pp_dir, self.pp_home, self.pp_profile, ShowManager.command_callback, ) elif selected_show["type"] == "artliveshow": return ArtLiveShow( show_id, selected_show, self.root, show_canvas, self.showlist, self.pp_dir, self.pp_home, self.pp_profile, ShowManager.command_callback, ) else: return None def compute_show_canvas(self, show_params): canvas = {} canvas["canvas-obj"] = ShowManager.canvas status, message, self.show_canvas_x1, self.show_canvas_y1, self.show_canvas_x2, self.show_canvas_y2 = self.parse_show_canvas( show_params["show-canvas"] ) if status == "error": # self.mon.err(self,'show canvas error: ' + message + ' in ' + show_params['show-canvas']) return "error", "show canvas error: " + message + " in " + show_params["show-canvas"], canvas else: self.show_canvas_width = self.show_canvas_x2 - self.show_canvas_x1 self.show_canvas_height = self.show_canvas_y2 - self.show_canvas_y1 self.show_canvas_centre_x = self.show_canvas_width / 2 self.show_canvas_centre_y = self.show_canvas_height / 2 canvas["show-canvas-x1"] = self.show_canvas_x1 canvas["show-canvas-y1"] = self.show_canvas_y1 canvas["show-canvas-x2"] = self.show_canvas_x2 canvas["show-canvas-y2"] = self.show_canvas_y2 canvas["show-canvas-width"] = self.show_canvas_width canvas["show-canvas-height"] = self.show_canvas_height canvas["show-canvas-centre-x"] = self.show_canvas_centre_x canvas["show-canvas-centre-y"] = self.show_canvas_centre_y return "normal", "", canvas def parse_show_canvas(self, text): fields = text.split() # blank so show canvas is the whole screen if len(fields) < 1: return "normal", "", 0, 0, int(self.canvas["width"]), int(self.canvas["height"]) elif len(fields) == 4: # window is specified if not (fields[0].isdigit() and fields[1].isdigit() and fields[2].isdigit() and fields[3].isdigit()): return "error", "coordinates are not positive integers", 0, 0, 0, 0 return "normal", "", int(fields[0]), int(fields[1]), int(fields[2]), int(fields[3]) else: # error return "error", "illegal Show canvas dimensions " + text, 0, 0, 0, 0
class MessagePlayer: """ Displays lines of text in the centre of a coloured screen with background image See pp_imageplayer for common software design description """ # ******************* # external commands # ******************* def __init__(self,show_id,root,canvas,show_params,track_params,pp_dir,pp_home,pp_profile): self.mon=Monitor() self.mon.on() self.root=root self.canvas=canvas self.show_id=show_id self.track_params=track_params self.show_params=show_params self.pp_dir=pp_dir self.pp_home=pp_home self.pp_profile=pp_profile # get config from medialist if there. if 'duration' in self.track_params and self.track_params['duration']<>"": self.duration= int(self.track_params['duration']) else: self.duration= int(self.show_params['duration']) # get background image from profile. self.background_file='' if self.track_params['background-image']<>"": self.background_file= self.track_params['background-image'] else: if self.track_params['display-show-background']=='yes': self.background_file= self.show_params['background-image'] # get background colour from profile. if self.track_params['background-colour']<>"": self.background_colour= self.track_params['background-colour'] else: self.background_colour= self.show_params['background-colour'] self.centre_x = int(self.canvas['width'])/2 self.centre_y = int(self.canvas['height'])/2 # keep tick as an integer sub-multiple of 1 second self.tick = 100 # tick time for image display (milliseconds) self.dwell = 1000*self.duration #get animation instructions from profile self.animate_begin_text=self.track_params['animate-begin'] self.animate_end_text=self.track_params['animate-end'] # open the plugin Manager self.pim=PluginManager(self.show_id,self.root,self.canvas,self.show_params,self.track_params,self.pp_dir,self.pp_home,self.pp_profile) #create an instance of PPIO so we can create gpio events self.ppio = PPIO() def play(self, text, showlist, end_callback, ready_callback, enable_menu=False): # instantiate arguments self.text=text self.showlist=showlist self.end_callback=end_callback self.ready_callback=ready_callback self.enable_menu=enable_menu #init state and signals self.quit_signal=False self.tick_timer=None self.drawn=None # create an instance of showmanager so we can control concurrent shows self.show_manager=ShowManager(self.show_id,self.showlist,self.show_params,self.root,self.canvas,self.pp_dir,self.pp_profile,self.pp_home) # Control other shows at beginning reason,message=self.show_manager.show_control(self.track_params['show-control-begin']) if reason == 'error': self.end_callback(reason,message) self=None else: #display content reason,message=self.display_content() if reason == 'error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: # create animation events reason,message=self.ppio.animate(self.animate_begin_text,id(self)) if reason=='error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: # start text display self.start_dwell() def terminate(self,reason): # no lower level things to terminate so just go to end self.end(reason,'kill or error') def get_links(self): return self.track_params['links'] def input_pressed(self,symbol): self.mon.log(self,"input received: "+symbol) if symbol=='stop': self.stop() # ******************* # internal functions # ******************* def stop(self): self.quit_signal=True # ******************* # sequencing # ******************* def start_dwell(self): self.dwell_counter=0 if self.ready_callback<>None: self.ready_callback() self.tick_timer=self.canvas.after(self.tick, self.do_dwell) def do_dwell(self): if self.quit_signal == True: self.mon.log(self,"quit received") self.end('normal','user quit') else: if self.dwell<>0: self.dwell_counter=self.dwell_counter+1 if self.dwell_counter==self.dwell/self.tick: self.end('normal','finished') else: self.tick_timer=self.canvas.after(self.tick, self.do_dwell) else: self.tick_timer=self.canvas.after(self.tick, self.do_dwell) # ***************** # ending the player # ***************** def end(self,reason,message): # stop the plugin if self.track_params['plugin']<>'': self.pim.stop_plugin() # abort the timer if self.tick_timer<>None: self.canvas.after_cancel(self.tick_timer) self.tick_timer=None if reason in ('error','killed'): self.end_callback(reason,message) self=None else: # normal end so do show control # Control concurrent shows at end reason,message=self.show_manager.show_control(self.track_params['show-control-end']) if reason =='error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: # clear events list for this track if self.track_params['animate-clear']=='yes': self.ppio.clear_events_list(id(self)) # create animation events for ending reason,message=self.ppio.animate(self.animate_end_text,id(self)) if reason=='error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: self.end_callback('normal',"track has terminated or quit") self=None # ***************** # displaying things # ***************** def display_content(self): if self.background_colour<>'': self.canvas.config(bg=self.background_colour) self.canvas.delete('pp-content') if self.background_file<>'': self.background_img_file = self.complete_path(self.background_file) if not os.path.exists(self.background_img_file): self.mon.err(self,"Message background file not found: "+ self.background_img_file) self.end('error',"Message background file not found") else: pil_background_img=PIL.Image.open(self.background_img_file) self.background = PIL.ImageTk.PhotoImage(pil_background_img) self.drawn = self.canvas.create_image(int(self.canvas['width'])/2, int(self.canvas['height'])/2, image=self.background, anchor=CENTER, tag='pp-content') # display show text if enabled if self.show_params['show-text']<> ''and self.track_params['display-show-text']=='yes': self.canvas.create_text(int(self.show_params['show-text-x']),int(self.show_params['show-text-y']), anchor=NW, text=self.show_params['show-text'], fill=self.show_params['show-text-colour'], font=self.show_params['show-text-font'], tag='pp-content') # display track text if enabled if self.track_params['track-text']<> '': self.canvas.create_text(int(self.track_params['track-text-x']),int(self.track_params['track-text-y']), anchor=NW, text=self.track_params['track-text'], fill=self.track_params['track-text-colour'], font=self.track_params['track-text-font'], tag='pp-content') # execute the plugin if required if self.track_params['plugin']<>'': reason,message,self.text = self.pim.do_plugin(self.text,self.track_params['plugin']) if reason <> 'normal': return reason,message # display message text if self.track_params['message-x']<>'': self.canvas.create_text(int(self.track_params['message-x']), int(self.track_params['message-y']), text=self.text.rstrip('\n'), fill=self.track_params['message-colour'], font=self.track_params['message-font'], justify=self.track_params['message-justify'], anchor = 'nw', tag='pp-content') else: self.canvas.create_text(int(self.canvas['width'])/2, int(self.canvas['height'])/2, text=self.text.rstrip('\n'), fill=self.track_params['message-colour'], font=self.track_params['message-font'], justify=self.track_params['message-justify'], tag='pp-content') # display instructions (hint) if self.enable_menu==True: self.canvas.create_text(int(self.show_params['hint-x']), int(self.show_params['hint-y']), text=self.show_params['hint-text'], fill=self.show_params['hint-colour'], font=self.show_params['hint-font'], anchor=NW, tag='pp-content') self.canvas.tag_raise('pp-click-area') self.canvas.update_idletasks( ) return 'normal','' # ***************** # utilities # ***************** def complete_path(self,track_file): # complete path of the filename of the selected entry if track_file[0]=="+": track_file=self.pp_home+track_file[1:] self.mon.log(self,"Background image is "+ track_file) return track_file
class VideoPlayer: """ plays a track using omxplayer See pp_imageplayer for common software design description """ _CLOSED = "omx_closed" #probably will not exist _STARTING = "omx_starting" #track is being prepared _PLAYING = "omx_playing" #track is playing to the screen, may be paused _ENDING = "omx_ending" #track is in the process of ending due to quit or end of track # *************************************** # EXTERNAL COMMANDS # *************************************** def __init__(self, show_id, root, canvas, show_params, track_params , pp_dir, pp_home, pp_profile): self.mon=Monitor() self.mon.on() #instantiate arguments self.show_id=show_id self.root=root self.canvas = canvas self.show_params=show_params self.track_params=track_params self.pp_dir=pp_dir self.pp_home=pp_home self.pp_profile=pp_profile # get config from medialist if there. if self.track_params['omx-audio']<>"": self.omx_audio= self.track_params['omx-audio'] else: self.omx_audio= self.show_params['omx-audio'] if self.omx_audio<>"": self.omx_audio= "-o "+ self.omx_audio if self.track_params['omx-volume']<>"": self.omx_volume= self.track_params['omx-volume'] else: self.omx_volume= self.show_params['omx-volume'] if self.omx_volume<>"": self.omx_volume= "--vol "+ str(int(self.omx_volume)*100) + ' ' if self.track_params['omx-window']<>'': self.omx_window= self.track_params['omx-window'] else: self.omx_window= self.show_params['omx-window'] # get background image from profile. self.background_file='' if self.track_params['background-image']<>"": self.background_file= self.track_params['background-image'] else: if self.track_params['display-show-background']=='yes': self.background_file= self.show_params['background-image'] # get background colour from profile. if self.track_params['background-colour']<>"": self.background_colour= self.track_params['background-colour'] else: self.background_colour= self.show_params['background-colour'] self.centre_x = int(self.canvas['width'])/2 self.centre_y = int(self.canvas['height'])/2 #get animation instructions from profile self.animate_begin_text=self.track_params['animate-begin'] self.animate_end_text=self.track_params['animate-end'] # open the plugin Manager self.pim=PluginManager(self.show_id,self.root,self.canvas,self.show_params,self.track_params,self.pp_dir,self.pp_home,self.pp_profile) #create an instance of PPIO so we can create gpio events self.ppio = PPIO() # could put instance generation in play, not sure which is better. self.omx=OMXDriver(self.canvas) self.tick_timer=None self.init_play_state_machine() def play(self, track, showlist, end_callback, ready_callback, enable_menu=False): #instantiate arguments self.track=track self.showlist=showlist self.ready_callback=ready_callback #callback when ready to play self.end_callback=end_callback # callback when finished self.enable_menu = enable_menu # callback to the calling object to e.g remove egg timer and enable click areas. if self.ready_callback<>None: self.ready_callback() # create an instance of showmanager so we can control concurrent shows self.show_manager=ShowManager(self.show_id,self.showlist,self.show_params,self.root,self.canvas,self.pp_dir,self.pp_profile,self.pp_home) #set up video window reason,message,comand,has_window,x1,y1,x2,y2= self.parse_window(self.omx_window) if reason =='error': self.mon.err(self,'omx window error: ' + message + ' in ' + self.omx_window) self.end_callback(reason,message) else: if has_window==True: self.omx_window= '--win " '+ str(x1) + ' ' + str(y1) + ' ' + str(x2) + ' ' + str(y2) + ' " ' else: self.omx_window='' # Control other shows at beginning reason,message=self.show_manager.show_control(self.track_params['show-control-begin']) if reason in ('error','killed'): self.end_callback(reason,message) self=None else: #display content reason,message=self.display_content() if reason == 'error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: # create animation events reason,message=self.ppio.animate(self.animate_begin_text,id(self)) if reason=='error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: # start playing the video. if self.play_state == VideoPlayer._CLOSED: self.mon.log(self,">play track received") self.start_play_state_machine(self.track) else: self.mon.err(self,'play track rejected') self.end_callback('error','play track rejected') self=None def terminate(self,reason): # circumvents state machine and does not wait for omxplayer to close if self.omx<>None: self.mon.log(self,"sent terminate to omxdriver") self.omx.terminate(reason) self.end('killed',' end without waiting for omxplayer to finish') # end without waiting else: self.mon.log(self,"terminate, omxdriver not running") self.end('killed','terminate, mplayerdriver not running') def input_pressed(self,symbol): if symbol[0:4]=='omx-': self.control(symbol[4]) elif symbol =='pause': self.pause() elif symbol=='stop': self.stop() else: pass def get_links(self): return self.track_params['links'] # *************************************** # INTERNAL FUNCTIONS # *************************************** # respond to normal stop def stop(self): # send signal to stop the track to the state machine self.mon.log(self,">stop received") self.quit_signal=True #toggle pause def pause(self): if self.play_state in (VideoPlayer._PLAYING,VideoPlayer._ENDING): self.omx.pause() return True #NIK # for pausing video after first frame elif self.play_state == VideoPlayer._STARTING: self.omx.delayed_pause = True return True #NIK else: self.mon.log(self,"!<pause rejected") return False # other control when playing def control(self,char): if self.play_state==VideoPlayer._PLAYING and char not in ('q'): self.mon.log(self,"> send control to omx: "+ char) self.omx.control(char) return True else: self.mon.log(self,"!<control rejected") return False # *********************** # sequencing # ********************** """self. play_state controls the playing sequence, it has the following values. I am not entirely sure the starting and ending states are required. - _closed - the omx process is not running, omx process can be initiated - _starting - omx process is running but is not yet able to receive controls - _playing - playing a track, controls can be sent - _ending - omx is doing its termination, controls cannot be sent """ def init_play_state_machine(self): self.quit_signal=False self.play_state=VideoPlayer._CLOSED def start_play_state_machine(self,track): #initialise all the state machine variables #self.iteration = 0 # for debugging self.quit_signal=False # signal that user has pressed stop self.play_state=VideoPlayer._STARTING #play the selected track options=self.omx_audio+ " " + self.omx_volume + ' ' + self.omx_window + ' ' + self.show_params['omx-other-options']+" " # NIK ADDITION # adding subtitles file for video if 'omx-subtitles' in self.track_params and self.track_params['omx-subtitles'] <> '': subtitles_full_path = self.complete_path(self.track_params['omx-subtitles']) if os.path.exists (subtitles_full_path): options += '--font-size 40 --subtitles "' + subtitles_full_path + '" ' if 'omx-subtitles-numlines' in self.track_params and self.track_params['omx-subtitles-numlines'] <> '': options += '--lines ' + self.track_params['omx-subtitles-numlines'] + ' ' # END NIK ADDITION self.omx.play(track,options) self.mon.log (self,'Playing track from show Id: '+ str(self.show_id)) # and start polling for state changes self.tick_timer=self.canvas.after(50, self.play_state_machine) def play_state_machine(self): if self.play_state == VideoPlayer._CLOSED: self.mon.log(self," State machine: " + self.play_state) return elif self.play_state == VideoPlayer._STARTING: self.mon.log(self," State machine: " + self.play_state) # if omxplayer is playing the track change to play state if self.omx.start_play_signal==True: self.mon.log(self," <start play signal received from omx") self.omx.start_play_signal=False self.play_state=VideoPlayer._PLAYING self.mon.log(self," State machine: omx_playing started") self.tick_timer=self.canvas.after(50, self.play_state_machine) elif self.play_state == VideoPlayer._PLAYING: # self.mon.log(self," State machine: " + self.play_state) # service any queued stop signals if self.quit_signal==True: self.mon.log(self," Service stop required signal") self.stop_omx() self.quit_signal=False # self.play_state = VideoPlayer._ENDING # omxplayer reports it is terminating so change to ending state if self.omx.end_play_signal: self.mon.log(self," <end play signal received") self.mon.log(self," <end detected at: " + str(self.omx.video_position)) if self.omx.end_play_reason<>'nice_day': # deal with omxplayer not sending 'have a nice day' self.mon.warn(self," <end detected at: " + str(self.omx.video_position)) self.mon.warn(self," <pexpect reports: "+self.omx.end_play_reason) self.mon.warn(self,'pexpect.before is'+self.omx.xbefore) self.play_state = VideoPlayer._ENDING self.ending_count=0 self.tick_timer=self.canvas.after(200, self.play_state_machine) elif self.play_state == VideoPlayer._ENDING: self.mon.log(self," State machine: " + self.play_state) # if spawned process has closed can change to closed state self.mon.log (self," State machine : is omx process running? - " + str(self.omx.is_running())) if self.omx.is_running() ==False: self.mon.log(self," <omx process is dead") self.play_state = VideoPlayer._CLOSED self.end('normal','quit by user or system') else: self.ending_count+=1 if self.ending_count>10: # deal with omxplayer not terminating at the end of a track self.mon.warn(self," <omxplayer failed to close at: " + str(self.omx.video_position)) self.mon.warn(self,'pexpect.before is'+self.omx.xbefore) self.omx.kill() self.mon.warn(self,'omxplayer now terminated ') self.play_state = VideoPlayer._CLOSED self.end('normal','end from omxplayer failed to terminate') else: self.tick_timer=self.canvas.after(200, self.play_state_machine) def stop_omx(self): # send signal to stop the track to the state machine self.mon.log(self," >stop omx received from state machine") if self.play_state==VideoPlayer._PLAYING: self.omx.stop() return True else: self.mon.log(self,"!<stop rejected") return False # ***************** # ending the player # ***************** def end(self,reason,message): # stop the plugin if self.track_params['plugin']<>'': self.pim.stop_plugin() # os.system("xrefresh -display :0") # abort the timer if self.tick_timer<>None: self.canvas.after_cancel(self.tick_timer) self.tick_timer=None if reason in ('error','killed'): self.end_callback(reason,message) self=None else: # normal end so do show control and animation # Control concurrent shows at end reason,message=self.show_manager.show_control(self.track_params['show-control-end']) if reason =='error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: # clear events list for this track if self.track_params['animate-clear']=='yes': self.ppio.clear_events_list(id(self)) # create animation events for ending reason,message=self.ppio.animate(self.animate_end_text,id(self)) if reason=='error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: self.end_callback('normal',"track has terminated or quit") self=None # ***************** # displaying things # ***************** def display_content(self): #background colour if self.background_colour<>'': self.canvas.config(bg=self.background_colour) # delete previous content self.canvas.delete('pp-content') # background image if self.background_file<>'': self.background_img_file = self.complete_path(self.background_file) if not os.path.exists(self.background_img_file): self.mon.err(self,"Video background file not found: "+ self.background_img_file) self.end('error',"Video background file not found") else: pil_background_img=PIL.Image.open(self.background_img_file) self.background = PIL.ImageTk.PhotoImage(pil_background_img) self.drawn = self.canvas.create_image(int(self.canvas['width'])/2, int(self.canvas['height'])/2, image=self.background, anchor=CENTER, tag='pp-content') # execute the plugin if required if self.track_params['plugin']<>'': reason,message,self.track = self.pim.do_plugin(self.track,self.track_params['plugin'],) if reason <> 'normal': return reason,message # display show text if enabled if self.show_params['show-text']<> '' and self.track_params['display-show-text']=='yes': self.canvas.create_text(int(self.show_params['show-text-x']),int(self.show_params['show-text-y']), anchor=NW, text=self.show_params['show-text'], fill=self.show_params['show-text-colour'], font=self.show_params['show-text-font'], tag='pp-content') # display track text if enabled if self.track_params['track-text']<> '': self.canvas.create_text(int(self.track_params['track-text-x']),int(self.track_params['track-text-y']), anchor=NW, text=self.track_params['track-text'], fill=self.track_params['track-text-colour'], font=self.track_params['track-text-font'], tag='pp-content') # display instructions if enabled if self.enable_menu== True: self.canvas.create_text(int(self.show_params['hint-x']), int(self.show_params['hint-y']), text=self.show_params['hint-text'], fill=self.show_params['hint-colour'], font=self.show_params['hint-font'], anchor=NW, tag='pp-content') self.canvas.tag_raise('pp-click-area') self.canvas.update_idletasks( ) return 'normal','' # **************** # utilities # ***************** def complete_path(self,track_file): # complete path of the filename of the selected entry if track_file[0]=="+": track_file=self.pp_home+track_file[1:] self.mon.log(self,"Background image is "+ track_file) return track_file # original _ # warp _ or xy2 def parse_window(self,line): fields = line.split() # check there is a command field if len(fields) < 1: return 'error','no type field','',False,0,0,0,0 # deal with original which has 1 if fields[0]=='original': if len(fields) <> 1: return 'error','number of fields for original','',False,0,0,0,0 return 'normal','',fields[0],False,0,0,0,0 #deal with warp which has 1 or 5 arguments # check basic syntax if fields[0] <>'warp': return 'error','not a valid type','',False,0,0,0,0 if len(fields) not in (1,5): return 'error','wrong number of coordinates for warp','',False,0,0,0,0 # deal with window coordinates if len(fields) == 5: #window is specified if not (fields[1].isdigit() and fields[2].isdigit() and fields[3].isdigit() and fields[4].isdigit()): return 'error','coordinates are not positive integers','',False,0,0,0,0 has_window=True return 'normal','',fields[0],has_window,int(fields[1]),int(fields[2]),int(fields[3]),int(fields[4]) else: # fullscreen has_window=True return 'normal','',fields[0],has_window,0,0,self.canvas['width'],self.canvas['height']
class Player(object): # common bits of __init__(...) def __init__(self, show_id, showlist, root, canvas, show_params, track_params, pp_dir, pp_home, pp_profile, end_callback, command_callback): # create debugging log object self.mon=Monitor() self.mon.trace(self,'') # instantiate arguments self.show_id=show_id self.showlist=showlist self.root=root self.canvas=canvas['canvas-obj'] self.show_canvas_x1 = canvas['show-canvas-x1'] self.show_canvas_y1 = canvas['show-canvas-y1'] self.show_canvas_x2 = canvas['show-canvas-x2'] self.show_canvas_y2 = canvas['show-canvas-y2'] self.show_canvas_width = canvas['show-canvas-width'] self.show_canvas_height= canvas['show-canvas-height'] self.show_canvas_centre_x= canvas['show-canvas-centre-x'] self.show_canvas_centre_y= canvas['show-canvas-centre-y'] self.show_params=show_params self.track_params=track_params self.pp_dir=pp_dir self.pp_home=pp_home self.pp_profile=pp_profile self.end_callback=end_callback self.command_callback=command_callback # get background image from profile. self.background_file='' if self.track_params['background-image'] != '': self.background_file= self.track_params['background-image'] # get background colour from profile. if self.track_params['background-colour'] != '': self.background_colour= self.track_params['background-colour'] else: self.background_colour= self.show_params['background-colour'] # get animation instructions from profile self.animate_begin_text=self.track_params['animate-begin'] self.animate_end_text=self.track_params['animate-end'] # create an instance of showmanager so we can control concurrent shows # self.show_manager=ShowManager(self.show_id,self.showlist,self.show_params,self.root,self.canvas,self.pp_dir,self.pp_profile,self.pp_home) # open the plugin Manager self.pim=PluginManager(self.show_id,self.root,self.canvas,self.show_params,self.track_params,self.pp_dir,self.pp_home,self.pp_profile) # create an instance of Animate so we can send animation commands self.animate = Animate() # initialise state and signals self.background_obj=None self.show_text_obj=None self.track_text_obj=None self.hint_obj=None self.background=None self.freeze_at_end_required='no' # overriden by videoplayer self.tick_timer=None self.terminate_signal=False self.play_state='' def pre_load(self): pass # common bits of show(....) def pre_show(self): self.mon.trace(self,'') # show_x_content moved to just before ready_callback to improve flicker. self.show_x_content() #ready callback hides and closes players from previous track, also displays show background if self.ready_callback is not None: self.ready_callback(self.enable_show_background) # Control other shows and do counters and osc at beginning self.show_control(self.track_params['show-control-begin']) # and show whatever the plugin has created self.show_plugin() # create animation events reason,message=self.animate.animate(self.animate_begin_text,id(self)) if reason == 'error': self.mon.err(self,message) self.play_state='show-failed' if self.finished_callback is not None: self.finished_callback('error',message) else: # return to start playing the track. self.mon.log(self,">show track received from show Id: "+ str(self.show_id)) return # to keep landscape happy def ready_callback(self,enable_show_background): self.mon.fatal(self,'ready callback not overridden') self.end('error','ready callback not overridden') def finished_callback(self,reason,message): self.mon.fatal(self,'finished callback not overridden') self.end('error','finished callback not overridden') def closed_callback(self,reason,message): self.mon.fatal(self,'closed callback not overridden') self.end('error','closed callback not overridden') # Control shows so pass the show control commands back to PiPresents via the command callback def show_control(self,show_control_text): lines = show_control_text.split('\n') for line in lines: if line.strip() == "": continue # print 'show control command: ',line self.command_callback(line, source='track',show=self.show_params['show-ref']) # ***************** # hide content and end animation, show control etc. # called by ready calback and end # ***************** def hide(self): self.mon.trace(self,'') # abort the timer if self.tick_timer is not None: self.canvas.after_cancel(self.tick_timer) self.tick_timer=None self.hide_x_content() # stop the plugin self.hide_plugin() # Control concurrent shows at end self.show_control(self.track_params['show-control-end']) # clear events list for this track if self.track_params['animate-clear'] == 'yes': self.animate.clear_events_list(id(self)) # create animation events for ending # !!!!! TEMPORARY FIX reason,message=self.animate.animate(self.animate_end_text,id(self)) if reason == 'error': self.mon.err(self,message) # self.play_state='show-failed' # if self.finished_callback is not None: # self.finished_callback('error',message) else: return def terminate(self): self.mon.trace(self,'') self.terminate_signal=True if self.play_state == 'showing': # call the derived class's stop method self.stop() else: self.end('killed','terminate with no track or show open') # must be overriden by derived class def stop(self): self.mon.fatal(self,'stop not overidden by derived class') self.play_state='show-failed' if self.finished_callback is not None: self.finished_callback('error','stop not overidden by derived class') def get_play_state(self): return self.play_state # ***************** # ending the player # ***************** def end(self,reason,message): self.mon.trace(self,'') # stop the plugin if self.terminate_signal is True: reason='killed' self.terminate_signal=False self.hide() self.end_callback(reason,message) self=None # ***************** # displaying common things # ***************** def load_plugin(self): # called in load before load_x_content modify the track here if self.track_params['plugin'] != '': reason,message,self.track = self.pim.load_plugin(self.track,self.track_params['plugin']) return reason,message def show_plugin(self): # called at show time, write to the track here if you need it after show_control_begin (counters) if self.track_params['plugin'] != '': self.pim.show_plugin() def hide_plugin(self): # called at the end of the track if self.track_params['plugin'] != '': self.pim.stop_plugin() def load_x_content(self,enable_menu): self.mon.trace(self,'') self.background_obj=None self.background=None self.track_text_obj=None self.show_text_obj=None self.hint_obj=None self.track_obj=None # background image if self.background_file != '': background_img_file = self.complete_path(self.background_file) if not os.path.exists(background_img_file): return 'error',"Track background file not found "+ background_img_file else: try: pil_background_img=Image.open(background_img_file) except: pil_background_img=None self.background=None self.background_obj=None return 'error','Track background, not a recognised image format '+ background_img_file # print 'pil_background_img ',pil_background_img image_width,image_height=pil_background_img.size window_width=self.show_canvas_width window_height=self.show_canvas_height if image_width != window_width or image_height != window_height: pil_background_img=pil_background_img.resize((window_width, window_height)) self.background = ImageTk.PhotoImage(pil_background_img) del pil_background_img self.background_obj = self.canvas.create_image(self.show_canvas_x1, self.show_canvas_y1, image=self.background, anchor=NW) # print '\nloaded background_obj: ',self.background_obj # load the track content. Dummy function below is overridden in players status,message=self.load_track_content() if status == 'error': return 'error',message # load show text if enabled if self.show_params['show-text'] != '' and self.track_params['display-show-text'] == 'yes': x,y,anchor,justify=calculate_text_position(self.show_params['show-text-x'],self.show_params['show-text-y'], self.show_canvas_x1,self.show_canvas_y1, self.show_canvas_centre_x,self.show_canvas_centre_y, self.show_canvas_x2,self.show_canvas_y2,self.show_params['show-text-justify']) self.show_text_obj=self.canvas.create_text(x,y, anchor=anchor, justify=justify, text=self.show_params['show-text'], fill=self.show_params['show-text-colour'], font=self.show_params['show-text-font']) # load track text if enabled if self.track_params['track-text-x'] =='': track_text_x= self.show_params['track-text-x'] else: track_text_x= self.track_params['track-text-x'] if self.track_params['track-text-y'] =='': track_text_y= self.show_params['track-text-y'] else: track_text_y= self.track_params['track-text-y'] if self.track_params['track-text-justify'] =='': track_text_justify= self.show_params['track-text-justify'] else: track_text_justify= self.track_params['track-text-justify'] if self.track_params['track-text-font'] =='': track_text_font= self.show_params['track-text-font'] else: track_text_font= self.track_params['track-text-font'] if self.track_params['track-text-colour'] =='': track_text_colour= self.show_params['track-text-colour'] else: track_text_colour= self.track_params['track-text-colour'] if self.track_params['track-text'] != '': x,y,anchor,justify=calculate_text_position(track_text_x,track_text_y, self.show_canvas_x1,self.show_canvas_y1, self.show_canvas_centre_x,self.show_canvas_centre_y, self.show_canvas_x2,self.show_canvas_y2,track_text_justify) self.track_text_obj=self.canvas.create_text(x,y, anchor=anchor, justify=justify, text=self.track_params['track-text'], fill=track_text_colour, font=track_text_font) # load instructions if enabled if enable_menu is True: x,y,anchor,justify=calculate_text_position(self.show_params['hint-x'],self.show_params['hint-y'], self.show_canvas_x1,self.show_canvas_y1, self.show_canvas_centre_x,self.show_canvas_centre_y, self.show_canvas_x2,self.show_canvas_y2,self.show_params['hint-justify']) self.hint_obj=self.canvas.create_text(x,y, justify=justify, text=self.show_params['hint-text'], fill=self.show_params['hint-colour'], font=self.show_params['hint-font'], anchor=anchor) self.display_show_canvas_rectangle() self.canvas.tag_raise('pp-click-area') self.canvas.itemconfig(self.background_obj,state='hidden') self.canvas.itemconfig(self.show_text_obj,state='hidden') self.canvas.itemconfig(self.track_text_obj,state='hidden') self.canvas.itemconfig(self.hint_obj,state='hidden') self.canvas.update_idletasks( ) return 'normal','x-content loaded' # display the rectangle that is the show canvas def display_show_canvas_rectangle(self): # coords=[self.show_canvas_x1,self.show_canvas_y1,self.show_canvas_x2-1,self.show_canvas_y2-1] # self.canvas.create_rectangle(coords, # outline='yellow', # fill='') pass # dummy functions to manipulate the track content, overidden in some players, # message text in messageplayer # image in imageplayer # menu stuff in menuplayer def load_track_content(self): return 'normal','player has no track content to load' def show_track_content(self): pass def hide_track_content(self): pass def show_x_content(self): self.mon.trace(self,'') # background colour if self.background_colour != '': self.canvas.config(bg=self.background_colour) # print 'showing background_obj: ', self.background_obj # reveal background image and text self.canvas.itemconfig(self.background_obj,state='normal') self.show_track_content() self.canvas.itemconfig(self.show_text_obj,state='normal') self.canvas.itemconfig(self.track_text_obj,state='normal') self.canvas.itemconfig(self.hint_obj,state='normal') # self.canvas.update_idletasks( ) # decide whether the show background should be enabled. # print 'DISPLAY SHOW BG',self.track_params['display-show-background'],self.background_obj if self.background_obj is None and self.track_params['display-show-background']=='yes': self.enable_show_background=True else: self.enable_show_background=False # print 'ENABLE SB',self.enable_show_background def hide_x_content(self): self.mon.trace(self,'') self.hide_track_content() self.canvas.itemconfig(self.background_obj,state='hidden') self.canvas.itemconfig(self.show_text_obj,state='hidden') self.canvas.itemconfig(self.track_text_obj,state='hidden') self.canvas.itemconfig(self.hint_obj,state='hidden') # self.canvas.update_idletasks( ) self.canvas.delete(self.background_obj) self.canvas.delete(self.show_text_obj) self.canvas.delete(self.track_text_obj) self.canvas.delete(self.hint_obj) self.background=None # self.canvas.update_idletasks( ) # **************** # utilities # ***************** def get_links(self): return self.track_params['links'] # produce an absolute path from the relative one in track paramters def complete_path(self,track_file): # complete path of the filename of the selected entry if track_file[0] == "+": track_file=self.pp_home+track_file[1:] elif track_file[0] == "@": track_file=self.pp_profile+track_file[1:] return track_file # get a text string from resources.cfg def resource(self,section,item): value=self.rr.get(section,item) return value # False if not found
def __init__(self, show_id, root, canvas, show_params, track_params , pp_dir, pp_home, pp_profile): self.mon=Monitor() self.mon.on() #instantiate arguments self.show_id=show_id self.root=root self.canvas = canvas self.show_params=show_params self.track_params=track_params self.pp_dir=pp_dir self.pp_home=pp_home self.pp_profile=pp_profile # get config from medialist if there. if self.track_params['omx-audio']<>"": self.omx_audio= self.track_params['omx-audio'] else: self.omx_audio= self.show_params['omx-audio'] if self.omx_audio<>"": self.omx_audio= "-o "+ self.omx_audio if self.track_params['omx-volume']<>"": self.omx_volume= self.track_params['omx-volume'] else: self.omx_volume= self.show_params['omx-volume'] if self.omx_volume<>"": self.omx_volume= "--vol "+ str(int(self.omx_volume)*100) + ' ' if self.track_params['omx-window']<>'': self.omx_window= self.track_params['omx-window'] else: self.omx_window= self.show_params['omx-window'] # get background image from profile. self.background_file='' if self.track_params['background-image']<>"": self.background_file= self.track_params['background-image'] else: if self.track_params['display-show-background']=='yes': self.background_file= self.show_params['background-image'] # get background colour from profile. if self.track_params['background-colour']<>"": self.background_colour= self.track_params['background-colour'] else: self.background_colour= self.show_params['background-colour'] self.centre_x = int(self.canvas['width'])/2 self.centre_y = int(self.canvas['height'])/2 #get animation instructions from profile self.animate_begin_text=self.track_params['animate-begin'] self.animate_end_text=self.track_params['animate-end'] # open the plugin Manager self.pim=PluginManager(self.show_id,self.root,self.canvas,self.show_params,self.track_params,self.pp_dir,self.pp_home,self.pp_profile) #create an instance of PPIO so we can create gpio events self.ppio = PPIO() # could put instance generation in play, not sure which is better. self.omx=OMXDriver(self.canvas) self.tick_timer=None self.init_play_state_machine()
class AudioPlayer: """ plays an audio track using mplayer against a coloured backgroud and image track can be paused and interrupted See pp_imageplayer for common software design description """ #state constants _CLOSED = "mplayer_closed" #probably will not exist _STARTING = "mplayer_starting" #track is being prepared _PLAYING = "mplayer_playing" #track is playing to the screen, may be paused _ENDING = "mplayer_ending" #track is in the process of ending due to quit or end of track _WAITING = "wait for timeout" # track has finished but timeout still running # audio mixer matrix settings _LEFT = "channels=2:1:0:0:1:1" _RIGHT = "channels=2:1:0:1:1:0" _STEREO = "channels=2" # *************************************** # EXTERNAL COMMANDS # *************************************** def __init__(self, show_id, root, canvas, show_params, track_params, pp_dir, pp_home, pp_profile): self.mon = Monitor() self.mon.off() #instantiate arguments self.show_id = show_id self.root = root self.canvas = canvas self.show_params = show_params self.track_params = track_params self.pp_dir = pp_dir self.pp_home = pp_home self.pp_profile = pp_profile # get duration limit (secs ) from profile if self.track_params['duration'] <> '': self.duration = int(self.track_params['duration']) self.duration_limit = 20 * self.duration else: self.duration_limit = -1 # get background image from profile. self.background_file = '' if self.track_params['background-image'] <> "": self.background_file = self.track_params['background-image'] else: if self.track_params['display-show-background'] == 'yes': self.background_file = self.show_params['background-image'] # get background colour from profile. if self.track_params['background-colour'] <> "": self.background_colour = self.track_params['background-colour'] else: self.background_colour = self.show_params['background-colour'] # get audio device from profile. if self.track_params['mplayer-audio'] <> "": self.mplayer_audio = self.track_params['mplayer-audio'] else: self.mplayer_audio = self.show_params['mplayer-audio'] # get audio volume from profile. if self.track_params['mplayer-volume'] <> "": self.mplayer_volume = self.track_params['mplayer-volume'].strip() else: self.mplayer_volume = self.show_params['mplayer-volume'].strip() self.volume_option = 'volume=' + self.mplayer_volume #get speaker from profile if self.track_params['audio-speaker'] <> "": self.audio_speaker = self.track_params['audio-speaker'] else: self.audio_speaker = self.show_params['audio-speaker'] if self.audio_speaker == 'left': self.speaker_option = AudioPlayer._LEFT elif self.audio_speaker == 'right': self.speaker_option = AudioPlayer._RIGHT else: self.speaker_option = AudioPlayer._STEREO #get animation instructions from profile self.animate_begin_text = self.track_params['animate-begin'] self.animate_end_text = self.track_params['animate-end'] # open the plugin Manager self.pim = PluginManager(self.show_id, self.root, self.canvas, self.show_params, self.track_params, self.pp_dir, self.pp_home, self.pp_profile) #create an instance of PPIO so we can create gpio events self.ppio = PPIO() # could put instance generation in play, not sure which is better. self.mplayer = mplayerDriver(self.canvas) self.tick_timer = None self.init_play_state_machine() def play(self, track, showlist, end_callback, ready_callback, enable_menu=False): #instantiate arguments self.track = track self.showlist = showlist self.end_callback = end_callback # callback when finished self.ready_callback = ready_callback #callback when ready to play self.enable_menu = enable_menu # select the sound device if self.mplayer_audio <> "": if self.mplayer_audio == 'hdmi': os.system("amixer -q -c 0 cset numid=3 2") else: os.system("amixer -q -c 0 cset numid=3 1") # callback to the calling object to e.g remove egg timer. if self.ready_callback <> None: self.ready_callback() # create an instance of showmanager so we can control concurrent shows self.show_manager = ShowManager(self.show_id, self.showlist, self.show_params, self.root, self.canvas, self.pp_dir, self.pp_profile, self.pp_home) # Control other shows at beginning reason, message = self.show_manager.show_control( self.track_params['show-control-begin']) if reason == 'error': self.mon.err(self, message) self.end_callback(reason, message) self = None else: # display image and text reason, message = self.display_content() if reason == 'error': self.mon.err(self, message) self.end_callback(reason, message) self = None else: # create animation events reason, message = self.ppio.animate(self.animate_begin_text, id(self)) if reason == 'error': self.mon.err(self, message) self.end_callback(reason, message) self = None else: # start playing the track. if self.duration_limit <> 0: self.start_play_state_machine() else: self.tick_timer = self.canvas.after(10, self.end_zero) def end_zero(self): self.end('normal', 'zero duration') def terminate(self, reason): """ terminate the player in special circumstances normal user termination if by key_pressed 'stop' reason will be killed or error """ # circumvents state machine to terminate lower level and then itself. if self.mplayer <> None: self.mon.log(self, "sent terminate to mplayerdriver") self.mplayer.terminate(reason) self.end('killed', ' end without waiting for mplayer to finish' ) # end without waiting else: self.mon.log(self, "terminate, mplayerdriver not running") self.end('killed', 'terminate, mplayerdriver not running') def get_links(self): return self.track_params['links'] def input_pressed(self, symbol): if symbol[0:6] == 'mplay-': self.control(symbol[6]) elif symbol == 'pause': self.pause() elif symbol == 'stop': self.stop() # *************************************** # INTERNAL FUNCTIONS # *************************************** #toggle pause def pause(self): if self.play_state in (AudioPlayer._PLAYING, AudioPlayer._ENDING) and self.track <> '': self.mplayer.pause() return True else: self.mon.log(self, "!<pause rejected") return False # other control when playing, not currently used def control(self, char): if self.play_state == AudioPlayer._PLAYING and self.track <> '' and char not in ( 'q'): self.mon.log(self, "> send control to mplayer: " + char) self.mplayer.control(char) return True else: self.mon.log(self, "!<control rejected") return False # respond to normal stop def stop(self): # send signal to stop the track to the state machine self.mon.log(self, ">stop received") self.quit_signal = True # *************************************** # sequencing # *************************************** """self. play_state controls the playing sequence, it has the following values. I am not entirely sure the starting and ending states are required. - _closed - the mplayer process is not running, mplayer process can be initiated - _starting - mplayer process is running but is not yet able to receive controls - _playing - playing a track, controls can be sent - _ending - mplayer is doing its termination, controls cannot be sent """ def init_play_state_machine(self): self.quit_signal = False self.play_state = AudioPlayer._CLOSED def start_play_state_machine(self): #initialise all the state machine variables self.duration_count = 0 self.quit_signal = False # signal that user has pressed stop #play the track options = self.show_params[ 'mplayer-other-options'] + '-af ' + self.speaker_option + ',' + self.volume_option + ' ' if self.track <> '': self.mplayer.play(self.track, options) self.mon.log(self, 'Playing track from show Id: ' + str(self.show_id)) self.play_state = AudioPlayer._STARTING else: self.play_state = AudioPlayer._PLAYING # and start polling for state changes and count duration self.tick_timer = self.canvas.after(50, self.play_state_machine) def play_state_machine(self): self.duration_count += 1 if self.play_state == AudioPlayer._CLOSED: self.mon.log(self, " State machine: " + self.play_state) return elif self.play_state == AudioPlayer._STARTING: self.mon.log(self, " State machine: " + self.play_state) # if mplayer is playing the track change to play state if self.mplayer.start_play_signal == True: self.mon.log( self, " <start play signal received from mplayer") self.mplayer.start_play_signal = False self.play_state = AudioPlayer._PLAYING self.mon.log(self, " State machine: mplayer_playing started") self.tick_timer = self.canvas.after(50, self.play_state_machine) elif self.play_state == AudioPlayer._PLAYING: # self.mon.log(self," State machine: " + self.play_state) # service any queued stop signals if self.quit_signal == True or ( self.duration_limit > 0 and self.duration_count > self.duration_limit): self.mon.log(self, " Service stop required signal or timeout") # self.quit_signal=False if self.track <> '': self.stop_mplayer() self.play_state = AudioPlayer._ENDING else: self.play_state = AudioPlayer._CLOSED self.end('normal', 'stop required signal or timeout') # mplayer reports it is terminating so change to ending state if self.track <> '' and self.mplayer.end_play_signal: self.mon.log(self, " <end play signal received") self.mon.log( self, " <end detected at: " + str(self.mplayer.audio_position)) self.play_state = AudioPlayer._ENDING self.tick_timer = self.canvas.after(50, self.play_state_machine) elif self.play_state == AudioPlayer._ENDING: # self.mon.log(self," State machine: " + self.play_state) # if spawned process has closed can change to closed state # self.mon.log (self," State machine : is mplayer process running? - " + str(self.mplayer.is_running())) if self.mplayer.is_running() == False: self.mon.log(self, " <mplayer process is dead") if self.quit_signal == True: self.quit_signal = False self.play_state = AudioPlayer._CLOSED self.end('normal', 'quit required or timeout') elif self.duration_limit > 0 and self.duration_count < self.duration_limit: self.play_state = AudioPlayer._WAITING self.tick_timer = self.canvas.after( 50, self.play_state_machine) else: self.play_state = AudioPlayer._CLOSED self.end('normal', 'mplayer dead') else: self.tick_timer = self.canvas.after(50, self.play_state_machine) elif self.play_state == AudioPlayer._WAITING: # self.mon.log(self," State machine: " + self.play_state) if self.quit_signal == True or ( self.duration_limit > 0 and self.duration_count > self.duration_limit): self.mon.log( self, " Service stop required signal or timeout from wait") self.quit_signal = False self.play_state = AudioPlayer._CLOSED self.end('normal', 'mplayer dead') else: self.tick_timer = self.canvas.after(50, self.play_state_machine) def stop_mplayer(self): # send signal to stop the track to the state machine self.mon.log(self, " >stop mplayer received from state machine") if self.play_state == AudioPlayer._PLAYING: self.mplayer.stop() return True else: self.mon.log(self, "!<stop rejected") return False # ***************** # ending the player # ***************** def end(self, reason, message): # stop the plugin if self.track_params['plugin'] <> '': self.pim.stop_plugin() # abort the timer if self.tick_timer <> None: self.canvas.after_cancel(self.tick_timer) self.tick_timer = None if reason in ('error', 'killed'): self.end_callback(reason, message) self = None else: # normal end so do show control and animation # Control concurrent shows at end reason, message = self.show_manager.show_control( self.track_params['show-control-end']) if reason == 'error': self.mon.err(self, message) self.end_callback(reason, message) self = None else: # clear events list for this track if self.track_params['animate-clear'] == 'yes': self.ppio.clear_events_list(id(self)) # create animation events for ending reason, message = self.ppio.animate(self.animate_end_text, id(self)) if reason == 'error': self.mon.err(self, message) self.end_callback(reason, message) self = None else: self.end_callback('normal', "track has terminated or quit") self = None # ***************** # displaying things # ***************** def display_content(self): #if self.background_file<>'' or self.show_params['show-text']<> '' or #self.track_params['track-text']<> '' or self.enable_menu== True or #self.track_params['clear-screen']=='yes': if self.track_params['clear-screen'] == 'yes': self.canvas.delete('pp-content') # self.canvas.update() #background colour if self.background_colour <> '': self.canvas.config(bg=self.background_colour) if self.background_file <> '': self.background_img_file = self.complete_path(self.background_file) if not os.path.exists(self.background_img_file): self.mon.err( self, "Audio background file not found: " + self.background_img_file) self.end('error', "Audio background file not found") else: pil_background_img = PIL.Image.open(self.background_img_file) self.background = PIL.ImageTk.PhotoImage(pil_background_img) self.drawn = self.canvas.create_image( int(self.canvas['width']) / 2, int(self.canvas['height']) / 2, image=self.background, anchor=CENTER, tag='pp-content') # execute the plugin if required if self.track_params['plugin'] <> '': reason, message, self.track = self.pim.do_plugin( self.track, self.track_params['plugin'], ) if reason <> 'normal': return reason, message # display hint text if enabled if self.enable_menu == True: self.canvas.create_text(int(self.show_params['hint-x']), int(self.show_params['hint-y']), text=self.show_params['hint-text'], fill=self.show_params['hint-colour'], font=self.show_params['hint-font'], anchor=NW, tag='pp-content') # display show text if enabled if self.show_params['show-text'] <> '' and self.track_params[ 'display-show-text'] == 'yes': self.canvas.create_text(int(self.show_params['show-text-x']), int(self.show_params['show-text-y']), anchor=NW, text=self.show_params['show-text'], fill=self.show_params['show-text-colour'], font=self.show_params['show-text-font'], tag='pp-content') # display track text if enabled if self.track_params['track-text'] <> '': self.canvas.create_text( int(self.track_params['track-text-x']), int(self.track_params['track-text-y']), anchor=NW, text=self.track_params['track-text'], fill=self.track_params['track-text-colour'], font=self.track_params['track-text-font'], tag='pp-content') self.mon.log(self, "Displayed background and text ") self.canvas.tag_raise('pp-click-area') self.canvas.update_idletasks() return 'normal', '' # ***************** # utilities # ***************** def complete_path(self, track_file): # complete path of the filename of the selected entry if track_file[0] == "+": track_file = self.pp_home + track_file[1:] self.mon.log(self, "Background image is " + track_file) return track_file
def __init__(self, show_id, root, canvas, show_params, track_params, pp_dir, pp_home, pp_profile): self.mon=Monitor() self.mon.on() #instantiate arguments self.show_id=show_id self.root=root, self.canvas = canvas self.show_params=show_params self.track_params=track_params self.pp_dir=pp_dir self.pp_home=pp_home self.pp_profile=pp_profile # get duration limit (secs ) from profile if self.track_params['duration']<>"": self.duration= int(self.track_params['duration']) else: self.duration= int(self.show_params['duration']) self.duration_limit=20*self.duration # get background image from profile. self.background_file='' if self.track_params['background-image']<>"": self.background_file= self.track_params['background-image'] else: if self.track_params['display-show-background']=='yes': self.background_file= self.show_params['background-image'] # get background colour from profile. if self.track_params['background-colour']<>"": self.background_colour= self.track_params['background-colour'] else: self.background_colour= self.show_params['background-colour'] #get animation instructions from profile self.animate_begin_text=self.track_params['animate-begin'] self.animate_end_text=self.track_params['animate-end'] # open the plugin Manager self.pim=PluginManager(self.show_id,self.root,self.canvas,self.show_params,self.track_params,self.pp_dir,self.pp_home,self.pp_profile) #create an instance of PPIO so we can create gpio events self.ppio = PPIO() # could put instance generation in play, not sure which is better. self.bplayer=uzblDriver(self.canvas) self.command_timer=None self.tick_timer=None self.init_play_state_machine()
def __init__(self, show_id, root, canvas, show_params, track_params, pp_dir, pp_home, pp_profile): self.mon = Monitor() self.mon.off() #instantiate arguments self.show_id = show_id self.root = root self.canvas = canvas self.show_params = show_params self.track_params = track_params self.pp_dir = pp_dir self.pp_home = pp_home self.pp_profile = pp_profile # get duration limit (secs ) from profile if self.track_params['duration'] <> '': self.duration = int(self.track_params['duration']) self.duration_limit = 20 * self.duration else: self.duration_limit = -1 # get background image from profile. self.background_file = '' if self.track_params['background-image'] <> "": self.background_file = self.track_params['background-image'] else: if self.track_params['display-show-background'] == 'yes': self.background_file = self.show_params['background-image'] # get background colour from profile. if self.track_params['background-colour'] <> "": self.background_colour = self.track_params['background-colour'] else: self.background_colour = self.show_params['background-colour'] # get audio device from profile. if self.track_params['mplayer-audio'] <> "": self.mplayer_audio = self.track_params['mplayer-audio'] else: self.mplayer_audio = self.show_params['mplayer-audio'] # get audio volume from profile. if self.track_params['mplayer-volume'] <> "": self.mplayer_volume = self.track_params['mplayer-volume'].strip() else: self.mplayer_volume = self.show_params['mplayer-volume'].strip() self.volume_option = 'volume=' + self.mplayer_volume #get speaker from profile if self.track_params['audio-speaker'] <> "": self.audio_speaker = self.track_params['audio-speaker'] else: self.audio_speaker = self.show_params['audio-speaker'] if self.audio_speaker == 'left': self.speaker_option = AudioPlayer._LEFT elif self.audio_speaker == 'right': self.speaker_option = AudioPlayer._RIGHT else: self.speaker_option = AudioPlayer._STEREO #get animation instructions from profile self.animate_begin_text = self.track_params['animate-begin'] self.animate_end_text = self.track_params['animate-end'] # open the plugin Manager self.pim = PluginManager(self.show_id, self.root, self.canvas, self.show_params, self.track_params, self.pp_dir, self.pp_home, self.pp_profile) #create an instance of PPIO so we can create gpio events self.ppio = PPIO() # could put instance generation in play, not sure which is better. self.mplayer = mplayerDriver(self.canvas) self.tick_timer = None self.init_play_state_machine()
class AudioPlayer: """ plays an audio track using mplayer against a coloured backgroud and image track can be paused and interrupted See pp_imageplayer for common software design description """ # state constants _CLOSED = "mplayer_closed" # probably will not exist _STARTING = "mplayer_starting" # track is being prepared _PLAYING = "mplayer_playing" # track is playing to the screen, may be paused _ENDING = "mplayer_ending" # track is in the process of ending due to quit or end of track _WAITING = "wait for timeout" # track has finished but timeout still running # audio mixer matrix settings _LEFT = "channels=2:1:0:0:1:1" _RIGHT = "channels=2:1:0:1:1:0" _STEREO = "channels=2" # *************************************** # EXTERNAL COMMANDS # *************************************** def __init__(self, show_id, root, canvas, show_params, track_params, pp_dir, pp_home, pp_profile): self.mon = Monitor() self.mon.off() # instantiate arguments self.show_id = show_id self.root = root self.canvas = canvas self.show_params = show_params self.track_params = track_params self.pp_dir = pp_dir self.pp_home = pp_home self.pp_profile = pp_profile # get duration limit (secs ) from profile if self.track_params["duration"] <> "": self.duration = int(self.track_params["duration"]) self.duration_limit = 20 * self.duration else: self.duration_limit = -1 # get background image from profile. self.background_file = "" if self.track_params["background-image"] <> "": self.background_file = self.track_params["background-image"] else: if self.track_params["display-show-background"] == "yes": self.background_file = self.show_params["background-image"] # get background colour from profile. if self.track_params["background-colour"] <> "": self.background_colour = self.track_params["background-colour"] else: self.background_colour = self.show_params["background-colour"] # get audio device from profile. if self.track_params["mplayer-audio"] <> "": self.mplayer_audio = self.track_params["mplayer-audio"] else: self.mplayer_audio = self.show_params["mplayer-audio"] # get audio volume from profile. if self.track_params["mplayer-volume"] <> "": self.mplayer_volume = self.track_params["mplayer-volume"].strip() else: self.mplayer_volume = self.show_params["mplayer-volume"].strip() self.volume_option = "volume=" + self.mplayer_volume # get speaker from profile if self.track_params["audio-speaker"] <> "": self.audio_speaker = self.track_params["audio-speaker"] else: self.audio_speaker = self.show_params["audio-speaker"] if self.audio_speaker == "left": self.speaker_option = AudioPlayer._LEFT elif self.audio_speaker == "right": self.speaker_option = AudioPlayer._RIGHT else: self.speaker_option = AudioPlayer._STEREO # get animation instructions from profile self.animate_begin_text = self.track_params["animate-begin"] self.animate_end_text = self.track_params["animate-end"] # open the plugin Manager self.pim = PluginManager( self.show_id, self.root, self.canvas, self.show_params, self.track_params, self.pp_dir, self.pp_home, self.pp_profile, ) # create an instance of PPIO so we can create gpio events self.ppio = PPIO() # could put instance generation in play, not sure which is better. self.mplayer = mplayerDriver(self.canvas) self.tick_timer = None self.init_play_state_machine() def play(self, track, showlist, end_callback, ready_callback, enable_menu=False): # instantiate arguments self.track = track self.showlist = showlist self.end_callback = end_callback # callback when finished self.ready_callback = ready_callback # callback when ready to play self.enable_menu = enable_menu # select the sound device if self.mplayer_audio <> "": if self.mplayer_audio == "hdmi": os.system("amixer -q -c 0 cset numid=3 2") else: os.system("amixer -q -c 0 cset numid=3 1") # callback to the calling object to e.g remove egg timer. if self.ready_callback <> None: self.ready_callback() # create an instance of showmanager so we can control concurrent shows self.show_manager = ShowManager( self.show_id, self.showlist, self.show_params, self.root, self.canvas, self.pp_dir, self.pp_profile, self.pp_home, ) # Control other shows at beginning reason, message = self.show_manager.show_control(self.track_params["show-control-begin"]) if reason == "error": self.mon.err(self, message) self.end_callback(reason, message) self = None else: # display image and text reason, message = self.display_content() if reason == "error": self.mon.err(self, message) self.end_callback(reason, message) self = None else: # create animation events reason, message = self.ppio.animate(self.animate_begin_text, id(self)) if reason == "error": self.mon.err(self, message) self.end_callback(reason, message) self = None else: # start playing the track. if self.duration_limit <> 0: self.start_play_state_machine() else: self.tick_timer = self.canvas.after(10, self.end_zero) def end_zero(self): self.end("normal", "zero duration") def terminate(self, reason): """ terminate the player in special circumstances normal user termination if by key_pressed 'stop' reason will be killed or error """ # circumvents state machine to terminate lower level and then itself. if self.mplayer <> None: self.mon.log(self, "sent terminate to mplayerdriver") self.mplayer.terminate(reason) self.end("killed", " end without waiting for mplayer to finish") # end without waiting else: self.mon.log(self, "terminate, mplayerdriver not running") self.end("killed", "terminate, mplayerdriver not running") def get_links(self): return self.track_params["links"] def input_pressed(self, symbol): if symbol[0:6] == "mplay-": self.control(symbol[6]) elif symbol == "pause": self.pause() elif symbol == "stop": self.stop() # *************************************** # INTERNAL FUNCTIONS # *************************************** # toggle pause def pause(self): if self.play_state in (AudioPlayer._PLAYING, AudioPlayer._ENDING) and self.track <> "": self.mplayer.pause() return True else: self.mon.log(self, "!<pause rejected") return False # other control when playing, not currently used def control(self, char): if self.play_state == AudioPlayer._PLAYING and self.track <> "" and char not in ("q"): self.mon.log(self, "> send control to mplayer: " + char) self.mplayer.control(char) return True else: self.mon.log(self, "!<control rejected") return False # respond to normal stop def stop(self): # send signal to stop the track to the state machine self.mon.log(self, ">stop received") self.quit_signal = True # *************************************** # sequencing # *************************************** """self. play_state controls the playing sequence, it has the following values. I am not entirely sure the starting and ending states are required. - _closed - the mplayer process is not running, mplayer process can be initiated - _starting - mplayer process is running but is not yet able to receive controls - _playing - playing a track, controls can be sent - _ending - mplayer is doing its termination, controls cannot be sent """ def init_play_state_machine(self): self.quit_signal = False self.play_state = AudioPlayer._CLOSED def start_play_state_machine(self): # initialise all the state machine variables self.duration_count = 0 self.quit_signal = False # signal that user has pressed stop # play the track options = ( self.show_params["mplayer-other-options"] + "-af " + self.speaker_option + "," + self.volume_option + " " ) if self.track <> "": self.mplayer.play(self.track, options) self.mon.log(self, "Playing track from show Id: " + str(self.show_id)) self.play_state = AudioPlayer._STARTING else: self.play_state = AudioPlayer._PLAYING # and start polling for state changes and count duration self.tick_timer = self.canvas.after(50, self.play_state_machine) def play_state_machine(self): self.duration_count += 1 if self.play_state == AudioPlayer._CLOSED: self.mon.log(self, " State machine: " + self.play_state) return elif self.play_state == AudioPlayer._STARTING: self.mon.log(self, " State machine: " + self.play_state) # if mplayer is playing the track change to play state if self.mplayer.start_play_signal == True: self.mon.log(self, " <start play signal received from mplayer") self.mplayer.start_play_signal = False self.play_state = AudioPlayer._PLAYING self.mon.log(self, " State machine: mplayer_playing started") self.tick_timer = self.canvas.after(50, self.play_state_machine) elif self.play_state == AudioPlayer._PLAYING: # self.mon.log(self," State machine: " + self.play_state) # service any queued stop signals if self.quit_signal == True or (self.duration_limit > 0 and self.duration_count > self.duration_limit): self.mon.log(self, " Service stop required signal or timeout") # self.quit_signal=False if self.track <> "": self.stop_mplayer() self.play_state = AudioPlayer._ENDING else: self.play_state = AudioPlayer._CLOSED self.end("normal", "stop required signal or timeout") # mplayer reports it is terminating so change to ending state if self.track <> "" and self.mplayer.end_play_signal: self.mon.log(self, " <end play signal received") self.mon.log(self, " <end detected at: " + str(self.mplayer.audio_position)) self.play_state = AudioPlayer._ENDING self.tick_timer = self.canvas.after(50, self.play_state_machine) elif self.play_state == AudioPlayer._ENDING: # self.mon.log(self," State machine: " + self.play_state) # if spawned process has closed can change to closed state # self.mon.log (self," State machine : is mplayer process running? - " + str(self.mplayer.is_running())) if self.mplayer.is_running() == False: self.mon.log(self, " <mplayer process is dead") if self.quit_signal == True: self.quit_signal = False self.play_state = AudioPlayer._CLOSED self.end("normal", "quit required or timeout") elif self.duration_limit > 0 and self.duration_count < self.duration_limit: self.play_state = AudioPlayer._WAITING self.tick_timer = self.canvas.after(50, self.play_state_machine) else: self.play_state = AudioPlayer._CLOSED self.end("normal", "mplayer dead") else: self.tick_timer = self.canvas.after(50, self.play_state_machine) elif self.play_state == AudioPlayer._WAITING: # self.mon.log(self," State machine: " + self.play_state) if self.quit_signal == True or (self.duration_limit > 0 and self.duration_count > self.duration_limit): self.mon.log(self, " Service stop required signal or timeout from wait") self.quit_signal = False self.play_state = AudioPlayer._CLOSED self.end("normal", "mplayer dead") else: self.tick_timer = self.canvas.after(50, self.play_state_machine) def stop_mplayer(self): # send signal to stop the track to the state machine self.mon.log(self, " >stop mplayer received from state machine") if self.play_state == AudioPlayer._PLAYING: self.mplayer.stop() return True else: self.mon.log(self, "!<stop rejected") return False # ***************** # ending the player # ***************** def end(self, reason, message): # stop the plugin if self.track_params["plugin"] <> "": self.pim.stop_plugin() # abort the timer if self.tick_timer <> None: self.canvas.after_cancel(self.tick_timer) self.tick_timer = None if reason in ("error", "killed"): self.end_callback(reason, message) self = None else: # normal end so do show control and animation # Control concurrent shows at end reason, message = self.show_manager.show_control(self.track_params["show-control-end"]) if reason == "error": self.mon.err(self, message) self.end_callback(reason, message) self = None else: # clear events list for this track if self.track_params["animate-clear"] == "yes": self.ppio.clear_events_list(id(self)) # create animation events for ending reason, message = self.ppio.animate(self.animate_end_text, id(self)) if reason == "error": self.mon.err(self, message) self.end_callback(reason, message) self = None else: self.end_callback("normal", "track has terminated or quit") self = None # ***************** # displaying things # ***************** def display_content(self): # if self.background_file<>'' or self.show_params['show-text']<> '' or #self.track_params['track-text']<> '' or self.enable_menu== True or #self.track_params['clear-screen']=='yes': if self.track_params["clear-screen"] == "yes": self.canvas.delete("pp-content") # self.canvas.update() # background colour if self.background_colour <> "": self.canvas.config(bg=self.background_colour) if self.background_file <> "": self.background_img_file = self.complete_path(self.background_file) if not os.path.exists(self.background_img_file): self.mon.err(self, "Audio background file not found: " + self.background_img_file) self.end("error", "Audio background file not found") else: pil_background_img = PIL.Image.open(self.background_img_file) self.background = PIL.ImageTk.PhotoImage(pil_background_img) self.drawn = self.canvas.create_image( int(self.canvas["width"]) / 2, int(self.canvas["height"]) / 2, image=self.background, anchor=CENTER, tag="pp-content", ) # execute the plugin if required if self.track_params["plugin"] <> "": reason, message, self.track = self.pim.do_plugin(self.track, self.track_params["plugin"]) if reason <> "normal": return reason, message # display hint text if enabled if self.enable_menu == True: self.canvas.create_text( int(self.show_params["hint-x"]), int(self.show_params["hint-y"]), text=self.show_params["hint-text"], fill=self.show_params["hint-colour"], font=self.show_params["hint-font"], anchor=NW, tag="pp-content", ) # display show text if enabled if self.show_params["show-text"] <> "" and self.track_params["display-show-text"] == "yes": self.canvas.create_text( int(self.show_params["show-text-x"]), int(self.show_params["show-text-y"]), anchor=NW, text=self.show_params["show-text"], fill=self.show_params["show-text-colour"], font=self.show_params["show-text-font"], tag="pp-content", ) # display track text if enabled if self.track_params["track-text"] <> "": self.canvas.create_text( int(self.track_params["track-text-x"]), int(self.track_params["track-text-y"]), anchor=NW, text=self.track_params["track-text"], fill=self.track_params["track-text-colour"], font=self.track_params["track-text-font"], tag="pp-content", ) self.mon.log(self, "Displayed background and text ") self.canvas.tag_raise("pp-click-area") self.canvas.update_idletasks() return "normal", "" # ***************** # utilities # ***************** def complete_path(self, track_file): # complete path of the filename of the selected entry if track_file[0] == "+": track_file = self.pp_home + track_file[1:] self.mon.log(self, "Background image is " + track_file) return track_file
def __init__( self, show_id, showlist, root, canvas, show_params, track_params, pp_dir, pp_home, pp_profile, end_callback, command_callback, ): # create debugging log object self.mon = Monitor() self.mon.trace(self, "") # instantiate arguments self.show_id = show_id self.showlist = showlist self.root = root self.canvas = canvas["canvas-obj"] self.show_canvas_x1 = canvas["show-canvas-x1"] self.show_canvas_y1 = canvas["show-canvas-y1"] self.show_canvas_x2 = canvas["show-canvas-x2"] self.show_canvas_y2 = canvas["show-canvas-y2"] self.show_canvas_width = canvas["show-canvas-width"] self.show_canvas_height = canvas["show-canvas-height"] self.show_canvas_centre_x = canvas["show-canvas-centre-x"] self.show_canvas_centre_y = canvas["show-canvas-centre-y"] self.show_params = show_params self.track_params = track_params self.pp_dir = pp_dir self.pp_home = pp_home self.pp_profile = pp_profile self.end_callback = end_callback self.command_callback = command_callback # get background image from profile. self.background_file = "" if self.track_params["background-image"] != "": self.background_file = self.track_params["background-image"] # get background colour from profile. if self.track_params["background-colour"] != "": self.background_colour = self.track_params["background-colour"] else: self.background_colour = self.show_params["background-colour"] # get animation instructions from profile self.animate_begin_text = self.track_params["animate-begin"] self.animate_end_text = self.track_params["animate-end"] # create an instance of showmanager so we can control concurrent shows # self.show_manager=ShowManager(self.show_id,self.showlist,self.show_params,self.root,self.canvas,self.pp_dir,self.pp_profile,self.pp_home) # open the plugin Manager self.pim = PluginManager( self.show_id, self.root, self.canvas, self.show_params, self.track_params, self.pp_dir, self.pp_home, self.pp_profile, ) # create an instance of Animate so we can send animation commands self.animate = Animate() # initialise state and signals self.background_obj = None self.show_text_obj = None self.track_text_obj = None self.hint_obj = None self.background = None self.freeze_at_end_required = "no" # overriden by videoplayer self.tick_timer = None self.terminate_signal = False self.play_state = ""
class ImagePlayer: """ Displays an image on a canvas for a period of time. Image display can be paused and interrupted __init_ just makes sure that all the things the player needs are available play starts playing the track and returns immeadiately play must end up with a call to tkinter's after, the after callback will interrogae the playing state at intervals and eventually return through end_callback input-pressed receives user input while the track is playing. it might pass the input on to the driver Input-pressed must not wait, it must set a signal and return immeadiately. The signal is interrogated by the after callback. """ # slide state constants NO_SLIDE = 0 SLIDE_DWELL= 1 # ******************* # external commands # ******************* def __init__(self,show_id,root,canvas,show_params,track_params,pp_dir,pp_home,pp_profile): """ show_id - show instance that player is run from (for monitoring only) canvas - the canvas onto which the image is to be drawn show_params - dictionary of show parameters track_params - disctionary of track paramters pp_home - data home directory pp_profile - profile name """ self.mon=Monitor() self.mon.off() self.show_id=show_id self.root=root self.canvas=canvas self.show_params=show_params self.track_params=track_params self.pp_dir=pp_dir self.pp_home=pp_home self.pp_profile=pp_profile # open resources self.rr=ResourceReader() # get parameters self.animate_begin_text=self.track_params['animate-begin'] self.animate_end_text=self.track_params['animate-end'] if self.track_params['duration']<>"": self.duration= int(self.track_params['duration']) else: self.duration= int(self.show_params['duration']) #create an instance of PPIO so we can create gpio events self.ppio = PPIO() # get background image from profile. self.background_file='' if self.track_params['background-image']<>'': self.background_file= self.track_params['background-image'] else: if self.track_params['display-show-background']=='yes': self.background_file= self.show_params['background-image'] # get background colour from profile. if self.track_params['background-colour']<>"": self.background_colour= self.track_params['background-colour'] else: self.background_colour= self.show_params['background-colour'] # get image window from profile if self.track_params['image-window'].strip()<>"": self.image_window= self.track_params['image-window'].strip() else: self.image_window= self.show_params['image-window'].strip() # open the plugin Manager self.pim=PluginManager(self.show_id,self.root,self.canvas,self.show_params,self.track_params,self.pp_dir,self.pp_home,self.pp_profile) def play(self, track, showlist, end_callback, ready_callback, enable_menu=False): """ track - filename of track to be played showlist - from which track was taken end_callback - callback when player terminates ready_callback - callback just before anytthing is displayed enable_menu - there will be a child track so display the hint text """ # instantiate arguments self.track=track self.showlist=showlist self.enable_menu=enable_menu self.ready_callback=ready_callback self.end_callback=end_callback #init state and signals self.canvas_centre_x = int(self.canvas['width'])/2 self.canvas_centre_y = int(self.canvas['height'])/2 self.tick = 100 # tick time for image display (milliseconds) self.dwell = 10*self.duration self.dwell_counter=0 self.state=ImagePlayer.NO_SLIDE self.quit_signal=False self.drawn=None self.paused=False self.pause_text=None self.tick_timer=None #parse the image_window error,self.command,self.has_coords,self.image_x1,self.image_y1,self.image_x2,self.image_y2,self.filter=self.parse_window(self.image_window) if error =='error': self.mon.err(self,'image window error: '+self.image_window) self.end('error','image window error') else: # create an instance of showmanager so we can control concurrent shows self.show_manager=ShowManager(self.show_id,self.showlist,self.show_params,self.root,self.canvas,self.pp_dir,self.pp_profile,self.pp_home) # Control other shows at beginning reason,message=self.show_manager.show_control(self.track_params['show-control-begin']) if reason == 'error': self.end_callback(reason,message) self=None else: #display content reason,message=self.display_content() if reason == 'error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: # create animation events reason,message=self.ppio.animate(self.animate_begin_text,id(self)) if reason=='error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: # start dwelling self.mon.log(self,'playing track from show Id: '+str(self.show_id)) self.start_dwell() def input_pressed(self,symbol): if symbol =='pause': self.pause() elif symbol=='stop': self.stop() return def terminate(self,reason): # no lower level things to terminate so just go to end self.end(reason,'kill or error') def get_links(self): return self.track_params['links'] # ******************* # internal functions # ******************* def pause(self): if not self.paused: self.paused = True else: self.paused=False #print "self.paused is "+str(self.paused) def stop(self): self.quit_signal=True # ****************************************** # Sequencing # ******************************************** def start_dwell(self): if self.ready_callback<>None: self.ready_callback() self.state=ImagePlayer.SLIDE_DWELL self.tick_timer=self.canvas.after(self.tick, self.do_dwell) def do_dwell(self): if self.quit_signal == True: self.mon.log(self,"quit received") self.end('normal','user quit') else: if self.paused == False: self.dwell_counter=self.dwell_counter+1 #print "self.paused is "+str(self.paused) #if self.pause_text<>None: #print "self.pause_text exists" # one time flipping of pause text #NIK if self.paused==True and self.pause_text==None: self.pause_text=self.canvas.create_text(0,900, anchor=NW, text=self.resource('imageplayer','m01'), fill="white", font="arial 25 bold") self.canvas.update_idletasks( ) #NIK if self.paused==False and self.pause_text<>None: self.canvas.delete(self.pause_text) self.pause_text=None self.canvas.update_idletasks( ) if self.dwell<>0 and self.dwell_counter==self.dwell: self.end('normal','user quit or duration exceeded') else: self.tick_timer=self.canvas.after(self.tick, self.do_dwell) # ***************** # ending the player # ***************** def end(self,reason,message): self.state=self.NO_SLIDE # stop the plugin if self.track_params['plugin']<>'': self.pim.stop_plugin() # abort the timer if self.tick_timer<>None: self.canvas.after_cancel(self.tick_timer) self.tick_timer=None if reason in ('error','killed'): self.end_callback(reason,message) self=None else: # normal end so do show control and animation # Control concurrent shows at end reason,message=self.show_manager.show_control(self.track_params['show-control-end']) if reason =='error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: # clear events list for this track if self.track_params['animate-clear']=='yes': self.ppio.clear_events_list(id(self)) # create animation events for ending reason,message=self.ppio.animate(self.animate_end_text,id(self)) if reason=='error': self.mon.err(self,message) self.end_callback(reason,message) self=None else: self.end_callback('normal',"track has terminated or quit") #NIK if self.pause_text<>None: self.canvas.delete(self.pause_text) self.pause_text=None self.canvas.update_idletasks( ) #NIK self=None # ********************************** # displaying things # ********************************** def display_content(self): #background colour if self.background_colour<>'': self.canvas.config(bg=self.background_colour) self.canvas.delete('pp-content') # background image if self.background_file<> '': self.background_img_file = self.complete_path(self.background_file) if not os.path.exists(self.background_img_file): self.mon.err(self,"Background file not found: "+ self.background_img_file) self.end('error',"Background file not found") else: pil_background_img=PIL.Image.open(self.background_img_file) self.background = PIL.ImageTk.PhotoImage(pil_background_img) self.drawn = self.canvas.create_image(int(self.canvas['width'])/2, int(self.canvas['height'])/2, image=self.background, anchor=CENTER, tag='pp-content') # execute the plugin if required if self.track_params['plugin']<>'': reason,message,self.track = self.pim.do_plugin(self.track,self.track_params['plugin'],) if reason <> 'normal': return reason,message #get the track to be displayed if os.path.exists(self.track)==True: self.pil_image=PIL.Image.open(self.track) else: self.pil_image=None # display track image if self.pil_image<>None: self.image_width,self.image_height=self.pil_image.size if self.command=='original': # display image at its original size if self.has_coords==False: # load and display the unmodified image in centre self.tk_img=PIL.ImageTk.PhotoImage(self.pil_image) self.drawn = self.canvas.create_image(self.canvas_centre_x, self.canvas_centre_y, image=self.tk_img, anchor=CENTER, tag='pp-content') else: # load and display the unmodified image at x1,y1 self.tk_img=PIL.ImageTk.PhotoImage(self.pil_image) self.drawn = self.canvas.create_image(self.image_x1, self.image_y1, image=self.tk_img, anchor=NW, tag='pp-content') elif self.command in ('fit','shrink'): # shrink fit the window or screen preserving aspect if self.has_coords==True: window_width=self.image_x2 - self.image_x1 window_height=self.image_y2 - self.image_y1 window_centre_x=(self.image_x2+self.image_x1)/2 window_centre_y= (self.image_y2+self.image_y1)/2 else: window_width=int(self.canvas['width']) window_height=int(self.canvas['height']) window_centre_x=self.canvas_centre_x window_centre_y=self.canvas_centre_y if (self.image_width > window_width or self.image_height > window_height and self.command=='fit') or (self.command=='shrink') : # original image is larger or , shrink it to fit the screen preserving aspect self.pil_image.thumbnail((window_width,window_height),eval(self.filter)) self.tk_img=PIL.ImageTk.PhotoImage(self.pil_image) self.drawn = self.canvas.create_image(window_centre_x, window_centre_y, image=self.tk_img, anchor=CENTER, tag='pp-content') else: # fitting and original image is smaller, expand it to fit the screen preserving aspect prop_x = float(window_width) / self.image_width prop_y = float(window_height) / self.image_height if prop_x > prop_y: prop=prop_y else: prop=prop_x increased_width=int(self.image_width * prop) increased_height=int(self.image_height * prop) # print 'result',prop, increased_width,increased_height self.pil_image=self.pil_image.resize((increased_width, increased_height),eval(self.filter)) self.tk_img=PIL.ImageTk.PhotoImage(self.pil_image) self.drawn = self.canvas.create_image(window_centre_x, window_centre_y, image=self.tk_img, anchor=CENTER, tag='pp-content') elif self.command in ('warp'): # resize to window or screen without preserving aspect if self.has_coords==True: window_width=self.image_x2 - self.image_x1 window_height=self.image_y2 - self.image_y1 window_centre_x=(self.image_x2+self.image_x1)/2 window_centre_y= (self.image_y2+self.image_y1)/2 else: window_width=int(self.canvas['width']) window_height=int(self.canvas['height']) window_centre_x=self.canvas_centre_x window_centre_y=self.canvas_centre_y self.pil_image=self.pil_image.resize((window_width, window_height),eval(self.filter)) self.tk_img=PIL.ImageTk.PhotoImage(self.pil_image) self.drawn = self.canvas.create_image(window_centre_x, window_centre_y, image=self.tk_img, anchor=CENTER, tag='pp-content') # display hint if enabled if self.enable_menu== True: self.canvas.create_text(int(self.show_params['hint-x']), int(self.show_params['hint-y']), text=self.show_params['hint-text'], fill=self.show_params['hint-colour'], font=self.show_params['hint-font'], anchor=NW, tag='pp-content') # display show text if enabled if self.show_params['show-text']<> ''and self.track_params['display-show-text']=='yes': self.canvas.create_text(int(self.show_params['show-text-x']),int(self.show_params['show-text-y']), anchor=NW, text=self.show_params['show-text'], fill=self.show_params['show-text-colour'], font=self.show_params['show-text-font'], tag='pp-content') # display track text if enabled if self.track_params['track-text']<> '': self.canvas.create_text(int(self.track_params['track-text-x']),int(self.track_params['track-text-y']), anchor=NW, text=self.track_params['track-text'], fill=self.track_params['track-text-colour'], font=self.track_params['track-text-font'], tag='pp-content') self.canvas.tag_raise('pp-click-area') self.canvas.update_idletasks( ) return 'normal','' # ********************************** # utilties # ********************************** def parse_window(self,line): fields = line.split() # check there is a command field if len(fields) < 1: return 'error','',False,0,0,0,0,'' # deal with original whch has 0 or 2 arguments filter='' if fields[0]=='original': if len(fields) not in (1,3): return 'error','',False,0,0,0,0,'' # deal with window coordinates if len(fields) == 3: #window is specified if not (fields[1].isdigit() and fields[2].isdigit()): return 'error','',False,0,0,0,0,'' has_window=True return 'normal',fields[0],has_window,int(fields[1]),int(fields[2]),0,0,filter else: # no window has_window=False return 'normal',fields[0],has_window,0,0,0,0,filter #deal with remainder which has 1, 2, 5 or 6arguments # check basic syntax if fields[0] not in ('shrink','fit','warp'): return 'error','',False,0,0,0,0,'' if len(fields) not in (1,2,5,6): return 'error','',False,0,0,0,0,'' if len(fields)==6 and fields[5] not in ('NEAREST','BILINEAR','BICUBIC','ANTIALIAS'): return 'error','',False,0,0,0,0,'' if len(fields)==2 and fields[1] not in ('NEAREST','BILINEAR','BICUBIC','ANTIALIAS'): return 'error','',False,0,0,0,0,'' # deal with window coordinates if len(fields) in (5,6): #window is specified if not (fields[1].isdigit() and fields[2].isdigit() and fields[3].isdigit() and fields[4].isdigit()): return 'error','',False,0,0,0,0,'' has_window=True if len(fields)==6: filter=fields[5] else: filter='PIL.Image.NEAREST' return 'normal',fields[0],has_window,int(fields[1]),int(fields[2]),int(fields[3]),int(fields[4]),filter else: # no window has_window=False if len(fields)==2: filter=fields[1] else: filter='PIL.Image.NEAREST' return 'normal',fields[0],has_window,0,0,0,0,filter def complete_path(self,track_file): # complete path of the filename of the selected entry if track_file[0]=="+": track_file=self.pp_home+track_file[1:] self.mon.log(self,"Background image is "+ track_file) return track_file # get a text string from resources.cfg def resource(self,section,item): value=self.rr.get(section,item) if value==False: self.mon.err(self, "resource: "+section +': '+ item + " not found" ) self.error() else: return value