Esempio n. 1
0
class GapShow(Show):
    """
    this is the parent class of mediashow and liveshow
    The two derived clases just select the appropriate medialist from pp_medialist and pp_livelist
    the parents control the monitoring
    """

    # *******************
    # External interface
    # ********************

    def __init__(self, show_id, show_params, root, canvas, showlist, pp_dir,
                 pp_home, pp_profile, command_callback):

        # init the common bits
        Show.base__init__(self, show_id, show_params, root, canvas, showlist,
                          pp_dir, pp_home, pp_profile, command_callback)

        # instatiatate the screen driver - used only to access enable and hide click areas
        self.sr = ScreenDriver()

        self.controlsmanager = ControlsManager()

        # Init variables special to this show
        self.poll_for_interval_timer = None
        self.interval_timer_signal = False
        self.waiting_for_interval = False
        self.interval_timer = None
        self.duration_timer = None

        self.end_trigger_signal = False
        self.next_track_signal = False
        self.previous_track_signal = False
        self.play_child_signal = False
        self.error_signal = False
        self.show_timeout_signal = False

        self.req_next = 'nil'
        self.state = 'closed'

        self.count = 0
        self.interval = 0
        self.duration = 0
        self.controls_list = []
        self.enable_hint = True

    def play(self, end_callback, show_ready_callback, parent_kickback_signal,
             level, controls_list):
        self.mon.newline(3)
        self.mon.trace(self, self.show_params['show-ref'])

        Show.base_play(self, end_callback, show_ready_callback,
                       parent_kickback_signal, level, controls_list)

        # unpack show parameters

        reason, message, self.show_timeout = Show.calculate_duration(
            self, self.show_params['show-timeout'])
        if reason == 'error':
            self.mon.err(
                self, 'ShowTimeout has bad time: ' +
                self.show_params['show-timeout'])
            self.end(
                'error', 'ShowTimeout has bad time: ' +
                self.show_params['show-timeout'])

        self.track_count_limit = int(self.show_params['track-count-limit'])

        reason, message, self.interval = Show.calculate_duration(
            self, self.show_params['interval'])
        if reason == 'error':
            self.mon.err(
                self, 'Interval has bad time: ' + self.show_params['interval'])
            self.end('error',
                     'Interval has bad time: ' + self.show_params['interval'])

        # delete eggtimer started by the parent
        if self.previous_shower is not None:
            self.previous_shower.delete_eggtimer()

        self.start_show()

# ********************************
# Respond to external events
# ********************************

# exit received from another concurrent show

    def exit(self):
        Show.base_exit(self)

    # terminate Pi Presents
    def terminate(self):
        Show.base_terminate(self)

# respond to input events

    def handle_input_event(self, symbol):
        Show.base_handle_input_event(self, symbol)

    def handle_input_event_this_show(self, symbol):
        #  check symbol against mediashow triggers
        if self.state == 'waiting' and self.show_params[
                'trigger-start-type'] in (
                    'input', 'input-persist'
                ) and symbol == self.show_params['trigger-start-param']:
            self.mon.stats(self.show_params['type'],
                           self.show_params['show-ref'],
                           self.show_params['title'], 'start trigger', '', '',
                           '')
            Show.delete_admin_message(self)
            self.start_list()

        elif self.state == 'playing' and self.show_params[
                'trigger-end-type'] == 'input' and symbol == self.show_params[
                    'trigger-end-param']:
            self.end_trigger_signal = True
            if self.shower is not None:
                self.shower.do_operation('stop')
            elif self.current_player is not None:
                self.current_player.input_pressed('stop')

        elif self.state == 'playing' and self.show_params[
                'trigger-next-type'] == 'input' and symbol == self.show_params[
                    'trigger-next-param']:
            self.mon.stats(self.show_params['type'],
                           self.show_params['show-ref'],
                           self.show_params['title'], 'next trigger', '', '',
                           '')
            self.next()
        else:
            # event is not a trigger so must be internal operation
            operation = self.base_lookup_control(symbol, self.controls_list)
            if operation != '':
                self.do_operation(operation)

    # overrides base
    # service the standard operations for this show
    def do_operation(self, operation):
        # print 'do_operation ',operation
        self.mon.trace(self, operation)
        if operation == 'exit':
            self.exit()

        elif operation == 'stop':
            if self.level != 0:
                # not at top so stop the show
                self.user_stop_signal = True
                # and stop the track first
                if self.current_player is not None:
                    self.current_player.input_pressed('stop')
            else:
                # at top, just stop track if running
                if self.current_player is not None:
                    self.current_player.input_pressed('stop')

        elif operation == 'up' and self.state == 'playing':
            # print '\nUP'
            self.previous()

        elif operation == 'down' and self.state == 'playing':
            self.next()

        elif operation == 'play':
            # use 'play' to start child if state=playing or to trigger the show if waiting for trigger
            if self.state == 'playing':
                if self.show_params['child-track-ref'] != '':
                    # set a signal because must stop current track before running child show
                    self.play_child_signal = True
                    self.child_track_ref = self.show_params['child-track-ref']
                    # and stop the current track if its running
                    if self.current_player is not None:
                        self.current_player.input_pressed('stop')
            else:
                if self.state == 'waiting':
                    self.mon.stats(self.show_params['type'],
                                   self.show_params['show-ref'],
                                   self.show_params['title'], 'start trigger',
                                   '', '', '')
                    Show.delete_admin_message(self)
                    self.start_list()

        elif operation == 'pause':
            if self.current_player is not None:
                self.current_player.input_pressed('pause')

        elif operation in ('no-command', 'null'):
            return

        # if the operation is omxplayer mplayer or uzbl runtime control then pass it to player if running
        elif operation[0:4] == 'omx-' or operation[
                0:6] == 'mplay-' or operation[0:5] == 'uzbl-':
            if self.current_player is not None:
                self.current_player.input_pressed(operation)

    def next(self):
        # stop track if running and set signal
        self.next_track_signal = True
        if self.shower is not None:
            self.shower.do_operation('stop')
        else:
            if self.current_player is not None:
                self.current_player.input_pressed('stop')

    def previous(self):
        self.previous_track_signal = True
        if self.shower is not None:
            self.shower.do_operation('stop')
        else:
            if self.current_player is not None:
                self.current_player.input_pressed('stop')

# ***************************
# Show sequencing
# ***************************

    def start_show(self):
        # initial direction from parent show

        self.kickback_for_next_track = self.parent_kickback_signal
        # print '\n\ninital KICKBACK from parent', self.kickback_for_next_track

        # start duration timer
        if self.show_timeout != 0:
            # print 'set alarm ', self.show_timeout
            self.duration_timer = self.canvas.after(self.show_timeout * 1000,
                                                    self.show_timeout_stop)

        self.first_list = True

        # and start the first list of the show
        self.wait_for_trigger()

    def wait_for_trigger(self):

        # wait for trigger sets the state to waiting so that trigger events can do a start_list.
        self.state = 'waiting'

        self.mon.log(
            self, self.show_params['show-ref'] + ' ' + str(self.show_id) +
            ": Waiting for trigger: " + self.show_params['trigger-start-type'])

        if self.show_params['trigger-start-type'] == "input":

            #close the previous track to display admin message
            Show.base_shuffle(self)
            Show.base_track_ready_callback(self, False)
            Show.display_admin_message(self,
                                       self.show_params['trigger-wait-text'])

        elif self.show_params['trigger-start-type'] == "input-persist":
            if self.first_list == True:
                #first time through track list so play the track without waiting to get to end.
                self.first_list = False
                self.start_list()
            else:
                #wait for trigger while displaying previous track
                pass

        elif self.show_params['trigger-start-type'] == "start":
            # don't close the previous track to give seamless repeat of the show
            self.start_list()

        else:
            self.mon.err(
                self,
                "Unknown trigger: " + self.show_params['trigger-start-type'])
            self.end(
                'error',
                "Unknown trigger: " + self.show_params['trigger-start-type'])

    # timer for repeat=interval
    def end_interval_timer(self):
        self.interval_timer_signal = True
        # print 'INTERVAL TIMER ended'
        if self.shower is not None:
            self.shower.do_operation('stop')
        elif self.current_player is not None:
            self.current_player.input_pressed('stop')

    #  show timeout happened
    def show_timeout_stop(self):
        self.stop_timers()
        Show.base_show_timeout_stop(self)

    def start_list(self):
        # starts the list or any repeat having waited for trigger first.
        self.state = 'playing'

        # initialise track counter for the list
        self.track_count = 0

        # start interval timer
        self.interval_timer_signal = False
        if self.interval != 0:
            self.interval_timer = self.canvas.after(self.interval * 1000,
                                                    self.end_interval_timer)

        #get rid of previous track in order to display the empty message
        if self.medialist.display_length() == 0:
            Show.base_shuffle(self)
            Show.base_track_ready_callback(self, False)
            Show.display_admin_message(self, self.show_params['empty-text'])
            self.wait_for_not_empty()
        else:
            self.not_empty()

    def wait_for_not_empty(self):
        if self.medialist.display_length() == 0:
            # list is empty retry after 5 secs
            self.canvas.after(5000, self.wait_for_not_empty)
        else:
            Show.delete_admin_message(self)
            self.not_empty()

    def not_empty(self):
        #get first or last track depending on direction
        # print 'use direction for start or end of list', self.kickback_for_next_track
        if self.kickback_for_next_track is True:
            self.medialist.finish()
        else:
            self.medialist.start()
        self.start_load_show_loop(self.medialist.selected_track())

# ***************************
# Track load/show loop
# ***************************

# track playing loop starts here

    def start_load_show_loop(self, selected_track):

        # uncomment the next line to write stats for every track
        # Show.write_stats(self,'play a track',self.show_params,selected_track)

        # shuffle players
        Show.base_shuffle(self)

        self.delete_eggtimer()

        # is child track required
        if self.show_params['child-track-ref'] != '':
            self.enable_child = True
        else:
            self.enable_child = False

        # load the track or show
        # params - track,enable_menu
        enable = self.enable_child & self.enable_hint
        Show.base_load_track_or_show(self, selected_track,
                                     self.what_next_after_load,
                                     self.end_shower, enable)

    # track has loaded so show it.
    def what_next_after_load(self, status, message):
        self.mon.log(
            self, 'Show Id ' + str(self.show_id) +
            ' load complete with status: ' + status + '  message: ' + message)
        if self.current_player.play_state == 'load-failed':
            self.error_signal = True
            self.what_next_after_showing()

        else:
            if self.terminate_signal is True or self.exit_signal is True or self.user_stop_signal is True:
                self.what_next_after_showing()
            else:
                self.mon.trace(self, ' - showing track')
                self.current_player.show(self.track_ready_callback,
                                         self.finished_showing,
                                         self.closed_after_showing)

    def finished_showing(self, reason, message):
        self.sr.hide_click_areas(self.controls_list)
        if self.current_player.play_state == 'show-failed':
            self.error_signal = True
        else:
            self.req_next = 'finished-player'
        # showing has finished with 'pause at end', showing the next track will close it after next has started showing
        self.mon.trace(self, ' - pause at end ')
        self.mon.log(
            self, "pause at end of showing track with reason: " + reason +
            ' and message: ' + message)
        self.what_next_after_showing()

    def closed_after_showing(self, reason, message):
        self.sr.hide_click_areas(self.controls_list)
        if self.current_player.play_state == 'show-failed':
            self.error_signal = True
        else:
            self.req_next = 'closed-player'
        # showing has finished with closing of player but track instance is alive for hiding the x_content
        self.mon.trace(self, ' - closed')
        self.mon.log(
            self, "Closed after showing track with reason: " + reason +
            ' and message: ' + message)
        self.what_next_after_showing()

    # subshow or child show has ended
    def end_shower(self, show_id, reason, message):
        self.mon.log(
            self, self.show_params['show-ref'] + ' ' + str(self.show_id) +
            ': Returned from shower with ' + reason + ' ' + message)
        self.sr.hide_click_areas(self.controls_list)
        self.req_next = reason
        Show.base_end_shower(self)
        self.what_next_after_showing()

    def pretty_what_next_after_showing_state(self):
        state = '\n* terminate signal ' + str(self.terminate_signal)
        state += '\n* error signal ' + str(self.error_signal)
        state += '\n* req_next used to indicate subshow reports an error ' + self.req_next
        state += '\n* exit signal ' + str(self.exit_signal)
        state += '\n* show timeout  signal ' + str(self.show_timeout_signal)
        state += '\n* user stop  signal ' + str(self.user_stop_signal)
        state += '\n* previous track  signal ' + str(
            self.previous_track_signal)
        state += '\n* next track  signal ' + str(self.next_track_signal)
        state += '\n* kickback from subshow ' + str(
            self.subshow_kickback_signal)
        return state + '\n'

    def what_next_after_showing(self):
        self.mon.trace(self, self.pretty_what_next_after_showing_state())

        self.track_count += 1
        # set false when child rack is to be played
        self.enable_hint = True

        # first of all deal with conditions that do not require the next track to be shown
        # some of the conditions can happen at any time, others only when a track is closed or at pause_at_end

        # need to terminate
        if self.terminate_signal is True:
            self.terminate_signal = False
            self.stop_timers()
            # set what to do after closed or unloaded
            self.ending_reason = 'killed'
            Show.base_close_or_unload(self)

        elif self.error_signal == True or self.req_next == 'error':
            self.error_signal = False
            self.req_next = ''
            self.stop_timers()
            # set what to do after closed or unloaded
            self.ending_reason = 'error'
            Show.base_close_or_unload(self)

        # used for exiting show from other shows, time of day, external etc.
        elif self.exit_signal is True:
            self.exit_signal = False
            self.stop_timers()
            self.ending_reason = 'exit'
            Show.base_close_or_unload(self)

        #  show timeout
        elif self.show_timeout_signal is True:
            self.show_timeout_signal = False
            self.stop_timers()
            self.ending_reason = 'user-stop'
            Show.base_close_or_unload(self)

        # user wants to stop the show
        elif self.user_stop_signal is True:
            self.user_stop_signal = False
            self.stop_timers()
            self.ending_reason = 'user-stop'
            Show.base_close_or_unload(self)

    # interval > 0. If last track has finished we are waiting for interval timer before ending the list
    # note: if medialist finishes after interval is up then this route is used to start trigger.
        elif self.waiting_for_interval is True:
            # interval_timer_signal set by alarm clock started in start_list
            if self.interval_timer_signal is True:
                self.interval_timer_signal = False
                self.waiting_for_interval = False
                # print 'RECEIVED INTERVAL TIMER SIGNAL'
                if self.show_params['repeat'] == 'repeat':
                    self.wait_for_trigger()
                else:
                    self.stop_timers()
                    self.ending_reason = 'user-stop'
                    Show.base_close_or_unload(self)
            else:
                self.poll_for_interval_timer = self.canvas.after(
                    1000, self.what_next_after_showing)

        # has content of list been changed (replaced if it has, used for content of livelist)
        # causes it to go to wait_for_trigger and start_list
        #not sure why need clos_and_unload here ???????
        elif self.medialist.replace_if_changed() is True:
            self.ending_reason = 'change-medialist'
            # print 'CHANGE REQ'
            Show.base_close_or_unload(self)

        # otherwise consider operation that might show the next track
        else:
            # setup default direction for next track as normally goes forward unless kicked back
            self.kickback_for_next_track = False
            # print 'init direction to False at begin of what_next_after showing', self.kickback_for_next_track
            # end trigger from input, or track count
            if self.end_trigger_signal is True or (self.track_count_limit > 0
                                                   and self.track_count
                                                   == self.track_count_limit):
                self.end_trigger_signal = False
                # repeat so test start trigger
                if self.show_params['repeat'] == 'repeat':
                    self.stop_timers()
                    # print 'END TRIGGER restart'
                    self.wait_for_trigger()
                else:
                    # single run so exit show
                    if self.level != 0:
                        self.kickback_for_next_track = False
                        self.subshow_kickback_signal = False
                        self.end('normal',
                                 "End of single run - Return from Sub Show")
                    else:
                        # end of single run and at top - exit the show
                        self.stop_timers()
                        self.ending_reason = 'user-stop'
                        Show.base_close_or_unload(self)

            # user wants to play child
            elif self.play_child_signal is True:
                self.play_child_signal = False
                index = self.medialist.index_of_track(self.child_track_ref)
                if index >= 0:
                    # don't use select the track as need to preserve mediashow sequence for returning from child
                    child_track = self.medialist.track(index)
                    Show.write_stats(self, 'play child', self.show_params,
                                     child_track)
                    self.display_eggtimer()
                    self.enable_hint = False
                    self.start_load_show_loop(child_track)
                else:
                    self.mon.err(
                        self, "Child not found in medialist: " +
                        self.child_track_ref)
                    self.ending_reason = 'error'
                    Show.base_close_or_unload(self)

            # skip to next track on user input or after subshow
            elif self.next_track_signal is True:
                # print 'skip forward test' ,self.subshow_kickback_signal
                if self.next_track_signal is True or self.subshow_kickback_signal is False:
                    self.next_track_signal = False
                    self.kickback_for_next_track = False
                    if self.medialist.at_end() is True:
                        # medialist_at_end can give false positive for shuffle
                        if self.show_params[
                                'sequence'] == "ordered" and self.show_params[
                                    'repeat'] == 'repeat':
                            self.wait_for_trigger()
                        elif self.show_params[
                                'sequence'] == "ordered" and self.show_params[
                                    'repeat'] == 'single-run':
                            if self.level != 0:
                                self.kickback_for_next_track = False
                                self.subshow_kickback_signal = False
                                # print 'end subshow skip forward test, self. direction is ' ,self.kickback_for_next_track
                                self.end('normal', "Return from Sub Show")
                            else:
                                # end of single run and at top - exit the show
                                self.stop_timers()
                                self.ending_reason = 'user-stop'
                                Show.base_close_or_unload(self)
                        else:
                            # shuffling  - just do next track
                            self.kickback_for_next_track = False
                            self.medialist.next(self.show_params['sequence'])
                            self.start_load_show_loop(
                                self.medialist.selected_track())
                    else:
                        # not at end just do next track
                        self.medialist.next(self.show_params['sequence'])
                        self.start_load_show_loop(
                            self.medialist.selected_track())

            # skip to previous track on user input or after subshow
            elif self.previous_track_signal is True or self.subshow_kickback_signal is True:
                # print 'skip backward test, subshow kickback is' ,self.subshow_kickback_signal
                self.subshow_kickback_signal = False
                self.previous_track_signal = False
                self.kickback_for_next_track = True
                # medialist_at_start can give false positive for shuffle
                if self.medialist.at_start() is True:
                    # print 'AT START'
                    if self.show_params[
                            'sequence'] == "ordered" and self.show_params[
                                'repeat'] == 'repeat':
                        self.kickback_for_next_track = True
                        self.wait_for_trigger()
                    elif self.show_params[
                            'sequence'] == "ordered" and self.show_params[
                                'repeat'] == 'single-run':
                        if self.level != 0:
                            self.kickback_for_next_track = True
                            self.subshow_kickback_signal = True
                            # print 'end subshow skip forward test, self. direction is ' ,self.kickback_for_next_track
                            self.end('normal', "Return from Sub Show")
                        else:
                            # end of single run and at top - exit the show
                            self.stop_timers()
                            self.ending_reason = 'user-stop'
                            Show.base_close_or_unload(self)
                    else:
                        # shuffling  - just do previous track
                        self.kickback_for_next_track = True
                        self.medialist.previous(self.show_params['sequence'])
                        self.start_load_show_loop(
                            self.medialist.selected_track())
                else:
                    # not at end just do next track
                    self.medialist.previous(self.show_params['sequence'])
                    self.start_load_show_loop(self.medialist.selected_track())

            # AT END OF MEDIALIST
            elif self.medialist.at_end() is True:
                # print 'MEDIALIST AT END'

                # interval>0 and list finished so wait for the interval timer
                if self.show_params[
                        'sequence'] == "ordered" and self.interval > 0 and self.interval_timer_signal == False:
                    self.waiting_for_interval = True
                    # print 'WAITING FOR INTERVAL'
                    Show.base_shuffle(self)
                    Show.base_track_ready_callback(self, False)
                    self.poll_for_interval_timer = self.canvas.after(
                        200, self.what_next_after_showing)

                # interval=0
                #elif self.show_params['sequence'] == "ordered" and self.show_params['repeat'] == 'repeat' and self.show_params['trigger-end-type']== 'interval' and int(self.show_params['trigger-end-param']) == 0:
                #self.medialist.next(self.show_params['sequence'])
                # self.start_load_show_loop(self.medialist.selected_track())

                # shuffling so there is no end condition, get out of end test
                elif self.show_params['sequence'] == "shuffle":
                    self.medialist.next(self.show_params['sequence'])
                    self.start_load_show_loop(self.medialist.selected_track())

                # nothing special to do at end of list, just repeat or exit
                elif self.show_params['sequence'] == "ordered":
                    if self.show_params['repeat'] == 'repeat':
                        self.wait_for_trigger()
                    else:
                        # single run
                        # if not at top return to parent
                        if self.level != 0:
                            self.end('normal', "End of Single Run")
                        else:
                            # at top so close the show
                            self.stop_timers()
                            self.ending_reason = 'user-stop'
                            Show.base_close_or_unload(self)

                else:
                    self.mon.err(
                        self, "Unhandled playing event at end of list: " +
                        self.show_params['sequence'] + ' with ' +
                        self.show_params['repeat'] + " of " +
                        self.show_params['trigger-end-param'])
                    self.end(
                        'error', "Unhandled playing event at end of list: " +
                        self.show_params['sequence'] + ' with ' +
                        self.show_params['repeat'] + " of " +
                        self.show_params['trigger-end-param'])

            elif self.medialist.at_end() is False:
                # nothing special just do the next track
                self.medialist.next(self.show_params['sequence'])
                self.start_load_show_loop(self.medialist.selected_track())

            else:
                # unhandled state
                self.mon.err(
                    self, "Unhandled playing event: " +
                    self.show_params['sequence'] + ' with ' +
                    self.show_params['repeat'] + " of " +
                    self.show_params['trigger-end-param'])
                self.end(
                    'error', "Unhandled playing event: " +
                    self.show_params['sequence'] + ' with ' +
                    self.show_params['repeat'] + " of " +
                    self.show_params['trigger-end-param'])

# *********************
# Interface with other shows/players to reduce black gaps
# *********************

# called just before a track is shown to remove the  previous track from the screen
# and if necessary close it

    def track_ready_callback(self, enable_show_background):
        self.delete_eggtimer()

        # get control bindings for this show
        # needs to be done for each track as track can override the show controls
        if self.show_params['disable-controls'] == 'yes':
            self.controls_list = []
        else:
            reason, message, self.controls_list = self.controlsmanager.get_controls(
                self.show_params['controls'])
            if reason == 'error':
                self.mon.err(self, message)
                self.end('error', "error in controls: " + message)
                return

            # print 'controls',reason,self.show_params['controls'],self.controls_list
            #merge controls from the track
            controls_text = self.current_player.get_links()
            reason, message, track_controls = self.controlsmanager.parse_controls(
                controls_text)
            if reason == 'error':
                self.mon.err(
                    self, message + " in track: " +
                    self.current_player.track_params['track-ref'])
                self.error_signal = True
                self.what_next_after_showing()
            self.controlsmanager.merge_controls(self.controls_list,
                                                track_controls)

        # enable the click-area that are in the list of controls
        self.sr.enable_click_areas(self.controls_list)
        Show.base_track_ready_callback(self, enable_show_background)

    # callback from begining of a subshow, provide previous player to called show
    def subshow_ready_callback(self):
        return Show.base_subshow_ready_callback(self)

# *********************
# End the show
# *********************

    def end(self, reason, message):
        Show.base_end(self, reason, message)

    def stop_timers(self):
        # clear outstanding time of day events for this show
        # self.tod.clear_times_list(id(self))

        if self.poll_for_interval_timer is not None:
            self.canvas.after_cancel(self.poll_for_interval_timer)
            self.poll_for_interval_timer = None

        if self.interval_timer is not None:
            self.canvas.after_cancel(self.interval_timer)
            self.interval_timer = None

        if self.duration_timer is not None:
            self.canvas.after_cancel(self.duration_timer)
            self.duration_timer = None
Esempio n. 2
0
class HyperlinkShow(Show):
    """
        Aimed at touchscreens but can be used for any purpose where the user is required to follow hyperlinks between tracks
        Profiles for media tracks (message, image, video, audio ) specify links to other tracks
        In a link a symbolic name of an input is associated with a track-reference
        The show begins at a special track specified in the profiile called the First Track and moves to other tracks
         - when a link is executed by a input event with a specified symbolic name
        -  at the natural end of the track using the special pp-onend symbolic name
        If using the 'call' link command PP keeps a record of the tracks it has visited so the 'return' command can go back.
        Executes timeout-track if no user input is received (includes pp-onend but not repeat)

        There is a another special track with Home Track. The home command returns here and the 'return n' command will not go back past here.
        This was designed to allow the timeout to go back to First Track which would advertise the show.
        Once the user had been enticed and clicked a link to move to Home pressing home or return would not return him to First Track

        You can make the Home Track and the First Track the same track if you want.
        You may want a track, if it is a video or audio track, to repeat. You can use repeat for this and it will not cancel the timeout.
        
        Image and message tracks can have a zero duration so they never end naturally so repeat is not required.

        links are of the form:
           symbolic-name command [track-ref]
        
        link commands:
          call <track-ref> play track-ref and add it to the path
          return - return 1 back up the path removing the track from the path, stops at home-track.
          return n - return n tracks back up the path removing the track from the path, stops at home-track.
          return <track-ref> return to <track-ref> removing tracks from the path
          home  - return to home-track removing tracks from the path
          jump <track-ref-> - play track-ref forgetting the path back to home-track
          goto <track-ref> - play track-ref, forget the path
          repeat - repeat the track
          exit - end the hyperlink show
          null - inhibits the link defined in the show with the same symbolic name.

          reserved symbolic names
          pp-onend command  - pseudo symbolic name for end of a track

    interface:
        * __init__ - initlialises the show
         * play - selects the first track to play (first-track) 
         * input_pressed,  - receives user events passes them to a Shower/Player if a track is playing,
                otherwise actions them depending on the symbolic name supplied
        * exit  - stops the show from another show
        * terminate  - aborts the show, used whan clsing or after errors
        * track_ready_callback - called by the next track to be played to remove the previous track from display
        * subshow_ready_callback - called by the subshow to get the last track of the parent show
        * subshow_ended_callback - called at the start of a parent show to get the last track of the subshow
        
    """

    # *********************
    # external interface
    # ********************

    def __init__(self, show_id, show_params, root, canvas, showlist, pp_dir,
                 pp_home, pp_profile, command_callback):
        """
            show_id - index of the top level show caling this (for debug only)
            show_params - dictionary section for the menu
            canvas - the canvas that the menu is to be written on
            showlist  - the showlist
            pp_dir - Pi Presents directory
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory
        """

        # init the common bits
        Show.base__init__(self, show_id, show_params, root, canvas, showlist,
                          pp_dir, pp_home, pp_profile, command_callback)

        # instatiatate the screen driver - used only to access enable and hide click areas
        self.sr = ScreenDriver()

        # create a path stack and control path debugging
        if self.show_params['debug-path'] == 'yes':
            self.debug = True
        else:
            self.debug = False
        self.path = PathManager()

        self.allowed_links = ('return', 'home', 'call', 'null', 'exit', 'goto',
                              'play', 'jump', 'repeat', 'pause', 'no-command',
                              'stop')

        # init variables
        self.track_timeout_timer = None
        self.show_timeout_timer = None
        self.next_track_signal = False
        self.next_track_ref = ''
        self.current_track_ref = ''
        self.current_track_type = ''
        self.req_next = ''

    def play(self, end_callback, show_ready_callback, parent_kickback_signal,
             level, controls_list):
        """ starts the hyperlink show at start-track 
              end_callback - function to be called when the show exits
              show_ready_callback - callback to get previous show and track
              level is 0 when the show is top level (run from [start] or from show control)
              parent_kickback_signal is not used passed to subshow by base class as parent_kickback_signal
        """
        # need to instantiate the medialist here as in gapshow done in derived class
        self.medialist = MediaList('ordered')

        Show.base_play(self, end_callback, show_ready_callback,
                       parent_kickback_signal, level, controls_list)

        #dummy as it gets passed down to subshow, however it isn't actuallly used.
        self.controls_list = []

        self.mon.trace(self, self.show_params['show-ref'])

        # read show destinations
        self.first_track_ref = self.show_params['first-track-ref']
        self.home_track_ref = self.show_params['home-track-ref']
        self.timeout_track_ref = self.show_params['timeout-track-ref']

        #parse the show and track timeouts
        reason, message, self.show_timeout = Show.calculate_duration(
            self, self.show_params['show-timeout'])
        if reason == 'error':
            self.mon.err(
                self, 'Show Timeout has bad time: ' +
                self.show_params['show-timeout'])
            self.end(
                'error',
                'show timeout, bad time: ' + self.show_params['show-timeout'])

        reason, message, self.track_timeout = Show.calculate_duration(
            self, self.show_params['track-timeout'])
        if reason == 'error':
            self.mon.err(
                self, 'Track Timeout has bad time: ' +
                self.show_params['track-timeout'])
            self.end(
                'error', 'track timeout, bad time: ' +
                self.show_params['track-timeout'])

        # and delete eggtimer
        if self.previous_shower is not None:
            self.previous_shower.delete_eggtimer()

        self.do_first_track()

# exit received from another concurrent show via ShowManager

    def exit(self):
        self.stop_timers()
        Show.base_exit(self)

    #  show timeout happened
    def show_timeout_stop(self):
        self.stop_timers()
        Show.base_show_timeout_stop(self)

    # kill or error
    def terminate(self):
        self.stop_timers()
        Show.base_terminate(self)

# respond to inputs  - call base_input_pressed to pass to subshow

    def handle_input_event(self, symbol):
        Show.base_handle_input_event(self, symbol)

    def handle_input_event_this_show(self, symbol):
        # does the symbol match a link, if so execute it
        # some link commands do a subset of the internal operations
        # find the first entry in links that matches the symbol and execute its operation
        found, link_op, link_arg = self.path.find_link(symbol, self.links)
        if found is True:
            # cancel the show timeout when playing another track
            if self.show_timeout_timer is not None:
                self.canvas.after_cancel(self.show_timeout_timer)
                self.show_timeout_timer = None

            if link_op == 'home':
                self.decode_home()
                self.stop_current_track()

            elif link_op == 'return':
                self.decode_return(link_arg)
                self.stop_current_track()

            elif link_op == 'call':
                self.decode_call(link_arg)
                self.stop_current_track()

            elif link_op == 'goto':
                self.decode_goto(link_arg)
                self.stop_current_track()

            elif link_op == 'jump':
                self.decode_jump(link_arg)
                self.stop_current_track()

            elif link_op == 'repeat':
                self.decode_repeat()
                self.stop_current_track()

            elif link_op == 'exit':
                self.exit()

            elif link_op == 'stop':
                self.do_stop()

            elif link_op in ('no-command', 'null'):
                return

            # in-track operations
            elif link_op == 'pause':
                if self.current_player is not None:
                    self.current_player.input_pressed(link_op)

            elif link_op[0:4] == 'omx-' or link_op[0:6] == 'mplay-' or link_op[
                    0:5] == 'uzbl-':
                if self.current_player is not None:
                    self.current_player.input_pressed(link_op)

            else:
                self.mon.err(self, "unknown link command: " + link_op)
                self.end('error', "unknown link command: " + link_op)

    def do_operation(self, operation):
        if operation == 'stop':
            self.do_stop()

    def do_stop(self):
        # print link_op,self.current_player,self.current_track_ref,self.level
        #quiescent in all tracks
        if self.level != 0:
            # lower level so exit to level above
            self.stop_timers()
            self.user_stop_signal = True
            if self.current_player is not None:
                self.current_player.input_pressed('stop')
        else:
            # at top do nothing
            pass

# *********************
# INTERNAL FUNCTIONS
# ********************

# *********************
# Show Sequencer
# *********************

    def track_timeout_callback(self):
        self.mon.trace(self, 'goto ' + self.timeout_track_ref)
        self.next_track_op = 'goto'
        self.next_track_arg = self.timeout_track_ref
        self.what_next_after_showing()

    def stop_current_track(self):
        if self.shower is not None:
            self.shower.do_operation('stop')
        elif self.current_player is not None:
            self.current_player.input_pressed('stop')
        else:
            self.what_next_after_showing()

    def decode_call(self, track_ref):
        if track_ref != self.current_track_ref:
            self.mon.log(self, 'call: ' + track_ref)
            self.next_track_signal = True
            self.next_track_op = 'call'
            self.next_track_arg = track_ref

    def decode_goto(self, to):
        self.mon.log(self, 'goto: ' + to)
        self.next_track_signal = True
        self.next_track_op = 'goto'
        self.next_track_arg = to

    def decode_jump(self, to):
        self.mon.log(self, 'jump to: ' + to)
        self.next_track_signal = True
        self.next_track_op = 'jump'
        self.next_track_arg = to

    def decode_repeat(self):
        self.mon.log(self, 'repeat: ')
        self.next_track_signal = True
        self.next_track_op = 'repeat'

    def decode_return(self, to):
        self.next_track_signal = True
        if to.isdigit() or to == '':
            self.mon.log(self, 'hyperlink command - return by: ' + to)
            self.next_track_op = 'return-by'
            if to == '':
                self.next_track_arg = '1'
            else:
                self.next_track_arg = to
        else:
            self.mon.log(self, 'hyperlink command - return to: ' + to)
            self.next_track_op = 'return-to'
            self.next_track_arg = to

    def decode_home(self):
        self.mon.log(self, 'hyperlink command - home')
        self.next_track_signal = True
        self.next_track_op = 'home'

    def do_first_track(self):
        index = self.medialist.index_of_track(self.first_track_ref)
        if index >= 0:
            self.continue_timeout = False
            # don't use select the track as not using selected_track in hyperlinkshow
            first_track = self.medialist.track(index)
            self.current_track_ref = first_track['track-ref']
            self.path.append(first_track['track-ref'])
            if self.debug:
                print 'First Track: ' + first_track[
                    'track-ref'] + self.path.pretty_path()
            self.start_load_show_loop(first_track)
        else:
            self.mon.err(
                self, "first-track not found in medialist: " +
                self.show_params['first-track-ref'])
            self.end(
                'error', "first track not found in medialist: " +
                self.show_params['first-track-ref'])

    def start_load_show_loop(self, selected_track):
        # shuffle players
        Show.base_shuffle(self)

        self.mon.trace(self, '')

        self.display_eggtimer()

        # start the show timer when displaying the first track
        if self.current_track_ref == self.first_track_ref:
            if self.show_timeout_timer is not None:
                self.canvas.after_cancel(self.show_timeout_timer)
                self.show_timeout_timer = None
            if self.show_timeout != 0:
                self.show_timeout_timer = self.canvas.after(
                    self.show_timeout * 1000, self.show_timeout_stop)

    # start timeout for the track if required   ???? differnet to radiobuttonshow
        if self.continue_timeout is False:
            if self.track_timeout_timer is not None:
                self.canvas.after_cancel(self.track_timeout_timer)
                self.track_timeout_timer = None
            if self.current_track_ref != self.first_track_ref and self.track_timeout != 0:
                self.track_timeout_timer = self.canvas.after(
                    self.track_timeout * 1000, self.track_timeout_callback)

        # get control bindings for this show
        # needs to be done for each track as track can override the show controls
        # read the show links. Track links will be added by track_ready_callback
        if self.show_params['disable-controls'] == 'yes':
            self.links = []
        else:
            reason, message, self.links = self.path.parse_links(
                self.show_params['links'], self.allowed_links)
            if reason == 'error':
                self.mon.err(self, message + " in show")
                self.end('error', message + " in show")

        # load the track or show
        # params - track,, track loaded callback, end eshoer callback,enable_menu
        Show.base_load_track_or_show(self, selected_track,
                                     self.what_next_after_load,
                                     self.end_shower, False)

# track has loaded so show it.

    def what_next_after_load(self, status, message):
        self.mon.trace(
            self,
            ' - load complete with status: ' + status + ' message: ' + message)
        if self.current_player.play_state == 'load-failed':
            self.mon.err(self, 'load failed')
            self.req_next = 'error'
            self.what_next_after_showing()
        else:
            if self.show_timeout_signal is True or self.terminate_signal is True or self.exit_signal is True or self.user_stop_signal is True:
                self.what_next_after_showing()
            else:
                self.mon.trace(self, ' - showing track')
                self.current_player.show(self.track_ready_callback,
                                         self.finished_showing,
                                         self.closed_after_showing)

    def finished_showing(self, reason, message):
        # showing has finished with 'pause at end'. Player is paused and track instance is alive for hiding the x_content
        # this will happen in track_ready_callback of next track or in end?????
        self.mon.trace(self, ' - pause at end')
        self.mon.log(
            self, "pause at end of showing track with reason: " + reason +
            ' and message: ' + message)
        self.sr.hide_click_areas(self.links)
        if self.current_player.play_state == 'show-failed':
            self.req_next = 'error'
        else:
            self.req_next = 'finished-player'
        self.what_next_after_showing()

    def closed_after_showing(self, reason, message):
        # showing has finished with closing of player. Track instance is alive for hiding the x_content
        # this will happen in track_ready_callback of next track or in end?????
        self.mon.trace(self, ' - closed after showing')
        self.mon.log(
            self, "Closed after showing track with reason: " + reason +
            ' and message: ' + message)
        self.sr.hide_click_areas(self.links)
        if self.current_player.play_state == 'show-failed':
            self.req_next = 'error'
        else:
            self.req_next = 'closed-player'
        self.what_next_after_showing()

    # subshow or child show has ended
    def end_shower(self, show_id, reason, message):
        self.mon.log(
            self, self.show_params['show-ref'] + ' ' + str(self.show_id) +
            ': Returned from shower with ' + reason + ' ' + message)
        self.sr.hide_click_areas(self.links)
        if reason == 'error':
            self.req_next = 'error'
            self.what_next_after_showing()
        else:
            Show.base_end_shower(self)
            self.next_track_signal = True
            self.next_track_op = 'return-by'
            self.next_track_arg = '1'
            self.what_next_after_showing()

    def what_next_after_showing(self):
        self.mon.trace(self, '')
        # need to terminate
        if self.terminate_signal is True:
            self.terminate_signal = False
            # what to do when closed or unloaded
            self.ending_reason = 'killed'
            Show.base_close_or_unload(self)

        elif self.req_next == 'error':
            self.req_next = ''
            # set what to do after closed or unloaded
            self.ending_reason = 'error'
            Show.base_close_or_unload(self)

        # show timeout
        elif self.show_timeout_signal is True:
            self.show_timeout_signal = False
            # what to do when closed or unloaded
            self.ending_reason = 'show-timeout'
            Show.base_close_or_unload(self)

        # used by exit for stopping show from other shows.
        elif self.exit_signal is True:
            self.exit_signal = False
            self.ending_reason = 'exit'
            Show.base_close_or_unload(self)

        # user wants to stop
        elif self.user_stop_signal is True:
            self.user_stop_signal = False
            self.ending_reason = 'user-stop'
            Show.base_close_or_unload(self)

        # user has selected another track
        elif self.next_track_signal is True:
            self.next_track_signal = False
            self.continue_timeout = False

            # home
            if self.next_track_op in ('home'):
                # back to 1 before home
                back_ref = self.path.back_to(self.home_track_ref)
                if back_ref == '':
                    self.mon.err(
                        self, "home - home track not in path: " +
                        self.home_track_ref)
                    self.end(
                        'error', "home - home track not in path: " +
                        self.home_track_ref)
                # play home
                self.next_track_ref = self.home_track_ref
                self.path.append(self.next_track_ref)
                if self.debug: print 'Executed Home ' + self.path.pretty_path()

            # return-by
            elif self.next_track_op in ('return-by'):
                if self.current_track_ref != self.home_track_ref:
                    # back n stopping at home
                    # back one more and return it
                    back_ref = self.path.back_by(self.home_track_ref,
                                                 self.next_track_arg)
                    # use returned track
                    self.next_track_ref = back_ref
                    self.path.append(self.next_track_ref)
                    if self.debug:
                        print 'Executed Return By' + self.next_track_arg + self.path.pretty_path(
                        )

            # repeat is return by 1
            elif self.next_track_op in ('repeat'):
                # print 'current', self.current_track_ref
                # print 'home', self.home_track_ref
                self.path.pop_for_sibling()
                self.next_track_ref = self.current_track_ref
                self.path.append(self.current_track_ref)
                self.continue_timeout = True
                if self.debug:
                    print 'Executed Repeat ' + self.path.pretty_path()

            # return-to
            elif self.next_track_op in ('return-to'):
                # back to one before return-to track
                back_ref = self.path.back_to(self.next_track_arg)
                if back_ref == '':
                    self.mon.err(
                        self, "return-to - track not in path: " +
                        self.next_track_arg)
                    self.end(
                        'error', "return-to - track not in path: " +
                        self.next_track_arg)
                # and append the return to track
                self.next_track_ref = self.next_track_arg
                self.path.append(self.next_track_ref)
                if self.debug:
                    print 'Executed Return To' + self.next_track_arg + self.path.pretty_path(
                    )

            # call
            elif self.next_track_op in ('call'):
                # append the required track
                self.path.append(self.next_track_arg)
                self.next_track_ref = self.next_track_arg
                if self.debug:
                    print 'Executed Call ' + self.next_track_arg + self.path.pretty_path(
                    )

            # goto
            elif self.next_track_op in ('goto'):
                self.path.empty()
                # add the goto track
                self.next_track_ref = self.next_track_arg
                self.path.append(self.next_track_arg)
                if self.debug:
                    print 'Executed Goto ' + self.next_track_arg + self.path.pretty_path(
                    )

            # jump
            elif self.next_track_op in ('jump'):
                # back to home and remove it
                back_ref = self.path.back_to(self.home_track_ref)
                if back_ref == '':
                    self.mon.err(
                        self, "jump - home track not in path: " +
                        self.home_track_ref)
                    self.end(
                        'error',
                        "jump - track not in path: " + self.home_track_ref)
                # add back the home track without playing it
                self.path.append(self.home_track_ref)
                # append the jumped to track
                self.next_track_ref = self.next_track_arg
                self.path.append(self.next_track_ref)
                if self.debug:
                    print 'Executed Jump ' + self.next_track_arg + self.path.pretty_path(
                    )

            else:
                self.mon.err(
                    self, "unaddressed what next: " + self.next_track_op +
                    ' ' + self.next_track_arg)
                self.end(
                    'error', "unaddressed what next: " + self.next_track_op +
                    ' ' + self.next_track_arg)

            self.current_track_ref = self.next_track_ref
            index = self.medialist.index_of_track(self.next_track_ref)
            if index >= 0:
                Show.write_stats(self, self.next_track_op, self.show_params,
                                 self.medialist.track(index))
                # don't use select the track as not using selected_track in hyperlinkshow
                self.start_load_show_loop(self.medialist.track(index))

            else:
                self.mon.err(
                    self, "next-track not found in medialist: " +
                    self.next_track_ref)
                self.end(
                    'error', "next track not found in medialist: " +
                    self.next_track_ref)

        else:
            # track ends naturally look to see if there is a pp-onend link
            found, link_op, link_arg = self.path.find_link(
                'pp-onend', self.links)
            if found is True:
                if link_op == 'exit':
                    self.user_stop_signal = True
                    self.current_player.input_pressed('stop')
                    self.what_next_after_showing()
                elif link_op == 'home':
                    self.decode_home()
                    self.what_next_after_showing()
                elif link_op == 'return':
                    self.decode_return(link_arg)
                    self.what_next_after_showing()
                elif link_op == 'call':
                    self.decode_call(link_arg)
                    self.what_next_after_showing()
                elif link_op == 'goto':
                    self.decode_goto(link_arg)
                    self.what_next_after_showing()
                elif link_op == 'jump':
                    self.decode_jump(link_arg)
                    self.what_next_after_showing()
                elif link_op == 'repeat':
                    self.decode_repeat()
                    self.what_next_after_showing()
                else:
                    self.mon.err(
                        self, "unknown link command for pp_onend: " + link_op)
                    self.end('error',
                             "unkown link command for pp-onend: " + link_op)
            else:
                if self.show_params['disable-controls'] != 'yes':
                    self.mon.err(
                        self, "pp-onend for this track not found: " + link_op)
                    self.end('error',
                             "pp-onend for this track not found: " + link_op)

##            else:
##                # returning from subshow or a track that does not have pp-onend
##                self.next_track_op='return-by'
##                self,next_track_arg='1'
##                print 'subshow finishes or no on-end'
##                self.what_next_after_showing()

    def track_ready_callback(self, enable_show_background):
        # called from a Player when ready to play, merge the links from the track with those from the show
        # and then enable the click areas
        self.delete_eggtimer()

        if self.show_params['disable-controls'] == 'yes':
            track_links = []
        else:
            links_text = self.current_player.get_links()
            reason, message, track_links = self.path.parse_links(
                links_text, self.allowed_links)
            if reason == 'error':
                self.mon.err(
                    self, message + " in track: " +
                    self.current_player.track_params['track-ref'])
                self.req_next = 'error'
                self.what_next_after_showing()

        self.path.merge_links(self.links, track_links)

        # enable the click-area that are in the list of links
        self.sr.enable_click_areas(self.links)

        Show.base_track_ready_callback(self, enable_show_background)

    # callback from begining of a subshow, provide previous shower and player to called show
    def subshow_ready_callback(self):
        return Show.base_subshow_ready_callback(self)

# *********************
# End the show
# *********************
# finish the player for killing, error or normally
# this may be called directly sub/child shows or players are not running
# if they might be running then need to call terminate.

    def end(self, reason, message):
        self.mon.log(self,
                     "Ending hyperlinkshow: " + self.show_params['show-ref'])
        self.stop_timers()
        Show.base_end(self, reason, message)

    def stop_timers(self):
        pass
class RadioButtonShow(Show):
    """
        starts at 'first-track' which can be any type of track or a show
        The show has links of the form 'symbolic-name play track-ref'
        An event with the symbolic-name will play the referenced track,
        at the end of that track control will return to first-track
        links in the tracks are ignored. Links are inherited from the show.
        timeout returns to first-track

        interface:
        * __init__ - initlialises the show
         * play - selects the first track to play (first-track) 
         * input_pressed,  - receives user events passes them to a Shower/Player if a track is playing,
                otherwise actions them depending on the symbolic name supplied
        *exit  - exits the show from another show, time of day scheduler or external
        * terminate  - aborts the show, used whan clsing or after errors
        * track_ready_callback - called by the next track to be played to remove the previous track from display
        * subshow_ready_callback - called by the subshow to get the last track of the parent show
        * subshow_ended_callback - called at the start of a parent show to get the last track of the subshow
        
    """
    def __init__(self, show_id, show_params, root, canvas, showlist, pp_dir,
                 pp_home, pp_profile, command_callback):
        """
            show_id - index of the top level show caling this (for debug only)
            show_params - dictionary section for the menu
            canvas - the canvas that the menu is to be written on
            showlist  - the showlist
            pp_dir - Pi Presents directory
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory
        """

        # init the common bits
        Show.base__init__(self, show_id, show_params, root, canvas, showlist,
                          pp_dir, pp_home, pp_profile, command_callback)

        # instatiatate the screen driver - used only to access enable and hide click areas
        self.sr = ScreenDriver()

        # create an instance of PathManager -  only used to parse the links.
        self.path = PathManager()

        self.allowed_links = ('play', 'pause', 'exit', 'return', 'null',
                              'no-command', 'stop', 'pause-on', 'pause-off',
                              'mute', 'unmute', 'go')
        # init variables
        self.links = []
        self.track_timeout_timer = None
        self.show_timeout_timer = None
        self.next_track_signal = False
        self.current_track_ref = ''
        self.req_next = ''

    def play(self, end_callback, show_ready_callback, parent_kickback_signal,
             level, controls_list):
        """ starts the hyperlink show at start-track 
              end_callback - function to be called when the show exits
              show_ready_callback - callback to get the previous track
              level is 0 when the show is top level (run from [start] or from show control)
              parent_kickback_signal  - not used other than it being passed to a show
        """
        # need to instantiate the medialist here as in gapshow done in derived class
        self.medialist = MediaList('ordered')

        Show.base_play(self, end_callback, show_ready_callback,
                       parent_kickback_signal, level, controls_list)

        self.mon.trace(self, self.show_params['show-ref'])

        #parse the show and track timeouts
        reason, message, self.show_timeout = Show.calculate_duration(
            self, self.show_params['show-timeout'])
        if reason == 'error':
            self.mon.err(
                self, 'Show Timeout has bad time: ' +
                self.show_params['show-timeout'])
            self.end(
                'error',
                'show timeout, bad time: ' + self.show_params['show-timeout'])

        reason, message, self.track_timeout = Show.calculate_duration(
            self, self.show_params['track-timeout'])
        if reason == 'error':
            self.mon.err(
                self, 'Track Timeout has bad time: ' +
                self.show_params['track-timeout'])
            self.end(
                'error', 'track timeout, bad time: ' +
                self.show_params['track-timeout'])

        # and delete eggtimer
        if self.previous_shower is not None:
            self.previous_shower.delete_eggtimer()

        self.do_first_track()

# ********************************
# Respond to external events
# ********************************

# exit received from another concurrent show

    def exit(self):
        self.stop_timers()
        Show.base_exit(self)

    #  show timeout happened
    def show_timeout_stop(self):
        self.stop_timers()
        Show.base_show_timeout_stop(self)

    # terminate Pi Presents
    def terminate(self):
        self.stop_timers()
        Show.base_terminate(self)

# respond to inputs

    def handle_input_event(self, symbol):
        if self.show_params['controls-in-subshows'] == 'yes':
            Show.base_handle_input_event(self, symbol)
        else:
            self.handle_input_event_this_show(symbol)

    def handle_input_event_this_show(self, symbol):
        # for radiobuttonshow the symbolic names are links to play tracks, also a limited number of in-track operations
        # find the first entry in links that matches the symbol and execute its operation
        self.mon.log(
            self, self.show_params['show-ref'] + ' Show Id: ' +
            str(self.show_id) + ": received input event: " + symbol)

        # show control events
        self.handle_show_control_event(symbol, self.show_control_controls)

        # print 'radiobuttonshow ',symbol
        found, link_op, link_arg = self.path.find_link(symbol, self.links)
        # print 'input event',symbol,link_op
        if found is True:
            if link_op == 'play':
                self.do_play(link_arg)

            elif link_op == 'exit':
                #exit the show
                self.exit()

            elif link_op == 'stop':
                self.stop_timers()
                if self.current_player is not None:
                    if self.current_track_ref == self.first_track_ref and self.level != 0:
                        # if quiescent then set signal to stop the show when track has stopped
                        self.user_stop_signal = True
                    self.current_player.input_pressed('stop')

            elif link_op == 'return':
                # return to the first track
                if self.current_track_ref != self.first_track_ref:
                    self.do_play(self.first_track_ref)

            # in-track operations
            elif link_op in ('pause', 'pause-on', 'pause-off', 'mute',
                             'unmute', 'go'):
                if self.current_player is not None:
                    self.current_player.input_pressed(link_op)

            elif link_op in ('no-command', 'null'):
                return

            elif link_op[0:4] == 'omx-' or link_op[0:6] == 'mplay-' or link_op[
                    0:5] == 'uzbl-':
                if self.current_player is not None:
                    self.current_player.input_pressed(link_op)

            else:
                self.mon.err(self, "unknown link command: " + link_op)
                self.end('error', "unknown link command: " + link_op)

# *********************
# INTERNAL FUNCTIONS
# ********************

# *********************
# Show Sequencer
# *********************

    def track_timeout_callback(self):
        self.do_play(self.first_track_ref)

    def do_play(self, track_ref):
        # if track_ref != self.current_track_ref:
        # cancel the show timeout when playing another track
        if self.show_timeout_timer is not None:
            self.canvas.after_cancel(self.show_timeout_timer)
            self.show_timeout_timer = None
        # print '\n NEED NEXT TRACK'
        self.next_track_signal = True
        self.next_track_op = 'play'
        self.next_track_arg = track_ref
        if self.shower is not None:
            # print 'current_shower not none so stopping',self.mon.id(self.current_shower)
            self.shower.do_operation('stop')
        elif self.current_player is not None:
            # print 'current_player not none so stopping',self.mon.id(self.current_player), ' for' ,track_ref
            self.current_player.input_pressed('stop')
        else:
            return

    def do_first_track(self):
        # get first-track from profile
        self.first_track_ref = self.show_params['first-track-ref']
        if self.first_track_ref == '':
            self.mon.err(self, "first-track is blank: ")
            self.end('error', "first track is blank: ")

        # find the track-ref in the medialisst
        index = self.medialist.index_of_track(self.first_track_ref)
        if index >= 0:
            # don't use select the track as not using selected_track in radiobuttonshow
            self.current_track_ref = self.first_track_ref
            # start the show timer when displaying the first track
            if self.show_timeout_timer is not None:
                self.canvas.after_cancel(self.show_timeout_timer)
                self.show_timeout_timer = None
            if self.show_timeout != 0:
                self.show_timeout_timer = self.canvas.after(
                    self.show_timeout * 1000, self.show_timeout_stop)
            # print 'do first track',self.current_track_ref
            # and load it
            self.start_load_show_loop(self.medialist.track(index))
        else:
            self.mon.err(
                self, "first-track not found in medialist: " +
                self.show_params['first-track-ref'])
            self.end(
                'error', "first track not found in medialist: " +
                self.show_params['first-track-ref'])

# *********************
# Playing show or track
# *********************

    def start_load_show_loop(self, selected_track):
        # shuffle players
        Show.base_shuffle(self)
        # print '\nSHUFFLED previous is', self.mon.id(self.previous_player)
        self.mon.trace(self, '')

        self.display_eggtimer()

        if self.track_timeout_timer is not None:
            self.canvas.after_cancel(self.track_timeout_timer)
            self.track_timeout_timer = None

        # start timeout for the track if required
        if self.current_track_ref != self.first_track_ref and self.track_timeout != 0:
            self.track_timeout_timer = self.canvas.after(
                self.track_timeout * 1000, self.track_timeout_callback)

        # read the show links. Track links will  be added by ready_callback
        # needs to be done in show loop as each track adds different links to the show links
        if self.show_params['disable-controls'] == 'yes':
            self.links = []
        else:
            reason, message, self.links = self.path.parse_links(
                self.show_params['links'], self.allowed_links)
            if reason == 'error':
                self.mon.err(self, message + " in show")
                self.end('error', message + " in show")

        # load the track or show
        # params - track,, track loaded callback, end eshoer callback,enable_menu
        Show.base_load_track_or_show(self, selected_track,
                                     self.what_next_after_load,
                                     self.end_shower, False)

# track has loaded so show it.

    def what_next_after_load(self, status, message):
        self.mon.trace(
            self,
            'load complete with status: ' + status + '  message: ' + message)
        # print 'LOADED TRACK  ',self.mon.id(self.current_player)
        if self.current_player.play_state == 'load-failed':
            self.req_next = 'error'
            self.what_next_after_showing()
        else:
            if self.show_timeout_signal is True or self.terminate_signal is True or self.exit_signal is True or self.user_stop_signal is True:
                # print 'after load - what next'
                self.what_next_after_showing()
            else:
                self.mon.trace(self, '- showing track')
                self.current_player.show(self.track_ready_callback,
                                         self.finished_showing,
                                         self.closed_after_showing)

    def finished_showing(self, reason, message):
        # showing has finished with 'pause at end', showing the next track will close it after next has started showing
        self.mon.trace(self, ' - pause at end')
        self.mon.log(
            self, "pause at end of showing track with reason: " + reason +
            ' and message: ' + message)
        self.sr.hide_click_areas(self.links, self.canvas)
        if self.current_player.play_state == 'show-failed':
            self.req_next = 'error'
        else:
            self.req_next = 'finished-player'
        # print 'finished showing ',self.mon.id(self.current_player),' from state ',self.current_player.play_state
        self.what_next_after_showing()

    def closed_after_showing(self, reason, message):
        # showing has finished with closing of player but track instance is alive for hiding the x_content
        self.mon.trace(self, '- closed after showing')
        self.mon.log(
            self, "Closed after showing track with reason: " + reason +
            ' and message: ' + message)
        self.sr.hide_click_areas(self.links, self.canvas)
        if self.current_player.play_state == 'show-failed':
            self.req_next = 'error'
        else:
            self.req_next = 'closed-player'
        # print 'closed showing',self.mon.id(self.current_player),' from state ',self.current_player.play_state
        self.what_next_after_showing()

    # subshow or child show has ended
    def end_shower(self, show_id, reason, message):
        self.mon.log(
            self, self.show_params['show-ref'] + ' ' + str(self.show_id) +
            ': Returned from shower with ' + reason + ' ' + message)
        self.sr.hide_click_areas(self.links, self.canvas)
        self.req_next = reason
        Show.base_end_shower(self)
        # print 'end shower - wha-next'
        self.what_next_after_showing()

    def what_next_after_showing(self):
        self.mon.trace(self, '')
        # print 'WHAT NEXT AFTER SHOWING'
        # print 'current is',self.mon.id(self.current_player), '  next track signal ',self.next_track_signal
        # need to terminate
        if self.terminate_signal is True:
            self.terminate_signal = False
            # what to do when closed or unloaded
            self.ending_reason = 'killed'
            Show.base_close_or_unload(self)

        elif self.req_next == 'error':
            self.req_next = ''
            # set what to do after closed or unloaded
            self.ending_reason = 'error'
            Show.base_close_or_unload(self)

        # show timeout
        elif self.show_timeout_signal is True:
            self.show_timeout_signal = False
            # what to do when closed or unloaded
            self.ending_reason = 'show-timeout'
            Show.base_close_or_unload(self)

        # used by exit for stopping show from other shows.
        elif self.exit_signal is True:
            self.exit_signal = False
            self.ending_reason = 'exit'
            Show.base_close_or_unload(self)

        # user wants to stop
        elif self.user_stop_signal is True:
            self.user_stop_signal = False
            self.ending_reason = 'user-stop'
            Show.base_close_or_unload(self)

        # user has selected another track
        elif self.next_track_signal is True:
            self.next_track_signal = False
            self.current_track_ref = self.next_track_arg
            # print 'what next - next track signal is True so load ', self.current_track_ref
            index = self.medialist.index_of_track(self.current_track_ref)
            if index >= 0:
                # don't use select the track as not using selected_track in radiobuttonshow
                # and load it
                Show.write_stats(self, 'play', self.show_params,
                                 self.medialist.track(index))
                self.start_load_show_loop(self.medialist.track(index))
            else:
                self.mon.err(
                    self, "track reference not found in medialist: " +
                    self.current_track_ref)
                self.end(
                    'error', "track reference not found in medialist: " +
                    self.current_track_ref)

        else:
            # track ends naturally or is quit so go back to first track
            # print 'what next - natural end  so do first track'
            self.do_first_track()

# *********************
# Interface with other shows/players to reduce black gaps
# *********************

# called just before a track is shown to remove the  previous track from the screen
# and if necessary close it

    def track_ready_callback(self, enable_show_background):
        self.delete_eggtimer()
        # print 'TRACK READY CALLBACK'
        # print 'previous is',self.mon.id(self.previous_player), self.next_track_signal

        #merge links from the track
        if self.show_params['disable-controls'] == 'yes':
            track_links = []
        else:
            reason, message, track_links = self.path.parse_links(
                self.current_player.get_links(), self.allowed_links)
            if reason == 'error':
                self.mon.err(
                    self, message + " in track: " +
                    self.current_player.track_params['track-ref'])
                self.req_next = 'error'
                self.what_next_after_showing()
        self.path.merge_links(self.links, track_links)
        # enable the click-area that are in the list of links
        self.sr.enable_click_areas(self.links, self.canvas)

        Show.base_track_ready_callback(self, enable_show_background)

    # callback from begining of a subshow, provide previous shower player to called show
    def subshow_ready_callback(self):
        return Show.base_subshow_ready_callback(self)

# *********************
# End the show
# *********************

    def end(self, reason, message):
        self.stop_timers()
        Show.base_end(self, reason, message)

    def stop_timers(self):
        if self.show_timeout_timer is not None:
            self.canvas.after_cancel(self.show_timeout_timer)
            self.show_timeout_timer = None
        if self.track_timeout_timer is not None:
            self.canvas.after_cancel(self.track_timeout_timer)
            self.track_timeout_timer = None
Esempio n. 4
0
class MenuShow(Show):
    def __init__(self, show_id, show_params, root, canvas, showlist, pp_dir,
                 pp_home, pp_profile, command_callback):
        """
            show_id - index of the top level show caling this (for debug only)
            show_params - dictionary section for the menu
            canvas - the canvas that the menu is to be written on
            showlist  - the showlist
            pp_dir - Pi Presents directory
            pp_home - Pi presents data_home directory
            pp_profile - Pi presents profile directory
        """

        # init the common bits
        Show.base__init__(self, show_id, show_params, root, canvas, showlist,
                          pp_dir, pp_home, pp_profile, command_callback)

        # instatiatate the screen driver - used only to access enable and hide click areas
        self.sr = ScreenDriver()

        self.controlsmanager = ControlsManager()

        # init variables
        self.show_timeout_timer = None
        self.track_timeout_timer = None
        self.next_track_signal = False
        self.next_track = None
        self.menu_index = 0
        self.menu_showing = True
        self.req_next = ''
        self.last_menu_index = 0
        self.return_to_zero = False

    def play(self, end_callback, show_ready_callback, parent_kickback_signal,
             level, controls_list):
        """ displays the menu 
              end_callback - function to be called when the menu exits
              show_ready_callback - callback when menu is ready to display (not used)
              level is 0 when the show is top level (run from [start] or from show control)
              parent_kickback_signal  - not used other than it being passed to a show
        """
        # need to instantiate the medialist here as not using gapshow
        self.medialist = MediaList('ordered')

        Show.base_play(self, end_callback, show_ready_callback,
                       parent_kickback_signal, level, controls_list)

        self.mon.trace(self, self.show_params['show-ref'])

        #parse the show and track timeouts
        reason, message, self.show_timeout = Show.calculate_duration(
            self, self.show_params['show-timeout'])
        if reason == 'error':
            self.mon.err(
                self, 'Show Timeout has bad time: ' +
                self.show_params['show-timeout'])
            self.end(
                'error',
                'show timeout, bad time: ' + self.show_params['show-timeout'])

        reason, message, self.track_timeout = Show.calculate_duration(
            self, self.show_params['track-timeout'])
        if reason == 'error':
            self.mon.err(
                self, 'Track Timeout has bad time: ' +
                self.show_params['track-timeout'])
            self.end(
                'error', 'track timeout, bad time: ' +
                self.show_params['track-timeout'])

        # and delete eggtimer
        if self.previous_shower is not None:
            self.previous_shower.delete_eggtimer()

        # and display the menu
        self.do_menu_track()

# ********************
# respond to inputs.
# ********************

# exit received from another concurrent show

    def exit(self):
        self.stop_timers()
        Show.base_exit(self)

    #  show timeout happened
    def show_timeout_stop(self):
        self.stop_timers()
        Show.base_show_timeout_stop(self)

    # terminate Pi Presents
    def terminate(self):
        self.stop_timers()
        Show.base_terminate(self)

    def handle_input_event(self, symbol):
        Show.base_handle_input_event(self, symbol)

    def handle_input_event_this_show(self, symbol):

        self.handle_show_control_event(symbol, self.show_control_controls)

        # menushow has only internal operation
        operation = self.base_lookup_control(symbol, self.controls_list)
        self.do_operation(operation)

    def do_operation(self, operation):
        # service the standard inputs for this show

        self.mon.trace(self, operation)
        if operation == 'exit':
            self.exit()

        elif operation == 'stop':
            self.stop_timers()
            if self.current_player is not None:
                if self.menu_showing is True and self.level != 0:
                    # if quiescent then set signal to stop the show when track has stopped
                    self.user_stop_signal = True
                self.current_player.input_pressed('stop')

        elif operation in ('up', 'down'):
            # stop show timeout
            if self.show_timeout_timer is not None:
                self.canvas.after_cancel(self.show_timeout_timer)
                # and start it again
                if self.show_timeout != 0:
                    self.show_timeout_timer = self.canvas.after(
                        self.show_timeout * 1000, self.show_timeout_stop)
            if operation == 'up':
                self.previous()
            else:
                next(self)

        elif operation == 'play':
            self.next_track_signal = True
            st = self.medialist.select_anon_by_index(self.menu_index)
            self.next_track = self.medialist.selected_track()
            self.current_player.stop()

            # cancel show timeout
            if self.show_timeout_timer is not None:
                self.canvas.after_cancel(self.show_timeout_timer)
                self.show_timeout_timer = None

            # stop current track (the menuplayer) if running or just start the next track
            if self.current_player is not None:
                self.current_player.input_pressed('stop')
            else:
                self.what_next_after_showing()

        elif operation in ('no-command', 'null'):
            return

        elif operation in ('pause', 'pause-on', 'pause-off', 'mute', 'unmute',
                           'go'):
            if self.current_player is not None:
                self.current_player.input_pressed(operation)

        elif operation[0:4] == 'omx-' or operation[
                0:6] == 'mplay-' or operation[0:5] == 'uzbl-':
            if self.current_player is not None:
                self.current_player.input_pressed(operation)

    def __next__(self):
        # remove highlight
        if self.current_player.__class__.__name__ == 'MenuPlayer':
            self.current_player.highlight_menu_entry(self.menu_index, False)
            self.medialist.next('ordered')
            if self.menu_index == self.menu_length - 1:
                self.menu_index = 0
            else:
                self.menu_index += 1
            # and highlight the new entry
            self.current_player.highlight_menu_entry(self.menu_index, True)

    def previous(self):
        # remove highlight
        if self.current_player.__class__.__name__ == 'MenuPlayer':
            self.current_player.highlight_menu_entry(self.menu_index, False)
            if self.menu_index == 0:
                self.menu_index = self.menu_length - 1
            else:
                self.menu_index -= 1
            self.medialist.previous('ordered')
            # and highlight the new entry
            self.current_player.highlight_menu_entry(self.menu_index, True)

# *********************
# Sequencing
# *********************

    def track_timeout_callback(self):
        self.do_operation('stop')

    def do_menu_track(self):
        self.menu_showing = True
        self.mon.trace(self, '')
        # start show timeout alarm if required
        if self.show_timeout != 0:
            self.show_timeout_timer = self.canvas.after(
                self.show_timeout * 1000, self.show_timeout_stop)

        index = self.medialist.index_of_track(
            self.show_params['menu-track-ref'])
        if index == -1:
            self.mon.err(
                self, "'menu-track' not in medialist: " +
                self.show_params['menu-track-ref'])
            self.end(
                'error', "menu-track not in medialist: " +
                self.show_params['menu-track-ref'])
            return

        #make the medialist available to the menuplayer for cursor scrolling
        self.show_params['medialist_obj'] = self.medialist

        # load the menu track
        self.start_load_show_loop(self.medialist.track(index))

# *********************
# Playing show or track loop
# *********************

    def start_load_show_loop(self, selected_track):
        # shuffle players
        Show.base_shuffle(self)
        self.mon.trace(self, '')
        self.display_eggtimer()

        # get control bindings for this show
        # needs to be done for each track as track can override the show controls
        if self.show_params['disable-controls'] == 'yes':
            self.controls_list = []
        else:
            reason, message, self.controls_list = self.controlsmanager.get_controls(
                self.show_params['controls'])
            if reason == 'error':
                self.mon.err(self, message)
                self.end('error', "error in controls: " + message)
                return

        # load the track or show
        Show.base_load_track_or_show(self, selected_track,
                                     self.what_next_after_load,
                                     self.end_shower, False)

# track has loaded (menu or otherwise) so show it.

    def what_next_after_load(self, status, message):

        if self.current_player.play_state == 'load-failed':
            self.req_next = 'error'
            self.what_next_after_showing()

        else:
            # get the calculated length of the menu for the loaded menu track
            if self.current_player.__class__.__name__ == 'MenuPlayer':
                if self.medialist.anon_length() == 0:
                    self.req_next = 'error'
                    self.what_next_after_showing()
                    return

                #highlight either first or returning entry and select appropiate medialist entry
                if self.return_to_zero is True:
                    # init the index used to hiighlight the selected menu entry by menuplayer
                    self.menu_index = 0
                    # print 'initial index',self.menu_index
                else:
                    self.menu_index = self.last_menu_index
                    # print ' return to last ',self.menu_index

                self.menu_length = self.current_player.menu_length
                self.current_player.highlight_menu_entry(self.menu_index, True)
            self.mon.trace(
                self, ' - load complete with status: ' + status +
                '  message: ' + message)

            if self.show_timeout_signal is True or self.terminate_signal is True or self.exit_signal is True or self.user_stop_signal is True:
                self.what_next_after_showing()
            else:
                self.mon.trace(self, '')
                self.current_player.show(self.track_ready_callback,
                                         self.finished_showing,
                                         self.closed_after_showing)

    def finished_showing(self, reason, message):
        # showing has finished with 'pause at end', showing the next track will close it after next has started showing
        self.mon.trace(self, '')
        self.mon.log(
            self, "pause at end of showing track with reason: " + reason +
            ' and message: ' + message)
        self.sr.hide_click_areas(self.controls_list, self.canvas)
        if self.current_player.play_state == 'show-failed':
            self.req_next = 'error'
        else:
            self.req_next = 'finished-player'
        self.what_next_after_showing()

    def closed_after_showing(self, reason, message):
        # showing has finished with closing of player but track instance is alive for hiding the x_content
        self.mon.trace(self, '')
        self.mon.log(
            self, "Closed after showing track with reason: " + reason +
            ' and message: ' + message)
        self.sr.hide_click_areas(self.controls_list, self.canvas)
        if self.current_player.play_state == 'show-failed':
            self.req_next = 'error'
        else:
            self.req_next = 'closed-player'
        self.what_next_after_showing()

    # subshow or child show has ended
    def end_shower(self, show_id, reason, message):
        self.mon.log(
            self, self.show_params['show-ref'] + ' ' + str(self.show_id) +
            ': Returned from shower with ' + reason + ' ' + message)
        self.sr.hide_click_areas(self.controls_list, self.canvas)
        self.req_next = reason
        Show.base_end_shower(self)
        self.what_next_after_showing()

    # at the end of a track check for terminations else re-display the menu
    def what_next_after_showing(self):
        self.mon.trace(self, '')
        # cancel track timeout timer
        if self.track_timeout_timer is not None:
            self.canvas.after_cancel(self.track_timeout_timer)
            self.track_timeout_timer = None

        # need to terminate?
        if self.terminate_signal is True:
            self.terminate_signal = False
            # set what to do when closed or unloaded
            self.ending_reason = 'killed'
            Show.base_close_or_unload(self)

        elif self.req_next == 'error':
            self.req_next = ''
            # set what to do after closed or unloaded
            self.ending_reason = 'error'
            Show.base_close_or_unload(self)

        # show timeout
        elif self.show_timeout_signal is True:
            self.show_timeout_signal = False
            # set what to do when closed or unloaded
            self.ending_reason = 'show-timeout'
            Show.base_close_or_unload(self)

        # used by exit for stopping show from other shows.
        elif self.exit_signal is True:
            self.exit_signal = False
            self.ending_reason = 'exit'
            Show.base_close_or_unload(self)

        # user wants to stop
        elif self.user_stop_signal is True:
            self.user_stop_signal = False
            self.ending_reason = 'user-stop'
            Show.base_close_or_unload(self)

        elif self.next_track_signal is True:
            self.next_track_signal = False
            self.menu_showing = False
            # start timeout for the track if required
            if self.track_timeout != 0:
                self.track_timeout_timer = self.canvas.after(
                    self.track_timeout * 1000, self.track_timeout_callback)
            self.last_menu_index = self.menu_index
            Show.write_stats(self, 'play', self.show_params, self.next_track)
            self.start_load_show_loop(self.next_track)

        else:
            # no stopping the show required so re-display the menu
            self.do_menu_track()

# *********************
# Interface with other shows/players to reduce black gaps
# *********************

# called just before a track is shown to remove the  previous track from the screen
# and if necessary close it

    def track_ready_callback(self, enable_show_background):
        self.delete_eggtimer()

        if self.show_params['disable-controls'] != 'yes':
            #merge controls from the track
            controls_text = self.current_player.get_links()
            reason, message, track_controls = self.controlsmanager.parse_controls(
                controls_text)
            if reason == 'error':
                self.mon.err(
                    self, message + " in track: " +
                    self.current_player.track_params['track-ref'])
                self.req_next = 'error'
                self.what_next_after_showing()
            self.controlsmanager.merge_controls(self.controls_list,
                                                track_controls)

        self.sr.enable_click_areas(self.controls_list, self.canvas)

        Show.base_track_ready_callback(self, enable_show_background)

    # callback from begining of a subshow, provide previous shower player to called show
    def subshow_ready_callback(self):
        return Show.base_subshow_ready_callback(self)

# *********************
# Ending the show
# *********************

    def end(self, reason, message):
        self.stop_timers()
        Show.base_end(self, reason, message)

    def stop_timers(self):
        if self.track_timeout_timer is not None:
            self.canvas.after_cancel(self.track_timeout_timer)
            self.track_timeout_timer = None
        if self.show_timeout_timer is not None:
            self.canvas.after_cancel(self.show_timeout_timer)
            self.show_timeout_timer = None