]> git.sesse.net Git - vlc/blobdiff - modules/misc/inhibit.c
logger: use <strong> rather than <b> in strict HTML
[vlc] / modules / misc / inhibit.c
index 1ec6b1c2344a1f043c28ff46529d681876d0dae0..14162e8936ed7fe4dc8a7df225473030b9e2d2a8 100644 (file)
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <vlc_input.h>
 #include <vlc_interface.h>
+#include <vlc_playlist.h>
 
 #include <dbus/dbus.h>
 
-#define PM_SERVICE   "org.freedesktop.PowerManagement"
-#define PM_PATH     "/org/freedesktop/PowerManagement/Inhibit"
-#define PM_INTERFACE "org.freedesktop.PowerManagement.Inhibit"
+enum {
+    FREEDESKTOP = 0, /* as used by KDE and gnome <= 2.26 */
+    GNOME       = 1, /* as used by gnome > 2.26 */
+};
+
+static const char *dbus_service[] = {
+    [FREEDESKTOP]   = "org.freedesktop.PowerManagement",
+    [GNOME]         = "org.gnome.SessionManager",
+};
+
+static const char *dbus_path[] = {
+    [FREEDESKTOP]   = "/org/freedesktop/PowerManagement",
+    [GNOME]         = "/org/gnome/SessionManager",
+};
+
+static const char *dbus_interface[] = {
+    [FREEDESKTOP]   = "org.freedesktop.PowerManagement.Inhibit",
+    [GNOME]         = "org.gnome.SessionManager",
+};
+
 
 /*****************************************************************************
  * Local prototypes
- *****************************************************************************/
!*****************************************************************************/
 static int  Activate     ( vlc_object_t * );
 static void Deactivate   ( vlc_object_t * );
 
-static void Run          ( intf_thread_t *p_intf );
+static void UnInhibit( intf_thread_t *p_intf, int type );
+
+static int InputChange( vlc_object_t *, const char *,
+                        vlc_value_t, vlc_value_t, void * );
+static int StateChange( vlc_object_t *, const char *,
+                        vlc_value_t, vlc_value_t, void * );
 
 struct intf_sys_t
 {
+    playlist_t      *p_playlist;
+    vlc_object_t    *p_input;
     DBusConnection  *p_conn;
-    dbus_uint32_t   i_cookie;
+    dbus_uint32_t   i_cookie[2];
 };
 
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
-vlc_module_begin();
-    set_description( _("Power Management Inhibitor") );
-    set_capability( "interface", 0 );
-    set_callbacks( Activate, Deactivate );
-vlc_module_end();
+vlc_module_begin ()
+    set_description( N_("Power Management Inhibitor") )
+    set_capability( "interface", 0 )
+    set_callbacks( Activate, Deactivate )
+vlc_module_end ()
 
 /*****************************************************************************
  * Activate: initialize and create stuff
@@ -73,29 +99,34 @@ vlc_module_end();
 static int Activate( vlc_object_t *p_this )
 {
     intf_thread_t *p_intf = (intf_thread_t*)p_this;
+    intf_sys_t *p_sys;
     DBusError     error;
 
-
-    p_intf->pf_run = Run;
-
-    p_intf->p_sys = (intf_sys_t *) calloc( 1, sizeof( intf_sys_t ) );
-
-    if( !p_intf->p_sys )
+    p_sys = p_intf->p_sys = (intf_sys_t *) calloc( 1, sizeof( intf_sys_t ) );
+    if( !p_sys )
         return VLC_ENOMEM;
 
-    p_intf->p_sys->i_cookie = 0;
+    p_sys->i_cookie[FREEDESKTOP] = 0;
+    p_sys->i_cookie[GNOME] = 0;
+    p_sys->p_input = NULL;
 
     dbus_error_init( &error );
-    p_intf->p_sys->p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
-    if( !p_intf->p_sys->p_conn )
+
+    /* connect privately to the session bus
+     * the connection will not be shared with other vlc modules which use dbus,
+     * thus avoiding a whole class of concurrency issues */
+    p_sys->p_conn = dbus_bus_get_private( DBUS_BUS_SESSION, &error );
+    if( !p_sys->p_conn )
     {
         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
                 error.message );
         dbus_error_free( &error );
-        free( p_intf->p_sys );
+        free( p_sys );
         return VLC_EGENERIC;
     }
 
+    p_sys->p_playlist = pl_Get( p_intf );
+    var_AddCallback( p_sys->p_playlist, "item-current", InputChange, p_intf );
     return VLC_SUCCESS;
 }
 
@@ -105,163 +136,165 @@ static int Activate( vlc_object_t *p_this )
 static void Deactivate( vlc_object_t *p_this )
 {
     intf_thread_t *p_intf = (intf_thread_t*)p_this;
-    dbus_connection_unref( p_intf->p_sys->p_conn );
-    free( p_intf->p_sys );
+    intf_sys_t *p_sys = p_intf->p_sys;
+
+    var_DelCallback( p_sys->p_playlist, "item-current", InputChange, p_intf );
+
+    if( p_sys->p_input ) /* Do delete "state" after "item-changed"! */
+    {
+        var_DelCallback( p_sys->p_input, "state", StateChange, p_intf );
+        vlc_object_release( p_sys->p_input );
+    }
+
+    if( p_sys->i_cookie[FREEDESKTOP] )
+        UnInhibit( p_intf, FREEDESKTOP );
+    if( p_sys->i_cookie[GNOME] )
+        UnInhibit( p_intf, GNOME );
+
+    /* The dbus connection is private,
+     * so we are responsible for closing it */
+    dbus_connection_close( p_sys->p_conn );
+    dbus_connection_unref( p_sys->p_conn );
+
+    free( p_sys );
 }
 
 /*****************************************************************************
  * Inhibit: Notify the power management daemon that it shouldn't suspend
  * the computer because of inactivity
- *
- * returns VLC_FALSE if Out of memory, else VLC_TRUE
  *****************************************************************************/
-static int Inhibit( intf_thread_t *p_intf )
+static void Inhibit( intf_thread_t *p_intf, int type )
 {
-    DBusConnection *p_conn;
-    DBusMessage *p_msg;
-    DBusMessageIter args;
-    DBusMessage *p_reply;
-    DBusError error;
-    dbus_error_init( &error );
-    dbus_uint32_t i_cookie;
-
-    p_conn = p_intf->p_sys->p_conn;
-
-    p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
-                                          "Inhibit" );
-    if( !p_msg )
-        return VLC_FALSE;
-
-    dbus_message_iter_init_append( p_msg, &args );
-
-    char *psz_app = strdup( PACKAGE );
-    if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_app ) )
-    {
-        free( psz_app );
-        dbus_message_unref( p_msg );
-        return VLC_FALSE;
+    intf_sys_t *p_sys = p_intf->p_sys;
+
+    DBusMessage *msg = dbus_message_new_method_call(
+        dbus_service[type], dbus_path[type], dbus_interface[type], "Inhibit" );
+    if( unlikely(msg == NULL) )
+        return;
+
+    const char *app = PACKAGE;
+    const char *reason = _("Playing some media.");
+
+    p_sys->i_cookie[type] = 0;
+
+    dbus_bool_t ret;
+    dbus_uint32_t xid = 0; // FIXME?
+    dbus_uint32_t flags = 8 /* Inhibit suspending the session or computer */
+                        | 4;/* Inhibit the session being marked as idle */
+    switch( type ) {
+    case FREEDESKTOP:
+        ret = dbus_message_append_args( msg, DBUS_TYPE_STRING, &app,
+                                        DBUS_TYPE_STRING, &reason,
+                                        DBUS_TYPE_INVALID );
+        break;
+    case GNOME:
+    default:
+        ret = dbus_message_append_args( msg, DBUS_TYPE_STRING, &app,
+                                        DBUS_TYPE_UINT32, &xid,
+                                        DBUS_TYPE_STRING, &reason,
+                                        DBUS_TYPE_UINT32, &flags,
+                                        DBUS_TYPE_INVALID );
+        break;
     }
-    free( psz_app );
 
-    char *psz_inhibit_reason = strdup( "Playing some media." );
-    if( !psz_inhibit_reason )
-    {
-        dbus_message_unref( p_msg );
-        return VLC_FALSE;
-    }
-    if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING,
-                                         &psz_inhibit_reason ) )
+    if( !ret )
     {
-        free( psz_inhibit_reason );
-        dbus_message_unref( p_msg );
-        return VLC_FALSE;
+        dbus_message_unref( msg );
+        return;
     }
-    free( psz_inhibit_reason );
 
-    p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
-        50, &error ); /* blocks 50ms maximum */
+    /* blocks 50ms maximum */
+    DBusMessage *reply;
 
-    dbus_message_unref( p_msg );
-    if( p_reply == NULL )
-    {   /* g-p-m is not active, or too slow. Better luck next time? */
-        return VLC_TRUE;
-    }
+    reply = dbus_connection_send_with_reply_and_block( p_sys->p_conn, msg,
+                                                       50, NULL );
+    dbus_message_unref( msg );
+    if( reply == NULL )
+        /* g-p-m is not active, or too slow. Better luck next time? */
+        return;
 
     /* extract the cookie from the reply */
-    if( dbus_message_get_args( p_reply, &error,
-            DBUS_TYPE_UINT32, &i_cookie,
-            DBUS_TYPE_INVALID ) == FALSE )
-    {
-        return VLC_FALSE;
-    }
+    dbus_uint32_t i_cookie;
+
+    if( dbus_message_get_args( reply, NULL,
+                               DBUS_TYPE_UINT32, &i_cookie,
+                               DBUS_TYPE_INVALID ) )
+        p_sys->i_cookie[type] = i_cookie;
 
-    /* Save the cookie */
-    p_intf->p_sys->i_cookie = i_cookie;
-    return VLC_TRUE;
+    dbus_message_unref( reply );
 }
 
 /*****************************************************************************
  * UnInhibit: Notify the power management daemon that we aren't active anymore
- *
- * returns VLC_FALSE if Out of memory, else VLC_TRUE
  *****************************************************************************/
-static int UnInhibit( intf_thread_t *p_intf )
+static void UnInhibit( intf_thread_t *p_intf, int type )
 {
-    DBusConnection *p_conn;
-    DBusMessage *p_msg;
-    DBusMessageIter args;
-    DBusError error;
-    dbus_error_init( &error );
-    dbus_uint32_t i_cookie;
-
-    p_conn = p_intf->p_sys->p_conn;
+    intf_sys_t *p_sys = p_intf->p_sys;
 
-    p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
-                                          "UnInhibit" );
-    if( !p_msg )
-        return VLC_FALSE;
+    DBusMessage *msg = dbus_message_new_method_call( dbus_service[type],
+            dbus_path[type], dbus_interface[type], "UnInhibit" );
+    if( unlikely(msg == NULL) )
+        return;
 
-    dbus_message_iter_init_append( p_msg, &args );
-
-    i_cookie = p_intf->p_sys->i_cookie;
-    if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_cookie ) )
+    dbus_uint32_t i_cookie = p_sys->i_cookie[type];
+    if( dbus_message_append_args( msg, DBUS_TYPE_UINT32, &i_cookie,
+                                       DBUS_TYPE_INVALID )
+     && dbus_connection_send( p_sys->p_conn, msg, NULL ) )
     {
-        dbus_message_unref( p_msg );
-        return VLC_FALSE;
+        dbus_connection_flush( p_sys->p_conn );
+        p_sys->i_cookie[type] = 0;
     }
+    dbus_message_unref( msg );
+}
 
-    if( !dbus_connection_send( p_conn, p_msg, NULL ) )
-        return VLC_FALSE;
-    dbus_connection_flush( p_conn );
 
-    dbus_message_unref( p_msg );
+static int StateChange( vlc_object_t *p_input, const char *var,
+                        vlc_value_t prev, vlc_value_t value, void *data )
+{
+    intf_thread_t *p_intf = data;
+    intf_sys_t *p_sys = p_intf->p_sys;
+    const int old = prev.i_int, cur = value.i_int;
+
+    if( ( old == PLAYING_S ) == ( cur == PLAYING_S ) )
+        return VLC_SUCCESS; /* No interesting change */
+
+    if( cur == PLAYING_S ) {
+        if (p_sys->i_cookie[FREEDESKTOP] == 0)
+            Inhibit( p_intf, FREEDESKTOP );
+        if (p_sys->i_cookie[GNOME] == 0)
+            Inhibit( p_intf, GNOME );
+    }
+    else {
+        if (p_sys->i_cookie[FREEDESKTOP] != 0)
+            UnInhibit( p_intf, FREEDESKTOP );
+        if (p_sys->i_cookie[GNOME] != 0)
+            UnInhibit( p_intf, GNOME );
+    }
 
-    p_intf->p_sys->i_cookie = 0;
-    return VLC_TRUE;
+    (void)p_input; (void)var; (void)prev;
+    return VLC_SUCCESS;
 }
 
-/*****************************************************************************
- * Run: main thread
- *****************************************************************************/
-static void Run( intf_thread_t *p_intf )
+static int InputChange( vlc_object_t *p_playlist, const char *var,
+                        vlc_value_t prev, vlc_value_t value, void *data )
 {
-    vlc_object_lock( p_intf );
-    for(;;)
+    intf_thread_t *p_intf = data;
+    intf_sys_t *p_sys = p_intf->p_sys;
+
+    if( p_sys->p_input )
+    {
+        var_DelCallback( p_sys->p_input, "state", StateChange, p_intf );
+        vlc_object_release( p_sys->p_input );
+    }
+    p_sys->p_input = VLC_OBJECT(playlist_CurrentInput( p_sys->p_playlist ));
+    if( p_sys->p_input )
     {
-        input_thread_t *p_input;
-
-        /* Check playing state every 30 seconds */
-        if( vlc_object_timedwait( p_intf, mdate() + 30000000 ) < 0 )
-            break;
-
-        p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
-        if( p_input )
-        {
-            if( PLAYING_S == p_input->i_state && !p_intf->p_sys->i_cookie )
-            {
-                if( !Inhibit( p_intf ) )
-                {
-                    vlc_object_release( p_input );
-                    goto end;
-                }
-            }
-            else if( p_intf->p_sys->i_cookie )
-            {
-                if( !UnInhibit( p_intf ) )
-                {
-                    vlc_object_release( p_input );
-                    goto end;
-                }
-            }
-            vlc_object_release( p_input );
-        }
-        else if( p_intf->p_sys->i_cookie )
-        {
-            if( !UnInhibit( p_intf ) )
-                goto end;
-        }
+        Inhibit( p_intf, FREEDESKTOP );
+        Inhibit( p_intf, GNOME );
+
+        var_AddCallback( p_sys->p_input, "state", StateChange, p_intf );
     }
 
-end:
-    vlc_object_unlock( p_intf );
+    (void)var; (void)prev; (void)value; (void)p_playlist;
+    return VLC_SUCCESS;
 }