]> git.sesse.net Git - vlc/blobdiff - modules/misc/screensaver.c
screensaver: convert to inhibit plugin
[vlc] / modules / misc / screensaver.c
index 5ade8003458e8c1763d5ca92425320ebfbb036f1..057bfe048a2365a6ff58d951c942f12a9af2f63b 100644 (file)
@@ -1,10 +1,11 @@
 /*****************************************************************************
  * screensaver.c : disable screen savers when VLC is playing
  *****************************************************************************
- * Copyright (C) 2003 VideoLAN
+ * Copyright (C) 2006-2009 the VideoLAN team
  * $Id$
  *
  * Authors: Sam Hocevar <sam@zoy.org>
+ *          Benjamin Pracht <bigben AT videolan DOT org>
  *
  * 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
  *
  * 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, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>
 
-#include <vlc/vlc.h>
-#include <vlc/intf.h>
-#include <vlc/aout.h>
-#include <vlc/vout.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_inhibit.h>
+
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <signal.h>
 
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
 static int  Activate     ( vlc_object_t * );
-static void Run          ( intf_thread_t *p_intf );
+static void  Deactivate   ( vlc_object_t * );
+
+static void Timer( void * );
+static void Inhibit( vlc_inhibit_t *, bool );
+
+struct vlc_inhibit_sys
+{
+    vlc_timer_t timer;
+};
+
 
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
-vlc_module_begin();
-    set_description( _("X Screensaver disabler") );
-    set_capability( "interface", 0 );
-    set_callbacks( Activate, NULL );
-vlc_module_end();
+vlc_module_begin ()
+    set_description( N_("X Screensaver disabler") )
+    set_capability( "inhibit", 5 )
+    set_callbacks( Activate, Deactivate )
+vlc_module_end ()
 
 /*****************************************************************************
  * Activate: initialize and create stuff
  *****************************************************************************/
 static int Activate( vlc_object_t *p_this )
 {
-    intf_thread_t *p_intf = (intf_thread_t*)p_this;
+    vlc_inhibit_t *p_ih = (vlc_inhibit_t*)p_this;
+    vlc_inhibit_sys_t *p_sys;
+
+    p_sys = p_ih->p_sys = malloc( sizeof( *p_sys ) );
+    if( !p_sys )
+        return VLC_ENOMEM;
 
-    p_intf->pf_run = Run;
+    if( vlc_timer_create( &p_sys->timer, Timer, p_ih ) )
+    {
+        free( p_sys );
+        return VLC_ENOMEM;
+    }
+    p_ih->inhibit = Inhibit;
 
     return VLC_SUCCESS;
 }
 
 /*****************************************************************************
- * Run: main thread
- *****************************************************************************
- * This part of the module is in a separate thread so that we do not have
- * too much system() overhead.
+ * Deactivate: uninitialize and cleanup
  *****************************************************************************/
-static void Run( intf_thread_t *p_intf )
+static void Deactivate( vlc_object_t *p_this )
 {
-    int i_lastcall = 0;
+    vlc_inhibit_t *p_ih = (vlc_inhibit_t*)p_this;
+    vlc_inhibit_sys_t *p_sys = p_ih->p_sys;
 
-    while( !p_intf->b_die )
-    {
-        msleep( 100000 );
+    vlc_timer_destroy( p_sys->timer );
 
-        /* Check screensaver every 30 seconds */
-        if( ++i_lastcall > 300 )
-        {
-            vlc_object_t *p_vout;
-            p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
-            /* If there is a video output, disable xscreensaver */
-            if( p_vout )
-            {
-                vlc_object_release( p_vout );
+    free( p_sys );
+}
 
-                /* http://www.jwz.org/xscreensaver/faq.html#dvd */
-                system( "xscreensaver-command -deactivate >&- 2>&- &" );
+static void Inhibit( vlc_inhibit_t *p_ih, bool suspend )
+{
+    mtime_t d = suspend ? 30*CLOCK_FREQ : 0;
+    vlc_timer_schedule( p_ih->p_sys->timer, false, d, d );
+}
 
-                /* FIXME: add support for other screensavers */
-            }
+/*****************************************************************************
+ * Execute: Spawns a process using execv()
+ *****************************************************************************/
+static void Execute( vlc_object_t *p_this, const char *const *ppsz_args )
+{
+    pid_t pid = fork();
+    switch( pid )
+    {
+        case 0:     /* we're the child */
+        {
+            sigset_t set;
+            sigemptyset (&set);
+            pthread_sigmask (SIG_SETMASK, &set, NULL);
 
-            i_lastcall = 0;
+            /* We don't want output */
+            if( ( freopen( "/dev/null", "w", stdout ) != NULL )
+             && ( freopen( "/dev/null", "w", stderr ) != NULL ) )
+                execv( ppsz_args[0] , (char *const *)ppsz_args );
+            /* If the file we want to execute doesn't exist we exit() */
+            exit( EXIT_FAILURE );
         }
+        case -1:    /* we're the error */
+            msg_Dbg( p_this, "Couldn't fork() while launching %s",
+                     ppsz_args[0] );
+            break;
+        default:    /* we're the parent */
+            /* Wait for the child to exit.
+             * We will not deadlock because we ran "/bin/sh &" */
+            while( waitpid( pid, NULL, 0 ) != pid);
+            break;
     }
 }
 
+/*****************************************************************************
+ * Run: main thread
+ *****************************************************************************
+ * This part of the module is in a separate thread so that we do not have
+ * too much system() overhead.
+ *****************************************************************************/
+static void Timer( void *data )
+{
+    vlc_inhibit_t *p_ih = data;
+
+    /* If there is a playing video output, disable xscreensaver */
+    /* http://www.jwz.org/xscreensaver/faq.html#dvd */
+    const char *const ppsz_xsargs[] = { "/bin/sh", "-c",
+        "xscreensaver-command -deactivate &", (char*)NULL };
+    Execute( VLC_OBJECT(p_ih), ppsz_xsargs );
+
+    const char *const ppsz_gsargs[] = { "/bin/sh", "-c",
+        "gnome-screensaver-command --poke &", (char*)NULL };
+    Execute( VLC_OBJECT(p_ih), ppsz_gsargs );
+}