]> git.sesse.net Git - vlc/blobdiff - modules/control/dbus.c
dbus: add option --dbus-unique-service-id (default: false)
[vlc] / modules / control / dbus.c
index 90ee7b2e9083a1c2299f5d1929492d205ece2052..d64ed6973efc1e603f8bcb34e46143a2e047f534 100644 (file)
@@ -112,6 +112,8 @@ struct intf_sys_t
     bool            b_dead;
     vlc_array_t    *p_events;
     vlc_mutex_t     lock;
+    input_thread_t *p_input;
+    bool            b_unique;
 };
 
 typedef struct
@@ -128,6 +130,10 @@ typedef struct
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
+#define DBUS_UNIQUE_TEXT N_("Unique DBUS service id (org.mpris.vlc-<pid>)")
+#define DBUS_UNIQUE_LONGTEXT N_( \
+    "Use a unique dbus service id to identify this VLC instance on the DBUS bus. " \
+    "The process identifier (PID) is added to the service name: org.mpris.vlc-<pid>" )
 
 vlc_module_begin ()
     set_shortname( N_("dbus"))
@@ -136,6 +142,8 @@ vlc_module_begin ()
     set_description( N_("D-Bus control interface") )
     set_capability( "interface", 0 )
     set_callbacks( Open, Close )
+    add_bool( "dbus-unique-service-id", false, NULL,
+              DBUS_UNIQUE_TEXT, DBUS_UNIQUE_LONGTEXT, true )
 vlc_module_end ()
 
 /*****************************************************************************
@@ -697,6 +705,7 @@ static int Open( vlc_object_t *p_this )
     playlist_t      *p_playlist;
     DBusConnection  *p_conn;
     DBusError       error;
+    char            *psz_service_name = NULL;
 
     if( !p_sys )
         return VLC_ENOMEM;
@@ -704,6 +713,22 @@ static int Open( vlc_object_t *p_this )
     p_sys->b_meta_read = false;
     p_sys->i_caps = CAPS_NONE;
     p_sys->b_dead = false;
+    p_sys->p_input = NULL;
+
+    p_sys->b_unique = var_CreateGetBool( p_intf, "dbus-unique-service-id" );
+    if( p_sys->b_unique )
+    {
+        if( asprintf( &psz_service_name, "%s-%d",
+            VLC_MPRIS_DBUS_SERVICE, getpid() ) < 0 )
+        {
+            free( p_sys );
+            return VLC_ENOMEM;
+        }
+    }
+    else
+    {
+        psz_service_name = strdup(VLC_MPRIS_DBUS_SERVICE);
+    }
 
     dbus_error_init( &error );
 
@@ -719,15 +744,18 @@ static int Open( vlc_object_t *p_this )
     }
 
     /* register a well-known name on the bus */
-    dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
+    dbus_bus_request_name( p_conn, psz_service_name, 0, &error );
     if( dbus_error_is_set( &error ) )
     {
-        msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
-                 ": %s", error.message );
+        msg_Err( p_this, "Error requesting service %s: %s",
+                 psz_service_name, error.message );
         dbus_error_free( &error );
+        free( psz_service_name );
         free( p_sys );
         return VLC_EGENERIC;
     }
+    msg_Info( p_intf, "listening on dbus as: %s", psz_service_name );
+    free( psz_service_name );
 
     /* we register the objects */
     dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
@@ -745,10 +773,9 @@ static int Open( vlc_object_t *p_this )
     p_sys->p_events = vlc_array_new();
     vlc_mutex_init( &p_sys->lock );
 
-    p_playlist = pl_Hold( p_intf );
+    p_playlist = pl_Get( p_intf );
     p_sys->p_playlist = p_playlist;
 
-    PL_LOCK;
     var_AddCallback( p_playlist, "item-current", AllCallback, p_intf );
     var_AddCallback( p_playlist, "intf-change", AllCallback, p_intf );
     var_AddCallback( p_playlist, "playlist-item-append", AllCallback, p_intf );
@@ -756,7 +783,6 @@ static int Open( vlc_object_t *p_this )
     var_AddCallback( p_playlist, "random", AllCallback, p_intf );
     var_AddCallback( p_playlist, "repeat", AllCallback, p_intf );
     var_AddCallback( p_playlist, "loop", AllCallback, p_intf );
-    PL_UNLOCK;
 
     UpdateCaps( p_intf );
 
@@ -772,7 +798,6 @@ static void Close   ( vlc_object_t *p_this )
     intf_thread_t   *p_intf     = (intf_thread_t*) p_this;
     intf_sys_t      *p_sys      = p_intf->p_sys;
     playlist_t      *p_playlist = p_sys->p_playlist;
-    input_thread_t  *p_input;
 
     var_DelCallback( p_playlist, "item-current", AllCallback, p_intf );
     var_DelCallback( p_playlist, "intf-change", AllCallback, p_intf );
@@ -782,13 +807,11 @@ static void Close   ( vlc_object_t *p_this )
     var_DelCallback( p_playlist, "repeat", AllCallback, p_intf );
     var_DelCallback( p_playlist, "loop", AllCallback, p_intf );
 
-    p_input = playlist_CurrentInput( p_playlist );
-    if ( p_input )
+    if( p_sys->p_input )
     {
-        var_DelCallback( p_input, "state", AllCallback, p_intf );
-        vlc_object_release( p_input );
+        var_DelCallback( p_sys->p_input, "state", AllCallback, p_intf );
+        vlc_object_release( p_sys->p_input );
     }
-    pl_Release( p_intf );
 
     dbus_connection_unref( p_sys->p_conn );
 
@@ -931,8 +954,10 @@ DBUS_SIGNAL( TrackListChangeSignal )
     SIGNAL_INIT( MPRIS_DBUS_TRACKLIST_PATH, "TrackListChange");
     OUT_ARGUMENTS;
 
-    /* XXX: locking */
-    dbus_int32_t i_elements = ((intf_thread_t*)p_data)->p_sys->p_playlist->current.i_size;
+    playlist_t *p_playlist = ((intf_thread_t*)p_data)->p_sys->p_playlist;
+    PL_LOCK;
+    dbus_int32_t i_elements = p_playlist->current.i_size;
+    PL_UNLOCK;
 
     ADD_INT32( &i_elements );
     SIGNAL_SEND;
@@ -1067,6 +1092,13 @@ static int TrackChange( intf_thread_t *p_intf )
     if( p_intf->p_sys->b_dead )
         return VLC_SUCCESS;
 
+    if( p_sys->p_input )
+    {
+        var_DelCallback( p_sys->p_input, "state", AllCallback, p_intf );
+        vlc_object_release( p_sys->p_input );
+        p_sys->p_input = NULL;
+    }
+
     p_sys->b_meta_read = false;
 
     p_input = playlist_CurrentInput( p_playlist );
@@ -1088,9 +1120,9 @@ static int TrackChange( intf_thread_t *p_intf )
         TrackChangeSignal( p_sys->p_conn, p_item );
     }
 
+    p_sys->p_input = p_input;
     var_AddCallback( p_input, "state", AllCallback, p_intf );
 
-    vlc_object_release( p_input );
     return VLC_SUCCESS;
 }