[vlc-commits] posix: move D-Bus one-instance to system_Configure()

Rémi Denis-Courmont git at videolan.org
Sun Jun 18 17:47:25 CEST 2017


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Jun 18 17:10:30 2017 +0300| [ba8117c4fbb330fe9d09fdcc35595146af170299] | committer: Rémi Denis-Courmont

posix: move D-Bus one-instance to system_Configure()

This takes the platform-specific wart out of common initialization
code, and into the same callback as the equivalent Windows code.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=ba8117c4fbb330fe9d09fdcc35595146af170299
---

 src/libvlc.c         | 120 -----------------------------------------------
 src/posix/specific.c | 130 +++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 127 insertions(+), 123 deletions(-)

diff --git a/src/libvlc.c b/src/libvlc.c
index 4b220d2e31..844e04c09f 100644
--- a/src/libvlc.c
+++ b/src/libvlc.c
@@ -51,12 +51,6 @@
 
 #include "config/vlc_getopt.h"
 
-#ifdef HAVE_DBUS
-/* used for one-instance mode */
-#   include <dbus/dbus.h>
-#endif
-
-
 #include <vlc_playlist.h>
 #include <vlc_interface.h>
 
@@ -236,120 +230,6 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     if( libvlc_InternalKeystoreInit( p_libvlc ) != VLC_SUCCESS )
         msg_Warn( p_libvlc, "memory keystore init failed" );
 
-/* FIXME: could be replaced by using Unix sockets */
-#ifdef HAVE_DBUS
-
-#define MPRIS_APPEND "/org/mpris/MediaPlayer2/TrackList/Append"
-#define MPRIS_BUS_NAME "org.mpris.MediaPlayer2.vlc"
-#define MPRIS_OBJECT_PATH "/org/mpris/MediaPlayer2"
-#define MPRIS_TRACKLIST_INTERFACE "org.mpris.MediaPlayer2.TrackList"
-
-    if( var_InheritBool( p_libvlc, "one-instance" )
-    || ( var_InheritBool( p_libvlc, "one-instance-when-started-from-file" )
-      && var_InheritBool( p_libvlc, "started-from-file" ) ) )
-    {
-        for( int i = vlc_optind; i < i_argc; i++ )
-            if( ppsz_argv[i][0] == ':' )
-            {
-                msg_Err( p_libvlc, "item option %s incompatible with single instance",
-                         ppsz_argv[i] );
-                goto dbus_out;
-            }
-
-        /* Initialise D-Bus interface, check for other instances */
-        dbus_threads_init_default();
-
-        DBusError err;
-        dbus_error_init( &err );
-
-        /* connect to the session bus */
-        DBusConnection  *conn = dbus_bus_get( DBUS_BUS_SESSION, &err );
-        if( conn == NULL )
-        {
-            msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
-                    err.message );
-            dbus_error_free( &err );
-            goto dbus_out;
-        }
-
-        /* check if VLC is available on the bus
-         * if not: D-Bus control is not enabled on the other
-         * instance and we can't pass MRLs to it */
-        /* FIXME: This check is totally brain-dead and buggy. */
-        if( !dbus_bus_name_has_owner( conn, MPRIS_BUS_NAME, &err ) )
-        {
-            dbus_connection_unref( conn );
-            if( dbus_error_is_set( &err ) )
-            {
-                msg_Err( p_libvlc, "D-Bus error: %s", err.message );
-            }
-            else
-                msg_Dbg( p_libvlc, "No media player running. Continuing normally." );
-            dbus_error_free( &err );
-            goto dbus_out;
-        }
-
-        const dbus_bool_t play = !var_InheritBool( p_libvlc, "playlist-enqueue" );
-
-        msg_Warn( p_libvlc, "media player running. Exiting...");
-        for( int i = vlc_optind; i < i_argc; i++ )
-        {
-            DBusMessage *msg = dbus_message_new_method_call(
-               MPRIS_BUS_NAME, MPRIS_OBJECT_PATH, MPRIS_TRACKLIST_INTERFACE, "AddTrack" );
-            if( unlikely(msg == NULL) )
-                continue;
-
-            /* We need to resolve relative paths in this instance */
-            char *mrl;
-            if( strstr( ppsz_argv[i], "://" ) )
-                mrl = strdup( ppsz_argv[i] );
-            else
-                mrl = vlc_path2uri( ppsz_argv[i], NULL );
-            if( mrl == NULL )
-            {
-                dbus_message_unref( msg );
-                continue;
-            }
-
-            const char *after_track = MPRIS_APPEND;
-
-            /* append MRLs */
-            if( !dbus_message_append_args( msg, DBUS_TYPE_STRING, &mrl,
-                                                DBUS_TYPE_OBJECT_PATH, &after_track,
-                                                DBUS_TYPE_BOOLEAN, &play,
-                                                DBUS_TYPE_INVALID ) )
-            {
-                 dbus_message_unref( msg );
-                 msg = NULL;
-                 free( mrl );
-                 continue;
-            }
-
-            msg_Dbg( p_libvlc, "Adds %s to the running media player", mrl );
-            free( mrl );
-
-            /* send message and get a handle for a reply */
-            DBusMessage *reply = dbus_connection_send_with_reply_and_block( conn, msg, -1,
-                                                                            &err );
-            dbus_message_unref( msg );
-            if( reply == NULL )
-            {
-                msg_Err( p_libvlc, "D-Bus error: %s", err.message );
-                continue;
-            }
-            dbus_message_unref( reply );
-        }
-        /* we unreference the connection when we've finished with it */
-        dbus_connection_unref( conn );
-        exit( 0 );
-    }
-#undef MPRIS_APPEND
-#undef MPRIS_BUS_NAME
-#undef MPRIS_OBJECT_PATH
-#undef MPRIS_TRACKLIST_INTERFACE
-dbus_out:
-#endif // HAVE_DBUS
-
     vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
 
     priv->b_stats = var_InheritBool( p_libvlc, "stats" );
diff --git a/src/posix/specific.c b/src/posix/specific.c
index 971ec6a8fe..9622816d5f 100644
--- a/src/posix/specific.c
+++ b/src/posix/specific.c
@@ -25,12 +25,136 @@
 #include <vlc_common.h>
 #include "../libvlc.h"
 
+#ifdef HAVE_DBUS
+/* used for one-instance mode */
+# include <dbus/dbus.h>
+# include <vlc_url.h>
+#endif
+
 void system_Init (void)
 {
 }
 
-void system_Configure (libvlc_int_t *libvlc,
-                       int argc, const char *const argv[])
+static void system_ConfigureDbus(libvlc_int_t *vlc, int argc,
+                                 const char *const argv[])
+{
+/* FIXME: could be replaced by using Unix sockets */
+#ifdef HAVE_DBUS
+    if (!var_InheritBool(vlc, "one-instance")
+     && !(var_InheritBool(vlc, "one-instance-when-started-from-file")
+       && var_InheritBool(vlc, "started-from-file")))
+         return;
+
+#define MPRIS_APPEND "/org/mpris/MediaPlayer2/TrackList/Append"
+#define MPRIS_BUS_NAME "org.mpris.MediaPlayer2.vlc"
+#define MPRIS_OBJECT_PATH "/org/mpris/MediaPlayer2"
+#define MPRIS_TRACKLIST_INTERFACE "org.mpris.MediaPlayer2.TrackList"
+    for (int i = 0; i < argc; i++)
+        if (argv[i][0] == ':')
+        {
+            msg_Err(vlc, "item option %s incompatible with single instance",
+                    argv[i]);
+            return;
+        }
+
+    /* Initialise D-Bus interface, check for other instances */
+    dbus_threads_init_default();
+
+    DBusError err;
+    dbus_error_init(&err);
+
+    /* connect to the session bus */
+    DBusConnection *conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
+    if (conn == NULL)
+    {
+        msg_Err(vlc, "D-Bus session bus connection failure: %s",
+                 err.message);
+        dbus_error_free(&err);
+        return;
+    }
+
+    /* check if VLC is available on the bus
+     * if not: D-Bus control is not enabled on the other
+     * instance and we can't pass MRLs to it */
+    /* FIXME: This check is totally brain-dead and buggy. */
+    if (!dbus_bus_name_has_owner(conn, MPRIS_BUS_NAME, &err))
+    {
+        dbus_connection_unref(conn);
+        if (dbus_error_is_set(&err))
+        {
+            msg_Err(vlc, "D-Bus error: %s", err.message);
+            dbus_error_free(&err);
+        }
+        else
+            msg_Dbg(vlc, "no running VLC instance - continuing normally...");
+        return;
+    }
+    msg_Warn(vlc, "running VLC instance - exiting...");
+
+    const dbus_bool_t play = !var_InheritBool(vlc, "playlist-enqueue");
+
+    for (int i = 0; i < argc; i++)
+    {
+        DBusMessage *req = dbus_message_new_method_call(MPRIS_BUS_NAME,
+            MPRIS_OBJECT_PATH, MPRIS_TRACKLIST_INTERFACE, "AddTrack");
+        if (unlikely(req == NULL))
+            continue;
+
+        /* We need to resolve relative paths in this instance */
+        char *mrlbuf = NULL;
+        const char *mrl;
+
+        if (strstr(argv[i], "://"))
+            mrl = argv[i];
+        else
+        {
+            mrlbuf = vlc_path2uri(argv[i], NULL);
+            if (unlikely(mrlbuf == NULL))
+            {
+                dbus_message_unref(req);
+                continue;
+            }
+            mrl = mrlbuf;
+        }
+
+        /* append MRLs */
+        msg_Dbg(vlc, "adding track %s to running instance", mrl);
+
+        const char *after_track = MPRIS_APPEND;
+        dbus_bool_t ok = dbus_message_append_args(req, DBUS_TYPE_STRING, &mrl,
+                                           DBUS_TYPE_OBJECT_PATH, &after_track,
+                                           DBUS_TYPE_BOOLEAN, &play,
+                                           DBUS_TYPE_INVALID);
+        free(mrlbuf);
+        if (unlikely(!ok))
+        {
+             dbus_message_unref(req);
+             continue;
+        }
+
+        /* send message and get a handle for a reply */
+        DBusMessage *reply = dbus_connection_send_with_reply_and_block(conn,
+                                                                req, -1, &err);
+        dbus_message_unref(req);
+        if (reply == NULL)
+        {
+            msg_Err(vlc, "D-Bus error: %s", err.message);
+            dbus_error_free(&err);
+            continue;
+        }
+        dbus_message_unref(reply);
+    }
+
+    /* we unreference the connection when we've finished with it */
+    dbus_connection_unref(conn);
+    exit(0);
+#else
+    (void) libvlc; (void) argc; (void) argv;
+#endif // HAVE_DBUS
+}
+
+void system_Configure(libvlc_int_t *libvlc,
+                      int argc, const char *const argv[])
 {
-    (void)libvlc; (void)argc; (void)argv;
+    system_ConfigureDbus(libvlc, argc, argv);
 }



More information about the vlc-commits mailing list