]> git.sesse.net Git - vlc/blobdiff - modules/misc/inhibit.c
Removed useless includes (svg).
[vlc] / modules / misc / inhibit.c
index e9aeba27619508bc8ecfdaaa3127a3dcffde50ee..924aa20f238386d0f7bbb10f7555147032e080f6 100644 (file)
@@ -38,6 +38,7 @@
 #include <vlc_plugin.h>
 #include <vlc_input.h>
 #include <vlc_interface.h>
+#include <vlc_playlist.h>
 
 #include <dbus/dbus.h>
 
 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 );
+
+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;
 };
@@ -74,26 +82,29 @@ 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 = 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 )
+    p_sys->p_conn = dbus_bus_get( 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;
 }
 
@@ -103,8 +114,21 @@ 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 )
+        UnInhibit( p_intf );
+    dbus_connection_unref( p_sys->p_conn );
+
+    free( p_sys );
 }
 
 /*****************************************************************************
@@ -113,67 +137,47 @@ static void Deactivate( vlc_object_t *p_this )
  *
  * returns false if Out of memory, else true
  *****************************************************************************/
-static int Inhibit( intf_thread_t *p_intf )
+static void Inhibit( intf_thread_t *p_intf )
 {
-    DBusConnection *p_conn;
-    DBusMessage *p_msg;
-    DBusMessageIter args;
-    DBusMessage *p_reply;
-    dbus_uint32_t i_cookie;
+    intf_sys_t *p_sys = p_intf->p_sys;
 
-    p_conn = p_intf->p_sys->p_conn;
+    DBusMessage *msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH,
+                                                     PM_INTERFACE, "Inhibit" );
+    if( unlikely(msg == NULL) )
+        return;
 
-    p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
-                                          "Inhibit" );
-    if( !p_msg )
-        return false;
+    const char *app = PACKAGE;
+    const char *reason = _("Playing some media.");
 
-    dbus_message_iter_init_append( p_msg, &args );
+    p_sys->i_cookie = 0;
 
-    char *psz_app = strdup( PACKAGE );
-    if( !psz_app ||
-        !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_app ) )
+    if( !dbus_message_append_args( msg, DBUS_TYPE_STRING, &app,
+                                        DBUS_TYPE_STRING, &reason,
+                                        DBUS_TYPE_INVALID ) )
     {
-        free( psz_app );
-        dbus_message_unref( p_msg );
-        return false;
+        dbus_message_unref( msg );
+        return;
     }
-    free( psz_app );
 
-    char *psz_inhibit_reason = strdup( "Playing some media." );
-    if( !psz_inhibit_reason )
-    {
-        dbus_message_unref( p_msg );
-        return false;
-    }
-    if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING,
-                                         &psz_inhibit_reason ) )
-    {
-        free( psz_inhibit_reason );
-        dbus_message_unref( p_msg );
-        return false;
-    }
-    free( psz_inhibit_reason );
-
-    p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
-        50, NULL ); /* blocks 50ms maximum */
-    dbus_message_unref( p_msg );
-    if( p_reply == NULL )
-    {   /* g-p-m is not active, or too slow. Better luck next time? */
-        return true;
-    }
+    /* blocks 50ms maximum */
+    DBusMessage *reply;
+
+    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, NULL,
-            DBUS_TYPE_UINT32, &i_cookie,
-            DBUS_TYPE_INVALID ) == FALSE )
-    {
-        return 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 = i_cookie;
 
-    /* Save the cookie */
-    p_intf->p_sys->i_cookie = i_cookie;
-    return true;
+    dbus_message_unref( reply );
 }
 
 /*****************************************************************************
@@ -181,72 +185,63 @@ static int Inhibit( intf_thread_t *p_intf )
  *
  * returns false if Out of memory, else true
  *****************************************************************************/
-static int UnInhibit( intf_thread_t *p_intf )
+static void UnInhibit( intf_thread_t *p_intf )
 {
-    DBusConnection *p_conn;
-    DBusMessage *p_msg;
-    DBusMessageIter args;
-    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 false;
+    DBusMessage *msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH,
+                                                   PM_INTERFACE, "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;
+    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 false;
+        dbus_connection_flush( p_sys->p_conn );
+        p_sys->i_cookie = 0;
     }
+    dbus_message_unref( 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;
+    const int old = prev.i_int, cur = value.i_int;
+
+    if( ( old == PLAYING_S ) == ( cur == PLAYING_S ) )
+        return VLC_SUCCESS; /* No interesting change */
 
-    if( !dbus_connection_send( p_conn, p_msg, NULL ) )
-        return false;
-    dbus_connection_flush( p_conn );
+    if( ( p_intf->p_sys->i_cookie != 0 ) == ( cur == PLAYING_S ) )
+        return VLC_SUCCESS; /* Already in correct state */
 
-    dbus_message_unref( p_msg );
+    if( cur == PLAYING_S )
+        Inhibit( p_intf );
+    else
+        UnInhibit( p_intf );
 
-    p_intf->p_sys->i_cookie = 0;
-    return 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 )
 {
-    for( ;; )
+    intf_thread_t *p_intf = data;
+    intf_sys_t *p_sys = p_intf->p_sys;
+
+    if( p_sys->p_input )
     {
-        input_thread_t *p_input;
-
-        /* Check playing state every 30 seconds */
-        msleep( 30 * CLOCK_FREQ );
-
-        p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
-        if( p_input )
-        {
-            const int i_state = var_GetInteger( p_input, "state" );
-            vlc_object_release( p_input );
-
-            if( PLAYING_S == i_state && !p_intf->p_sys->i_cookie )
-            {
-                if( !Inhibit( p_intf ) )
-                    break;
-            }
-            else if( p_intf->p_sys->i_cookie )
-            {
-                if( !UnInhibit( p_intf ) )
-                    break;
-            }
-        }
-        else if( p_intf->p_sys->i_cookie )
-        {
-            if( !UnInhibit( p_intf ) )
-                break;
-        }
+        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 )
+        var_AddCallback( p_sys->p_input, "state", StateChange, p_intf );
+
+    (void)var; (void)prev; (void)value; (void)p_playlist;
+    return VLC_SUCCESS;
 }