Hi!

(I don't know why I always have problems with this list; this is my third try 
to get this mail through..) As promised, I hereby send a patch which tries to 
integrate nvram-wakeup into Freevo. This patch is not yet well-tested, and 
one problem I already stumbled over is that it is still possible to shutdown 
the computer too short before a recording, so that the wakeup-time lies in 
the past and no recording happens. I thought that the user should be warned 
about this (some customizable warning is already contained in the patch) but 
should not be forbidden to do so. For recordings at night for example, the 
attached patch should work.

Some other thoughts on this patch:

- nvram-wakeup is only called if the whole system is shutdown via Freevo's
  main menu.  Maybe this should be done on Freevo shutdown, too?  (For people
  using an external shutdown)

- If CONFIRM_SHUTDOWN is set, there is a dialog box anyways, so this is
  padded with some additional information in case of a scheduled recording.
  Also, the WARN_SHUTDOWN option has no effect in this case. (Ideally, I
  would like an AlertBox for confirmation if WARN_SHUTDOWN is set and the
  user should be warned, regardless of CONFIRM_SHUTDOWN.)

- Maybe some of the functionality should go into the recordserver itself?
  (Until now, recordserver.py was the only place using TV_RECORD_PADDING)

- It would be nice if Freevo would shutdown again after recording. For this,
  one would need to check that noone is using it, so I thought maybe a
  screensaver-like plugin for the main menu would be cool. For instance,
  after ten minutes without user events, a big nice analog clock appears,
  and if this plugin maintains a flag whether Freevo has been used at all yet,
  this could be used for automatic shutdown after recording. Comments?

Also, I am not sure that I put all code at the right places, but I am willing 
to correct everything if you tell me your opinions (/problems you see). Ah, 
and I did not (yet) increase the config file version, because I was not sure 
about the policy behind that (the options all have sane defaults), whether 
it's for important options or also for the announcement of new features (or 
only for releases)?

Ciao, /  /                                                    .o.
     /--/                                                     ..o
    /  / ANS                                                  ooo
Index: freevo_config.py
===================================================================
RCS file: /cvsroot/freevo/freevo/freevo_config.py,v
retrieving revision 1.340
diff -u -3 -p -d -b -r1.340 freevo_config.py
--- freevo_config.py	10 Jun 2004 02:32:16 -0000	1.340
+++ freevo_config.py	13 Jun 2004 17:51:05 -0000
@@ -285,2 +285,2 @@ RESTART_SYS_CMD  = 'shutdown -r now'  #
 ENABLE_SHUTDOWN_SYS = 0  # Performs a whole system shutdown at SHUTDOWN!
                          # For standalone boxes.

+# warn before shutting down the system if the next recording is
+# scheduled for starting in less than this number of seconds:
+WARN_SHUTDOWN = 15 * 60
+
+# on Freevo shutdown, use nvram-wakeup to program the computer to
+# wakeup / boot up right before the next recording:
+USE_NVRAM_WAKEUP = 0
+
+NVRAM_WAKEUP_CMD = 'nvram-wakeup -s %d' # see SHUTDOWN_SYS_CMD for sudo comment
+
+# does the computer's RTC store local or UTC time?
+# None means autodetect (default), True(/False) means RTC is (not) UTC
+NVRAM_RTC_IN_UTC = None
+
+# if using nvram-wakeup, time in seconds to startup before recording:
+BOOTTIME_PADDING = 150

 #
 # You can add more keybindings by adding them to the correct hash.
Index: local_conf.py.example
===================================================================
RCS file: /cvsroot/freevo/freevo/local_conf.py.example,v
retrieving revision 1.34
diff -u -3 -p -d -b -r1.34 local_conf.py.example
--- local_conf.py.example	3 Jun 2004 18:23:31 -0000	1.34
+++ local_conf.py.example	13 Jun 2004 17:51:06 -0000
@@ -777,2 +777,2 @@ IMAGEVIEWER_BLEND_MODE = -1
 # the percision of one minute.
 # TV_RECORD_PADDING = 0 * 60

+# warn before shutting down the system if the next recording is
+# scheduled for starting in less than this number of seconds:
+# WARN_SHUTDOWN = 15 * 60 # quarter hour
+# (this has no extra effect if CONFIRM_SHUTDOWN is set, since then
+#  the shutdown is already confirmed)
+
+# on Freevo shutdown, use nvram-wakeup to program the computer to
+# wakeup / boot up right before the next recording:
+# USE_NVRAM_WAKEUP    = 1
+
+# does the computer's RTC store local or UTC time?
+# None means autodetect (default), True(/False) means RTC is (not) UTC
+# NVRAM_RTC_IN_UTC = True
+
+# if using nvram-wakeup, time in seconds to startup before recording:
+# BOOTTIME_PADDING    = 2 * 60
+
 # VCR_AUDIO = (':adevice=%s' % AUDIO_DEVICE +
 #              ':audiorate=32000' +         # 44100 for better sound
 #              ':forceaudio:forcechan=1:' + # Forced mono for bug in my driver
Index: src/plugins/shutdown.py
===================================================================
RCS file: /cvsroot/freevo/freevo/src/plugins/shutdown.py,v
retrieving revision 1.6
diff -u -3 -p -d -b -r1.6 shutdown.py
--- src/plugins/shutdown.py	9 Jun 2004 19:42:08 -0000	1.6
+++ src/plugins/shutdown.py	13 Jun 2004 17:51:06 -0000
@@ -64,2 +64,2 @@ from gui import ConfirmBox
 from item import Item
 from plugin import MainMenuPlugin

+if config.WARN_SHUTDOWN or config.USE_NVRAM_WAKEUP:
+    import tv.record_client as record_client
+

 def shutdown(menuw=None, argshutdown=None, argrestart=None, exit=False):
     """
@@ -140,1 +143,1 @@ def shutdown(menuw=None, argshutdown=Non
         time.sleep(1)


-
 class ShutdownItem(Item):
     """
     Item for shutdown
@@ -160,7 +162,7 @@ class ShutdownItem(Item):
                           (self.confirm_system_restart, _('Restart system') ) ]
         else:
             items = [ (self.shutdown_freevo, _('Shutdown Freevo') ),
-                          (self.shutdown_system, _('Shutdown system') ),
+                      (self.check_shutdown_system, _('Shutdown system') ),
                           (self.shutdown_system_restart, _('Restart system') ) ]
         if config.ENABLE_SHUTDOWN_SYS:
             items = [ items[1], items[0], items[2] ]
@@ -168,1 +170,1 @@ class ShutdownItem(Item):
         return items


+    def next_scheduled_recording(self):
+        """
+        return starting time of next scheduled recording (or None)
+        """
+        (server_available, msg) = record_client.connectionTest()
+        if not server_available:
+            return None
+        (result, recordings) = record_client.getScheduledRecordings()
+        if result:
+            progs = recordings.getProgramList().values()
+            if len(progs):
+                progs.sort(lambda a, b: cmp(a.start, b.start))
+                return progs[0].start
+        return None
+
+
+    def next_recording_message(self, start_time = None):
+        if start_time == None:
+            start_time = self.next_scheduled_recording()
+        if start_time == None:
+            return ""
+        rec_distance = start_time - time.time() - config.TV_RECORD_PADDING
+        if rec_distance < 60*60:
+            return _('The next scheduled recording begins in %d minutes.') % int(rec_distance/60)
+        if rec_distance < 60*60*10:
+            return _('The next scheduled recording begins at %s.') % time.strftime(config.TV_TIMEFORMAT, time.localtime(start_time))
+        return _('The next recording is scheduled for %s.') % time.strftime(config.TV_DATETIMEFORMAT, time.localtime(start_time))
+
+
     def confirm_freevo(self, arg=None, menuw=None):
         """
         Pops up a ConfirmBox.
         """
         self.menuw = menuw
-        what = _('Do you really want to shut down Freevo?')
+        what = _('Do you really want to shut down Freevo? ') \
+               + self.next_recording_message()
         ConfirmBox(text=what, handler=self.shutdown_freevo, default_choice=1).show()


@@ -182,5 +214,6 @@ class ShutdownItem(Item):
         Pops up a ConfirmBox.
         """
         self.menuw = menuw
-        what = _('Do you really want to shut down the system?')
+        what = _('Do you really want to shut down the system? ') \
+               + self.next_recording_message()
         ConfirmBox(text=what, handler=self.shutdown_system, default_choice=1).show()

     def confirm_system_restart(self, arg=None, menuw=None):
@@ -190,5 +223,6 @@ class ShutdownItem(Item):
         Pops up a ConfirmBox.
         """
         self.menuw = menuw
-        what = _('Do you really want to restart the system?')
+        what = _('Do you really want to restart the system? ') \
+               + self.next_recording_message()
         ConfirmBox(text=what, handler=self.shutdown_system_restart, default_choice=1).show()


+    def check_shutdown_system(self, arg=None, menuw=None):
+        """
+        shutdown the complete system if the next recording is not too far away
+        """
+        if config.WARN_SHUTDOWN:
+            start_time = self.next_scheduled_recording()
+            if start_time != None:
+                if start_time - config.TV_RECORD_PADDING - time.time() \
+                       < config.WARN_SHUTDOWN:
+                    what = self.next_recording_message() + \
+                           _('Do you really want to shut down the system?')
+                    ConfirmBox(text=what, handler=self.shutdown_system, default_choice=1).show()
+                    return
+        self.shutdown_system(arg, menuw)
+
+
     def shutdown_freevo(self, arg=None, menuw=None):
         """
         shutdown freevo, don't shutdown the system
@@ -205,4 +255,24 @@ class ShutdownItem(Item):
         """
         shutdown the complete system
         """
+        if config.USE_NVRAM_WAKEUP:
+            if config.NVRAM_RTC_IN_UTC == None:
+                try:
+                    config.NVRAM_RTC_IN_UTC = \
+                        file("/etc/adjtime").readlines()[2].startswith("UTC")
+                except RuntimeError, e:
+                    # TODO: Write warning to console / log that autodetection
+                    # is not possible and NVRAM_RTC_IN_UTC should be set!
+                    config.NVRAM_RTC_IN_UTC = False
+            start_time = self.next_scheduled_recording()
+            if start_time != None:
+                wakeupTime = start_time \
+                             - config.TV_RECORD_PADDING \
+                             - config.BOOTTIME_PADDING
+                if config.NVRAM_RTC_IN_UTC:
+                    if time.localtime(wakeupTime)[8]: # daylight savings time?
+                        wakeupTime -= time.altzone
+                    else:
+                        wakeupTime -= time.timezone
+                os.system(config.NVRAM_WAKEUP_CMD % (wakeupTime,))
         shutdown(menuw=menuw, argshutdown=True, argrestart=False)

     def shutdown_system_restart(self, arg=None, menuw=None):

Reply via email to