# -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------
# autobacktomain.py - go back to main menu after timeout
# -----------------------------------------------------------------------
# $Id: $
#
# Notes:
# Todo:        
#
# -----------------------------------------------------------------------
# $Log: autobacktomain.py,v $
#
# -----------------------------------------------------------------------
# Freevo - A Home Theater PC framework
# Copyright (C) 2003 Krister Lagerstrom, et al. 
# Please see the file freevo/Docs/CREDITS for a complete list of authors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ----------------------------------------------------------------------- */


import time
import config
import plugin
import rc
import event as em

# Set to 1 for debug output
DEBUG = config.DEBUG

TRUE = 1
FALSE = 0

class PluginInterface(plugin.DaemonPlugin):
    """
    A plugin to go back to the main menu after a timeout
    plugin.activate('autobacktomain')
    """
    def __init__(self):
        plugin.DaemonPlugin.__init__(self)
        self.plugin_name = 'SCREENSAVER'
        self.event_listener = TRUE
        self.poll_menu_only = TRUE
        self.last_event = 0
        self.menuw = None
        self.poll_interval = 10 * config.BACK2MAIN_POLL
        self.saver_delay = config.BACK2MAIN_DELAY

    def config(self):
        return [ ('BACK2MAIN_DELAY', 300, '# of seconds to wait to start saver.'),
             ('BACK2MAIN_POLL', 600, '# of seconds to wait between polling.') ]

    def eventhandler(self, event = None, menuw=None, arg=None):
        """
        eventhandler to handle the events. Always return false since we
        are just a listener and really can't send back true.
        """
        _debug_("Back 2 Main saw %s" % event.name)
        if menuw:
            self.menuw = menuw

        # gotta ignore these or video screensavers shutoff before they begin
        ignored_events = ['VIDEO_START', 'PLAY_START', 'VIDEO_END', 'PLAY_END', 'IDENTIFY_MEDIA']
        if event.name in ignored_events: 
            return FALSE

        self.last_event = time.time()

        return FALSE

    def poll(self):
        _debug_("Back 2 Main got polled %f" % time.time())
        if (time.time() - self.last_event) > self.saver_delay :
            rc.post_event(em.MENU_GOTO_MAINMENU)

