]> git.sesse.net Git - vlc/blobdiff - modules/control/dbus.c
Another bunch of useless test.
[vlc] / modules / control / dbus.c
index fd3b65371c6fa540ed7bc28c270eff1e0dab4c6b..681207ef679a1dc0f021b14baeaf95ac4704f07a 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * dbus.c : D-Bus control interface
  *****************************************************************************
- * Copyright © 2006-2007 Rafaël Carré
- * Copyright © 2007 Mirsal Ennaime
+ * Copyright © 2006-2008 Rafaël Carré
+ * Copyright © 2007-2008 Mirsal Ennaime
  * $Id$
  *
  * Authors:    Rafaël Carré <funman at videolanorg>
@@ -31,7 +31,7 @@
  *  extract:
  *   "If you use this low-level API directly, you're signing up for some pain."
  *
- * MPRIS Specification (still drafting on Oct, 23 of 2007):
+ * MPRIS Specification (still drafting on Jan, 23 of 2008):
  *      http://wiki.xmms2.xmms.se/index.php/MPRIS
  */
 
 
 #include "dbus.h"
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <vlc/vlc.h>
 #include <vlc_aout.h>
 #include <vlc_interface.h>
 #include <vlc_meta.h>
 #include <vlc_input.h>
 #include <vlc_playlist.h>
+#include <vlc_demux.h>
+#include <vlc_access.h>
 
 /*****************************************************************************
  * Local prototypes.
@@ -67,13 +73,29 @@ static int TrackChange( vlc_object_t *, const char *, vlc_value_t,
 static int StatusChangeEmit( vlc_object_t *, const char *, vlc_value_t,
                         vlc_value_t, void * );
 
+static int TrackListChangeEmit( vlc_object_t *, const char *, vlc_value_t,
+                        vlc_value_t, void * );
+
 static int GetInputMeta ( input_item_t *, DBusMessageIter * );
 static int MarshalStatus ( intf_thread_t *, DBusMessageIter *, vlc_bool_t );
 
+/* GetCaps() capabilities */
+enum
+{
+     CAPS_NONE                  = 0,
+     CAPS_CAN_GO_NEXT           = 1 << 0,
+     CAPS_CAN_GO_PREV           = 1 << 1,
+     CAPS_CAN_PAUSE             = 1 << 2,
+     CAPS_CAN_PLAY              = 1 << 3,
+     CAPS_CAN_SEEK              = 1 << 4,
+     CAPS_CAN_PROVIDE_METADATA  = 1 << 5
+};
+
 struct intf_sys_t
 {
     DBusConnection *p_conn;
     vlc_bool_t      b_meta_read;
+    dbus_int32_t    i_caps;
 };
 
 /*****************************************************************************
@@ -280,10 +302,63 @@ DBUS_METHOD( GetCurrentMetadata )
     REPLY_SEND;
 }
 
+DBUS_METHOD( GetCaps )
+{
+    REPLY_INIT;
+    OUT_ARGUMENTS;
+    playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
+    PL_LOCK;
+    
+    dbus_int32_t i_caps = CAPS_NONE;
+
+    /* FIXME:
+     * Every capability should be checked in a callback, modifying p_sys->i_caps
+     * so we can send a signal whenever it changes.
+     * When it is done, GetCaps method will just return p_sys->i_caps
+     */
+
+    if( p_playlist->items.i_size > 0 )
+        i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
+
+    if( p_playlist->p_input )
+    {
+        access_t *p_access = (access_t*)vlc_object_find( p_playlist->p_input, 
+            VLC_OBJECT_ACCESS, FIND_CHILD );
+        if( p_access )
+        {
+            vlc_bool_t b_can_pause;
+            if( !access2_Control( p_access, ACCESS_CAN_PAUSE, &b_can_pause ) &&
+                    b_can_pause )
+                i_caps |= CAPS_CAN_PAUSE;
+            vlc_object_release( p_access );
+        }
+        demux_t *p_demux = (demux_t*)vlc_object_find( p_playlist->p_input,
+            VLC_OBJECT_DEMUX, FIND_CHILD );
+        if( p_demux )
+        {   /* XXX: is: demux can seek and access can not a possibility ? */
+            vlc_bool_t b_can_seek;
+            if( !stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek ) &&
+                    b_can_seek )
+                i_caps |= CAPS_CAN_SEEK;
+            vlc_object_release( p_demux );
+        }
+    }
+
+    if( ((intf_thread_t*)p_this)->p_sys->b_meta_read )
+        i_caps |= CAPS_CAN_PROVIDE_METADATA;
+
+    PL_UNLOCK;
+
+    ADD_INT32( &i_caps );
+
+    REPLY_SEND;
+}
+
 /* Media Player information */
 
 DBUS_METHOD( Identity )
 {
+    VLC_UNUSED(p_this);
     REPLY_INIT;
     OUT_ARGUMENTS;
     char *psz_identity;
@@ -417,7 +492,7 @@ DBUS_METHOD( DelTrack )
         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
     }
 
-    if( i_position >= p_playlist->items.i_size / 2 )
+    if( i_position <= p_playlist->items.i_size / 2 )
     {
         playlist_DeleteFromInput( p_playlist,
             p_playlist->items.p_elems[i_position*2-1]->i_id,
@@ -529,6 +604,7 @@ DBUS_METHOD( Random )
 
 DBUS_METHOD( handle_introspect_root )
 { /* handles introspection of root object */
+    VLC_UNUSED(p_this);
     REPLY_INIT;
     OUT_ARGUMENTS;
     ADD_STRING( &psz_introspection_xml_data_root );
@@ -537,6 +613,7 @@ DBUS_METHOD( handle_introspect_root )
 
 DBUS_METHOD( handle_introspect_player )
 {
+    VLC_UNUSED(p_this);
     REPLY_INIT;
     OUT_ARGUMENTS;
     ADD_STRING( &psz_introspection_xml_data_player );
@@ -545,6 +622,7 @@ DBUS_METHOD( handle_introspect_player )
 
 DBUS_METHOD( handle_introspect_tracklist )
 {
+    VLC_UNUSED(p_this);
     REPLY_INIT;
     OUT_ARGUMENTS;
     ADD_STRING( &psz_introspection_xml_data_tracklist );
@@ -578,7 +656,7 @@ DBUS_METHOD( handle_player )
 {
     if( dbus_message_is_method_call( p_from,
                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
-    return handle_introspect_player( p_conn, p_from, p_this );
+        return handle_introspect_player( p_conn, p_from, p_this );
 
     /* here D-Bus method's names are associated to an handler */
 
@@ -595,6 +673,7 @@ DBUS_METHOD( handle_player )
     METHOD_FUNC( "PositionGet",             PositionGet );
     METHOD_FUNC( "GetStatus",               GetStatus );
     METHOD_FUNC( "GetMetadata",             GetCurrentMetadata );
+    METHOD_FUNC( "GetCaps",                 GetCaps );
 
     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
@@ -634,6 +713,7 @@ static int Open( vlc_object_t *p_this )
         return VLC_ENOMEM;
 
     p_sys->b_meta_read = VLC_FALSE;
+    p_sys->i_caps = CAPS_NONE;
 
     dbus_error_init( &error );
 
@@ -672,6 +752,9 @@ static int Open( vlc_object_t *p_this )
     p_playlist = pl_Yield( p_intf );
     PL_LOCK;
     var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
+    var_AddCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
+    var_AddCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
+    var_AddCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
     var_AddCallback( p_playlist, "random", StatusChangeEmit, p_intf );
     var_AddCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
     var_AddCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
@@ -699,6 +782,9 @@ static void Close   ( vlc_object_t *p_this )
 
     PL_LOCK;
     var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
+    var_DelCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
+    var_DelCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
+    var_DelCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
     var_DelCallback( p_playlist, "random", StatusChangeEmit, p_intf );
     var_DelCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
     var_DelCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
@@ -732,6 +818,52 @@ static void Run          ( intf_thread_t *p_intf )
     }
 }
 
+/******************************************************************************
+ * TrackListChange: tracklist order / length change signal 
+ *****************************************************************************/
+DBUS_SIGNAL( TrackListChangeSignal )
+{ /* emit the new tracklist lengh */
+    SIGNAL_INIT("TrackListChange");
+    OUT_ARGUMENTS;
+
+    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_data );
+    dbus_int32_t i_elements = p_playlist->items.i_size / 2;
+    pl_Release( p_playlist );
+
+    ADD_INT32( &i_elements );
+    SIGNAL_SEND;
+}
+
+/*****************************************************************************
+ * TrackListChangeEmit: Emits the TrackListChange signal
+ *****************************************************************************/
+/* FIXME: It is not called on tracklist reordering */
+static int TrackListChangeEmit( vlc_object_t *p_this, const char *psz_var,
+            vlc_value_t oldval, vlc_value_t newval, void *p_data )
+{
+    VLC_UNUSED(oldval);
+    intf_thread_t *p_intf = p_data;
+
+    if( !strcmp( psz_var, "item-append" ) || !strcmp( psz_var, "item-remove" ) )
+    {
+        /* don't signal when items are added/removed in p_category */
+        playlist_t *p_playlist = (playlist_t*)p_this;
+        playlist_add_t *p_add = newval.p_address;
+        playlist_item_t *p_item;
+        p_item = playlist_ItemGetById( p_playlist, p_add->i_node, VLC_TRUE );
+        assert( p_item );
+        while( p_item->p_parent )
+            p_item = p_item->p_parent;
+        if( p_item == p_playlist->p_root_category )
+            return VLC_SUCCESS;
+    }
+
+    if( p_intf->b_dead )
+        return VLC_SUCCESS;
+
+    TrackListChangeSignal( p_intf->p_sys->p_conn, p_data );
+    return VLC_SUCCESS;
+}
 /*****************************************************************************
  * TrackChange: Playlist item change callback
  *****************************************************************************/
@@ -769,6 +901,7 @@ DBUS_SIGNAL( StatusChangeSignal )
 static int StateChange( vlc_object_t *p_this, const char* psz_var,
             vlc_value_t oldval, vlc_value_t newval, void *p_data )
 {
+    VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
     intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
     intf_sys_t          *p_sys      = p_intf->p_sys;
 
@@ -800,6 +933,8 @@ static int StateChange( vlc_object_t *p_this, const char* psz_var,
 static int StatusChangeEmit( vlc_object_t *p_this, const char *psz_var,
             vlc_value_t oldval, vlc_value_t newval, void *p_data )
 {
+    VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
+    VLC_UNUSED(oldval); VLC_UNUSED(newval);
     intf_thread_t *p_intf = p_data;
 
     if( p_intf->b_dead )
@@ -856,6 +991,39 @@ static int TrackChange( vlc_object_t *p_this, const char *psz_var,
         TrackChangeSignal( p_sys->p_conn, p_item );
     }
 
+    dbus_int32_t i_caps = CAPS_NONE;
+
+    if( p_playlist->items.i_size > 0 )
+        i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;
+
+    if( p_playlist->p_input )
+    {
+        access_t *p_access = (access_t*)vlc_object_find( p_playlist->p_input,
+            VLC_OBJECT_ACCESS, FIND_CHILD );
+        if( p_access )
+        {
+            vlc_bool_t b_can_pause;
+            if( !access2_Control( p_access, ACCESS_CAN_PAUSE, &b_can_pause ) &&
+                    b_can_pause )
+                i_caps |= CAPS_CAN_PAUSE;
+            vlc_object_release( p_access );
+        }
+        demux_t *p_demux = (demux_t*)vlc_object_find( p_playlist->p_input,
+            VLC_OBJECT_DEMUX, FIND_CHILD );
+        if( p_demux )
+        {   /* XXX: is: demux can seek and access can not a possibility ? */
+            vlc_bool_t b_can_seek;
+            if( !stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek ) &&
+                    b_can_seek )
+                i_caps |= CAPS_CAN_SEEK;
+            vlc_object_release( p_demux );
+        }
+    }
+
+    if( ((intf_thread_t*)p_this)->p_sys->b_meta_read )
+        i_caps |= CAPS_CAN_PROVIDE_METADATA;
+
+
     var_AddCallback( p_input, "state", StateChange, p_intf );
 
     vlc_object_release( p_input );